Blog Introductions

In this  blog contains tutorials about  HTML, Python, CSS, How to, PHP Tutorials, Java, C++, Tutorials,  Examples, Source code,Learning.  Php with mysql database complete tutorials -Php Projects


Complete Curd  Operation in php with mysql






Complete Curd  Operation in php with mysql database tutorials - In Php curd opetation refer as Create table and Update table and insert record in tabes and delete the record in tables . In this project we will use php with mysql database complete tutorials where we will let you know about how we Create Database and Create Tables and insert record  in tables and then select or read data from tables  and then  delete data from table and then update data from tables . First we  Create database name DatabaseName in Mysql where we Create table name of  TableName and then make a connection of mysql data base with php .


Complete Curd  Operation in php with mysql database tutorials - Php with mysql database complete tutorials - Php Projects
Complete Curd  Operation in php with mysql database tutorials

  Write the database Name and Click ON Create Button.
  Now your data base will be created and now you can create tables 
  on your database.
   Now click on database you created 
    Now Click on create new table table_name and size of column            you   want to create click the go button and your table is ready 
    to insert update and delete records on the given tables.


Complete Curd  Operation in php with mysql database tutorials - Php with mysql database complete tutorials - Php Projects
Complete Curd  Operation in php with mysql database tutorials



Now Work on Php Code Part to Understand the Work


First Make a Connection With Database 
Here give servername is "LocalHost"
User Name is "Root"
Password is Blank ""
Dbname is "Your database Name"
All variables pass to mysqli functions
It will check if all data is match the your connection is ready otherwise it will give an error.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phppractice";
$msg="";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


Now Create Form in HTML 
Form attribute method is "Post"
Form attribute action is "Your File Name.php"
Now input attribute name ="Give name to access in Php Code"
Now Write the Submit Button and give name= "Submit"
Now Close the Form

<form method="post" action="test.php">
<h1>Form Project</h1>
<input type="text" name="name">
<br>
<input type="text" name="lname">
<br>
<input type="submit" name="submit">
<p style="color: red"><?php echo $msg; ?></p>
</form>

Some Style for Form Element like Text and Submit Button
input[type="text"]{
width: 300px;
height: 30px;
margin-top: 10px;
}
input[type="submit"]{
width: 300px;
height: 30px;
margin-top: 10px;
}


Now Write Php Code to Check if Form Button is subbmited or Not 
IF you Click on form button then this will get data from form elements and get into the Php Variables and the put in the SQL querry and insert into the you database table.

First it will check that is button is set or click or not.
After that  get data using input attibute name and store in php variables
Write querry of insert table 
 Then run the querry and  Check if querry is True or not if true then new record created successfully  otherwise it will give an errors.
if (isset($_POST['submit']))
{
$name = $_POST["name"];
$lname = $_POST["lname"];

$sql = "INSERT INTO student (id, name, lname)
VALUES('', '$name', '$lname')
";

if ($conn->query($sql) == = TRUE) {
$msg = "New record created successfully";
} else {
echo "Error: ". $sql."<br>". $conn->error;
}
}


Now Write the querry of Select Data and Write it into HTML TABLE and Print the result
Write the Select Querry
 Check if  result or number of row returned greater then zero 
 then get the data and print row by row using php while loop and write it into html table rows form.


< table >

< ?php
$sql = "SELECT * FROM student";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
? >
< tr >
< td > < ?php echo $row["id"]; ? > < / td >
< td > < ?php echo $row["name"]; ? > < / td >
< td > < ?php echo $row["lname"]; ? > < / td >
< td > < a href="test.php?id=<?php echo $row["id"];?> " > Delete < / a > < / td >

< / tr >
< ?php}
} else {
echo"0 results";
}





? >
< / table >


Here we will Create a delete button also for deleting the record
We create hrf attritbute and then pass a table row id to which we will delete the record using it id.
Here is Method to check if Get method with id is set  
IF set then get the id using Get Method 
Now write the Delete querry using given id
Run the querry and print the result 

if(isset($_GET["id"]))
{
$id = $_GET["id"];
$sql = "DELETE FROM student WHERE id='$id'";

if ($conn->query($sql) === TRUE) {
$msg = "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}

}

                            Here is Full Code Curd Operation




<?php

// Connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phppractice";
$msg="";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET["id"]))
{
$id = $_GET["id"];
$sql = "DELETE FROM student WHERE id='$id'";

if ($conn->query($sql) === TRUE) {
$msg = "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}

}
if(isset($_POST['submit']))
{
$name = $_POST["name"];
$lname = $_POST["lname"];
$sql = "INSERT INTO student (id, name, lname)
VALUES ('', '$name', '$lname')";

if ($conn->query($sql) === TRUE) {
$msg = "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
table ,td{
border:1px solid black;
border-collapse: collapse;
}
table{
width: 400px;
margin-left: 300px;
margin-top: 200px;
}
form{
margin-left: 300px;
}
input[type="text"]{
width: 300px;
height: 30px;
margin-top: 10px;
}
input[type="submit"]{
width: 300px;
height: 30px;
margin-top: 10px;
}
</style>
</head>
<body>
<form method="post" action="test.php">
<h1>Form Project</h1>
<input type="text" name="name">
<br>
<input type="text" name="lname">
<br>
<input type="submit" name="submit">
<p style="color: red"><?php echo $msg; ?></p>
</form>
<table>

<?php
$sql = "SELECT * FROM student";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?php echo $row["id"]; ?> </td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["lname"]; ?></td>
<td><a href="test.php?id=<?php echo $row["id"];?> ">Delete</a></td>
</tr>
<?php }
} else {
echo "0 results";
}

?>
</table>

</body>
</html>




      Here Is Out Put of this Crud Operations

Complete Curd  Operation in php with mysql database tutorials - Php with mysql database complete tutorials - Php Projects
Complete Curd  Operation in php with mysql database tutorials




I hope You really enjoyed this blog . I try my best to make every thing is easy and helpfull to provide to public .You will get more blog about the Programing project and code . Thanks for visiting my blog if you have any querry related to this you can comment here i will try my best to response you back about Complete Curd  Operation in php with mysql database tutorials