We develop a simple core PHP CRUD Operation Using MySQLi With example source code. Which you use to learn PHP basics very quick and easy way. Here I will show you Create, read, update and delete operation with image uploading.
Anyone want a complete file in ZIP Check comment below.
So from here, we have started the code by creating a PHP MySQLi connection file, see the below code and if have you any problem with these of any line you can ping me with the comment box.
To Create a complete PHP CRUD Operation we create those required files,
- connect.php
- registration.php
- Login.php
- home.php
- view.php
- edit.php
- delete.php
And if you like it give me a word on comment how’s it.
Lets Start,
Connect.php (DB Connection File)
This is the database connection file, that is included in all other files to maintain the DB connection globally.
On mysqli_connect
function first parameter is HOSTNAME, second is Database User Name, third is for Password and the last parameter is for Database Name.
1 2 3 4 5 6 |
<?php //Session is use for store value of variables and transfer it to one page to another session_start(); $con= mysqli_connect("localhost","root","","test") ?> |
Registration.php (PHP User register File)
This is the user registration page where we create a query for the insertion of user data into the database. Where we use MySQLi which is the newer or extended version of MySQL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
<?php //This is connection file which we create on first and include using PHP include function. include 'connect.php'; //we use isset function, where it take submit button name and check button is clicked or not, if not clicked than code is not running if (isset($_POST['sub'])) { $t = $_POST['text']; $u = $_POST['user']; $p = $_POST['pass']; $c = $_POST['city']; $g = $_POST['gen']; if ($_FILES['f1']['name']) { //This function is used for uploading image on the directory which you create on project root directory move_uploaded_file($_FILES['f1']['tmp_name'], "image/" . $_FILES['f1']['name']); $img = "image/" . $_FILES['f1']['name']; } //This is insertion query of mysql $i = "insert into reg(name,username,password,city,image,gender)value('$t','$u','$p','$c','$img','$g')"; //On extend version of mysql, mysqli takes two parameters where First is connection variable and second is query variable mysqli_query($con, $i); } ?> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <!--on this enctype attribute is important for uploading files and also mandatory--> <form method="POST" enctype="multipart/form-data"> <table> <tr> <td> Name <input type="text" name="text"> </td> </tr> <tr> <td> Username <input type="text" name="user"> </td> </tr> <tr> <td> password <input type="password" name="pass"> </td> </tr> <tr> <td> city <select name="city"> <option value="">-select-</option> <option value="knp">kanpur</option> <option value="lko">lucknow</option> </td> </tr> <tr> <td> Gender <input type="radio" name="gen" id="gen" value="male">male <input type="radio" name="gen" id="gen" value="female">female </td> </tr> <tr> <td> Image <input type="file" name="f1"> </td> </tr> <tr> <td> <input type="submit" value="submit" name="sub"> </td> </tr> </table> </body> </html> |
Login.php (PHP User Login File)
Next is the login operation with MySQLi SELECT query, If you want to know more about mysqli_num_rows which we use below you can click on the link.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?php include 'connect.php'; if (isset($_POST['sub'])) { $u = $_POST['user']; $p = $_POST['pass']; $s = "select * from reg where username='$u' and password= '$p'"; $qu = mysqli_query($con, $s); //Here we check if our query is correct then databse have how many rows on o/p. if (mysqli_num_rows($qu) > 0) { $f = mysqli_fetch_assoc($qu); //Here we store the value ID on session for login and fetch data of same user who is logged in. $_SESSION['id'] = $f['id']; //if all conditions are correct then redirects to this page. header('location:home.php'); } else { echo 'username or password does not exist'; } } ?> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form method="POST" enctype="multipart/form-data"> <table> <tr> <td> Username <input type="text" name="user"> </td> </tr> <tr> <td> password <input type="password" name="pass"> </td> </tr> <tr> <td> <input type="submit" name="sub" value="submit"> </td> </tr> </table> </body> </html> |
View user’s data
View.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php include 'connect.php'; ?> <table border='1'> <tr> <th> Name </th> <th> Username </th> </tr> <?php $sq="select * from reg"; $qu=mysqli_query($con,$sq); //we use while loop for all data of the user and fetch_assoc function useful for getting data from database while($f= mysqli_fetch_assoc($qu)){ ?> <tr> <td><?php echo $f['name']?></td> <td><?php echo $f['username']?></td> </tr> <?php } ?> |
Home.php
On this page you can see particular user data which we get by using PHP SESSION.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php include'connect.php'; //here we check the id with session id which we get on the time of login $s="select*from reg where id='$_SESSION[id]'"; $qu= mysqli_query($con, $s); $f=mysqli_fetch_assoc($qu); ?> <html> <head> </head> <body> <table> <tr> <td> Name </td> <td> <?php echo $f['name'];?> </td> </tr> <tr> <td> Username </td> <td> <?php echo $f['username'];?> </td> </tr> <tr> <td> Password </td> <td> <?php echo $f['password']."<br>";?> </td> </tr> <tr> <td> City </td> <td> <?php echo $f['city']."<br>";?> </td> </tr> <tr> <td>Gender </td> <td> <?php echo $f['gender']."<br>";?> </td> </tr> <tr> <td> Image </td> <td> <img src="<?php echo $f['image'];?>" width="100px" height="100px"> </td> </tr> </table> <a href="edit.php">Edit </a> <a href="delete.php">Delete </a> </body> </html> |
Edit.php
See how we edit the particular user data using UPDATE query and SESSION.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<?php include'connect.php'; if(isset($_POST['sub'])){ $t=$_POST['text']; $u=$_POST['user']; $p=$_POST['pass']; $c=$_POST['city']; $g=$_POST['gen']; if($_FILES['f1']['name']){ move_uploaded_file($_FILES['f1']['tmp_name'], "image/".$_FILES['f1']['name']); $img="image/".$_FILES['f1']['name']; } else{ $img=$_POST['img1']; } $i="update reg set name='$t',username='$u',password='$p',city='$c',gender='$g',image='$img' where id='$_SESSION[id]'"; mysqli_query($con, $i); header('location:home.php'); } $s="select*from reg where id='$_SESSION[id]'"; $qu= mysqli_query($con, $s); $f=mysqli_fetch_assoc($qu); ?> <form method="POST" enctype="multipart/form-data"> <table> <tr> <td> Name <input type="text" name="text" value="<?php echo $f['name']?>"> </td> </tr> <tr> <td> Username <input type="text" name="user" value="<?php echo $f['username']?>"> </td> </tr> <tr> <td> password <input type="password" name="pass" value="<?php echo $f['password']?>"> </td> </tr> <tr> <td> city <select name="city"> <option value="">-select- </option> <option value="knp" <?php if($f['city']=='knp'){ echo "selected='selected'";}?>>kanpur</option> <option value="lko"<?php if($f['city']=='lko'){ echo "selected='selected'";}?>>lucknow</option> </td> </tr> <tr> <td> Gender <?php if($f['gender']=='male'){ ?> <input type="radio"name="gen" id="gen" value="male" checked> <?php } else { ?> <input type="radio"name="gen" id="gen" value="male"> <?php } ?>male <?php if($f['gender']=='female'){ ?> <input type="radio"name="gen" id="gen" value="female" checked> <?php } else { ?> <input type="radio"name="gen" id="gen" value="female"> <?php } ?>female </td> </tr> <tr> <td> Image <img src="<?php echo $f['image']?>" width="100px" height="100px"> <input type="file" name="f1"> <input type="hidden" name="img1" value="<?php echo $f['image']?>"> </td> </tr> <tr> <td> <input type="submit" value="submit" name="sub"> </td> </tr> </table> </form> |
Last is delete file. DELETE.php
1 2 3 4 5 6 |
<?php include 'connect.php'; $sq="delete from reg where id='$_SESSION[id]'"; mysqli_query($con,$sq); header('location:add_district.php'); ?> |
Above is complete CRUD operation with image uploading and Login Registration operation with some easy manner.
Any query, feel free to ping me on mail Contact us or on comment.
Also Read:
- How to Check Process Running in Windows
- HTML: How to Comment With Example
- JavaScript While Loop with an Example
- Set PHP Error Reporting Into The PHP File
Happy Coding…!
I want the complete file
Hello Coder,
Check your mail..!
I want completed file
https://codingtasks.net/download/download-source-code-simple-php-mysql-crud-operation/
Please sand complete project source code
please send to me for complete code
Please check your email.
please send code o deepakmarudih786@gmail.com
Download from here: Click Here
Please share the code
Download from here: Click Here
Download from here: http://codingtasks.net/download/download-source-code-simple-php-mysql-crud-operation/
[…] Simple PHP MySQL CRUD Operation […]
send complete source file
Download from here: http://codingtasks.net/download/download-source-code-simple-php-mysql-crud-operation/
[…] Also Read: PHP CRUD Operation Using MySQLi (Source Code) […]
tancks
hi iwant curd operations complete code like edit update edit and delete code in php
[…] Also Read: PHP CRUD Operation Using MySQLi (Source Code) […]