Providing Training Since 2009
  • Home -
  • Insert/Update/Delete (CRUD) using PHP/MySqli

Insert/Update/Delete (CRUD) using PHP/MySqli

 

STEP 1: We will create form and mention method=”post” action=” ”

[php dark=”true”]

<form method="post" action="">
First name:<input type="text" name ="f_name"></br></br>
Last name:<input type="text" name="l_name"></br></br>
Email:<input type="email" name="email"></br></br>
Password:<input type="password" name="password"></br></br>
Address:<textarea name="address"></textarea></br></br>
Gender :male.<input type="radio" name="gender" value="male">
female.<input type="radio" name="gender" value="female">
</br></br>
DOB: <select name="dd">
<?php
for($i=1; $i<=31;$i++){
echo "<option value=’".$i."’>" .$i."</option>";
}
?>
</select>
<select name="mm">
<?php
for($i=1; $i<=12;$i++){
echo "<option value=’".$i."’>" .$i."</option>";
}
?>
</select>
<select name="yy">
<?php
for($i=1950; $i<=2018;$i++){
echo "<option value=’".$i."’>" .$i."</option>";
}
?>
</select>
</br></br>
hobbies: Movies<input type="checkbox" name="hobbies[]" value="movies">
Reading<input type="checkbox" name="hobbies[]" value="reading">
Cricket<input type="checkbox" name="hobbies[]" value="cricket">
Swiming<input type="checkbox" name="hobbies[]" value="swiming">
</br></br>
city: <input type="text" name="city" value="city">
</br></br>
country: <select name="country">
<option value="India">India</option>
<option value="Pakistan">Pakistan</option>
<option value="China">China</option>
<option value="Srilanka">Srilanka</option>
</select>
</br></br>
<input type="submit" name="submit" value="submit">

</form>

[/php]

 

STEP 2:  The output of the form in browser.

STEP3:  Now, we will start our PHP code by making database. First we will create database and then table below is the table structure.

 

STEP4: We will make a separate page for database connection (db_connect.php) and this code we will just include in our every pages

 

[php dark=”true”]

<?php
$con=mysqli_connect(‘localhost’,’root’,”) or die(‘Database connection error’);
mysqli_select_db($con,’registration’) or die(‘database error’);
?>

[/php]

 

STEP 5: We will start writing insert query in insert.php page, and show how we connect form with database. 

                        

[php dark=”true”]

<?php
include(‘db_connect.php’);

if($_POST){

$fname= $_POST[‘f_name’];
$lname = $_POST[‘l_name’];
$Email = $_POST[’email’];
$Password = $_POST[‘password’];
$Address = $_POST[‘address’];
$gender = $_POST[‘gender’];
$DOB = $_POST[‘dd’].’/’.$_POST[‘mm’].’/’.$_POST[‘yy’];
$hobbies = $_POST[‘hobbies’];
$hbs =”;
for($i=0; $i<count($hobbies); $i++)
{
$hbs .= $hobbies[$i].’,’;
}
$city = $_POST[‘city’];
$country = $_POST[‘country’];

$ins = "INSERT INTO register(f_name,l_name,email,password,address,gender,DOB,hobbies,city,country)
values(‘$fname’,’$lname’,’$Email’,’$Password’,’$Address’,’$gender’,’$DOB’,’$hbs’,’$city’,’$country’)";

mysqli_query($con,$ins);
header(‘location:update.php’);
}
?>

[/php]

 

Form fill up in browser:   

 

Form details saved in database :   

 

STEP 6: Now, we want our details to display in browser, so we will create view. php page.   

[php dark=”true”]

<?php
include(‘db_connect.php’);
?>

<table width="800" border="1">
<tr>
<td>first name</td>
<td>last name</td>
<td>email</td>
<td>address</td>
<td>gender</td>
<td>date of birth</td>
<td>hobbies</td>
<td>city</td>
<td>country</td>
<td>EDIT</td>
<td>DELETE</td>
</tr>

<?php
$sel="select*from register";
$qry=mysqli_query($con,$sel);
while($rows=mysqli_fetch_array($qry)){
?>

<tr>
<td><?php echo $rows[‘f_name’] ?></td>
<td><?php echo $rows[‘l_name’] ?></td>
<td><?php echo $rows[’email’] ?></td>
<td><?php echo $rows[‘address’] ?></td>
<td><?php echo $rows[‘gender’] ?></td>
<td><?php echo $rows[‘DOB’] ?></td>
<td><?php echo $rows[‘hobbies’] ?></td>
<td><?php echo $rows[‘city’] ?></td>
<td><?php echo $rows[‘country’] ?></td>
<td><a href="edit.php?id=<?php echo $rows[‘id’]?>">EDIT</a></td>
<td><a href="delete.php?id=<?php echo $rows[‘id’]?>">DELETE</a></td>
</tr>

<?php
}
?>

[/php]

 

Form in Browser:   

