How To Insert, Delete, Edit, Update And Select Data From Mysql Database Using Php, Sql, Html And Css Tutorial

How To Insert, Delete, Edit, Update And Select Data From Mysql Database Using Php, Sql, Html And Css Tutorial – Source Code

You can update, delete, select and insert data into mysql database using php, html, sql easily using the code below. To delete, select and update data, you must first insert it in the database. After you click insert button the data is inserted in mysql database. And also displayed on html table. To delete the data, click delete button next to the row with the data you want to delete. Editing the data in a no-brainer, just click edit button or link next to the data you want to edit, the data will be displayed on the input boxes, just edit it and enter the required data and finally click save.

insertdeleteedit.php
<?php

include("db.php");
if(isset($_POST&#91;'submitinserdetails'&#93;)) {
  $firstname = $_POST&#91;'firstname'&#93;;
  $lastname = $_POST&#91;'lastname'&#93;;
  $email = $_POST&#91;'email'&#93;;
if(!empty($firstname) && !empty($lastname) && !empty($email) ) {
 $sql = "INSERT INTO `insertdeleteedittable`( `firstname`, `lastname`, `email`)
                                     VALUES ('$firstname','$lastname','$email')";
$qry = mysqli_query($connect, $sql);
if($qry) {
 echo "inserted successfully";
}
} else {
 echo "all fields must be filled";
}
}

?>



<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
<form action="" method="POST">
<input type="text" name="firstname"><br ><br >
<input type="text" name="lastname"><br ><br >
<input type="text" name="email"><br ><br >
<input type="submit" name="submitinserdetails" value="insert">
</form>
</body>
</html>



db.php



<?php
$connect = mysqli_connect("localhost", "root", "");
if ($connect) {
echo "<br/> Connected to server";
}else{
die("<br />Connection error ". mysqli_connect_error());
}
$selectdb = mysqli_select_db($connect, "insertdeleteedit");
if ($selectdb) {
echo "<br />Existing Database Selected";
} else {
$sqlcreatedb = "CREATE DATABASE IF NOT EXISTS `insertdeleteedit`";
if (mysqli_query($connect, $sqlcreatedb)) {
echo "<br />New database created";
$selectdb2 = mysqli_select_db($connect, "insertdeleteedit");
if ($selectdb2) {
echo "<br />Created database selected";
$sqlcreatetable = "
CREATE TABLE IF NOT EXISTS `insertdeleteedittable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(100) NOT NULL,
`lastname` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
";
if (mysqli_query($connect,$sqlcreatetable)) {
echo "<br />New table created";
} else {
echo "<br />No table created";
}
}
} else {
echo "<br />No database created";
} 
}
?>



display.php



<?php
include("db.php");
?>
<!DOCTYPE html>
<html>
<head>
 <title></title>
 <style type="text/css">
table {
 border:1px solid black;
 border-collapse: collapse;
}
td{
 border:1px solid black;
 padding: 10px;
}

 </style>

</head>
<body>
<table>
<tr>
<?php
$sel = "SELECT * FROM `insertdeleteedittable` ";
$qrydisplay = mysqli_query($connect, $sel);
while($row = mysqli_fetch_array($qrydisplay)){
 $id  =  $row&#91;'id'&#93;;
 $firstname  =  $row&#91;'firstname'&#93;;
 $lastname  =  $row&#91;'lastname'&#93;;
 $email  =  $row&#91;'email'&#93;;
 echo "<tr><td>".$id."</td><td>".$firstname."</td><td>".$lastname."</td><td>".$email."</td><td><a href='edit.php?edit=$id' >Edit</a></td><td><a href='delete.php?deleteid=$id' >Delete</a></td></tr>";
}
?>
</tr>
</table>
</body>
</html>

edit.php

<?php
include("db.php");
$getid = $_GET&#91;'edit'&#93;;
$seledittwo = "SELECT * FROM `insertdeleteedittable` WHERE `id` = '$getid'";
$qry = mysqli_query($connect, $seledittwo);
$selassoc = mysqli_fetch_assoc($qry);
$id = $selassoc&#91;'id'&#93;;
$firstname = $selassoc&#91;'firstname'&#93;;
$lastname = $selassoc&#91;'lastname'&#93;;
$email = $selassoc&#91;'email'&#93;;
if(isset($_POST&#91;'updateedit'&#93;)) {
 $upid =  $_POST&#91;'upid'&#93;;
 $upfirstname =  $_POST&#91;'upfirstname'&#93;;
 $uplastname =  $_POST&#91;'uplastname'&#93;;
 $upemail =  $_POST&#91;'upemail'&#93;;


 $seleditt = "UPDATE `insertdeleteedittable` SET `firstname`='$upfirstname',`lastname`='$uplastname',`email`='$upemail' WHERE `id` = '$upid'";
 $qry = mysqli_query($connect,$seleditt);
 if($qry) {
  header("location: display.php");
 }
}

//$seledit = "UPDATE `insertdeleteedittable` SET `id`=&#91;value-1&#93;,`firstname`=&#91;value-2&#93;,`lastname`=&#91;value-3&#93;,`email`=&#91;value-4&#93; WHERE `id` = '$getid'";

?>

<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
<form method="POST" action="">
   <input type="text" name="upid" value="<?php echo $id; ?>"><br><br>
 <input type="text" name="upfirstname" value="<?php echo $firstname; ?>"><br><br>
 <input type="text" name="uplastname" value="<?php echo $lastname ; ?>"><br><br>
 <input type="text" name="upemail" value="<?php echo $email; ?>"><br><br>
 <input type="submit" name="updateedit" value="Update">
</form>
</body>
</html>

delete.php


<?php
include("db.php");
$getid = $_GET&#91;'deleteid'&#93;;
$sel = "DELETE FROM `insertdeleteedittable` WHERE `id` = '$getid'";
$qry = mysqli_query($connect, $sel);
if($qry) {
 header("location: insertdeleteedit.php");
}

?>

How To Insert, Delete, Edit, Update And Select Data From Mysql Database Using Php, Sql, Html And Css Tutorial
How To Insert, Delete, Edit, Update And Select Data From Mysql Database Using Php, Sql, Html And Css Tutorial
How To Insert, Delete, Edit, Update And Select Data From Mysql Database Using Php, Sql, Html And Css Tutorial
How To Insert, Delete, Edit, Update And Select Data From Mysql Database Using Php, Sql, Html And Css Tutorial
How To Insert, Delete, Edit, Update And Select Data From Mysql Database Using Php, Sql, Html And Css Tutorial
How To Insert, Delete, Edit, Update And Select Data From Mysql Database Using Php, Sql, Html And Css Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *