Hello Coders, today i develop a simple core PHP source code with very simple manner. Which you use for learn PHP basics very quick and easy way. Here i will show you a CRUD operation with image uploading and login registration operation.
Anyone want a complete file in ZIP comment below.
So from here i started the code with connection file, see below code and if have you any problem with these of any line you can ping me with comment box. And if you like it give me a word on comment how’s it.
Lets Start,
Connect.php
This is database connection file which is included on all other files.
On mysqli_connect function 1st parameter is HOST NAME, 2nd is Database User Name, 3rd is for Password and last 4th parameter is for Database Name.
1 2 3 4 5 6 7 |
<?php <strong>//Session is use for store value of variables and transfer it to one page to another</strong> session_start(); $con= mysqli_connect("localhost","root","","test") ?> |
Registration.php
This is user registration page where we create query for insertion user data into the database. Where mysqli 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 76 77 78 79 80 81 82 83 84 85 86 |
<?php <span style="color: #008000"><strong>//This is connection file which we create on first and include using PHP include function.</strong> </span> include 'connect.php'; <span style="color: #008000"><strong>//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</strong></span> if(isset($_POST['sub'])){ $t=$_POST['text']; $u=$_POST['user']; $p=$_POST['pass']; $c=$_POST['city']; $g=$_POST['gen']; if($_FILES['f1']['name']){ <span style="color: #008000"><strong>//This function is used for uploading image on the directory which you create on project root directory</strong></span> move_uploaded_file($_FILES['f1']['tmp_name'], "image/".$_FILES['f1']['name']); $img="image/".$_FILES['f1']['name']; } <span style="color: #008000"><strong>//This is insertion query of mysql</strong></span> $i="insert into reg(name,username,password,city,image,gender)value('$t','$u','$p','$c','$img','$g')"; <span style="color: #008000"><strong>//On extend version of mysql, mysqli takes two parameters where First is connection variable and second is query variable</strong></span> mysqli_query($con, $i); } ?> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <!--<span style="color: #008000"><strong>on this enctype attribute is important for uploading files and also mandatory</strong></span>--> <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
Next is login operation with mysqli SELECT query, If you want to know more about mysqli_num_rows which i use on below you can click on 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 54 |
<?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); <span style="color: #008000"><strong>//Here we check if our query is correct then databse have how many rows on o/p.</strong></span> if(mysqli_num_rows($qu)>0){ $f= mysqli_fetch_assoc($qu); <span style="color: #008000"><strong>//Here we store the value ID on session for login and fetch data of same user who is logged in.</strong></span> $_SESSION['id']=$f['id']; <span style="color: #008000"><strong>//if all conditions are correct then redirects to this page.</strong></span> 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 24 25 26 27 28 29 30 31 |
<?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); <span style="color: #008000"><strong>//we use while loop for all data of the user and fetch_assoc function useful for getting data from database</strong></span> 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 i get using SESSION varibale.
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 |
<?php include'connect.php'; <span style="color: #008000"><strong>//here we check the id with session id which we get on the time of login</strong></span> $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 |
<?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.
Thanks, Happy Coding…!
I want the complete file
Hello Coder,
Check your mail..!
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://phpcoder.tech/download/download-source-code-simple-php-mysql-crud-operation/