STEP 7 :  If we want to delete any row from the form, we will make delete.php page and delete row by using ID. 

[php dark=”true”]

<?php
include(‘db_connect.php’);
$del="delete from register where id=’".$_GET[‘id’]."’";
mysqli_query($con,$del);
header(‘location:update.php’);
?>

[/php]

 

STEP 8: If we want to edit any row from the form, we will make edit.php and first we will edit form. 

[php]

<form method="post" action="">
First name:<input type="text" name ="f_name" value="<?php echo $rs[‘f_name’]?>"></br></br>
Last name:<input type="text" name="l_name" value="<?php echo $rs[‘l_name’]?>"></br></br>
Email:<input type="email" name="email" value="<?php echo $rs[’email’]?>"></br></br>
Address:<textarea name="address"> <?php echo $rs[‘address’]?></textarea></br></br>
Gender :male.<input type="radio" name="gender" value="male"
<?php if($rs[‘gender’]== ‘male’) {echo’checked’;} ?>>
female.<input type="radio" name="gender" value="female"
<?php if($rs[‘gender’]== ‘female’) {echo’checked’;} ?>>
</br></br>

DOB: <select name="dd">
<?php
$date=explode(‘/’, $rs[‘DOB’]);

for($i=1; $i<=31;$i++){ ?>
<option value='<?php echo $i;?>’

<?php if($date[0]==$i) {echo "selected";} ?>

<?php echo "<option value=’".$i."’>" .$i."</option>";
}
?>
</select>

<select name="mm">
<?php
$date=explode(‘/’, $rs[‘DOB’]);
//print_r($date);
for($i=1; $i<=12;$i++){ ?>
<option value='<?php echo $i;?>’

<?php if($date[1]==$i) {echo "selected";} ?>

<?php echo "<option value=’".$i."’>" .$i."</option>";
}
?>
</select>

<select name="yy">
<?php

$date=explode(‘/’, $rs[‘DOB’]);
for($i=1950; $i<=2018;$i++){ ?>
<option value='<?php echo $i;?>’

<?php if($date[2]==$i) {echo "selected";} ?>

<?php echo "<option value=’".$i."’>" .$i."</option>";
}
?>
</select>
</br></br>

<?php $hobbies= explode(‘,’,$rs[‘hobbies’]); ?>

hobbies: Movies<input type="checkbox" name="hobbies[]" value="movies"
<?php if(in_array(‘movies’,$hobbies)){echo "checked";} ?>>
Reading<input type="checkbox" name="hobbies[]" value="reading"
<?php if(in_array(‘reading’,$hobbies)){echo "checked";} ?>>
Cricket<input type="checkbox" name="hobbies[]" value="cricket"
<?php if(in_array(‘cricket’,$hobbies)){echo "checked";} ?>>
Swiming<input type="checkbox" name="hobbies[]" value="swiming"
<?php if(in_array(‘swiming’,$hobbies)){echo "checked";} ?>>
</br></br>
city: <input type="text" name="city" value="<?php echo $rs[‘city’]?>">
</br></br>
country: <select name="country">
<option value="India"
<?php if($rs[‘country’]==’India’){echo "selected";} ?>>India</option>
<option value="Pakistan"
<?php if($rs[‘country’]==’Pakistan’){echo "selected";} ?>>Pakistan </option>
<option value="China"
<?php if($rs[‘country’]==’China’){echo "selected";} ?>>China </option>
<option value="Srilanka"
<?php if($rs[‘country’]==’Srilanka’){echo "selected";} ?>>Srilanka</option>
</select>
</br></br>
<input type="submit" name="submit" value="submit">
</form>

[/php]

 

Now, edit query:

[php]

<?php
include(‘db_connect.php’);

if($_POST){

$fname= $_POST[‘f_name’];
$lname = $_POST[‘l_name’];
$Email = $_POST[’email’];
$Address = $_POST[‘address’];
$gender = $_POST[‘gender’];
$DOB = $_POST[‘dd’].’/’.$_POST[‘mm’].’/’.$_POST[‘yy’];
$hobbies = $_POST[‘hobbies’];
$hbs =”;
for($i=0; $i<count($hobbies); $i++) { $hbs .= $hobbies[$i].’,’; }
$city = $_POST[‘city’];
$country = $_POST[‘country’];

$upd="update register set
f_name=’$fname’,
l_name=’$lname’,
email=’$Email’,
address=’$Address’,
gender=’$gender’,
DOB=’$DOB’,
hobbies=’$hbs’,
city=’$city’,
country=’$country’

where id=’".$_GET[‘id’]."’";
mysqli_query($con,$upd);
header(‘location:update.php’);
}
$sel="select * from register where id=’".$_GET[‘id’]."’";
$qry= mysqli_query($con,$sel);
$rs=mysqli_fetch_array($qry);
?>

[/php]

 

Edit in browser by using ID: 

Updated row in browser: