Hi readers, today i going to post one of old topic in php and mysql, here i'm going to post Insert data through form, and fetch / view the data from database, and also Edit, Delete and Update with detailed explanation. i know mysql is depreciated, even most of the people are using mysql so now i'm going to use mysql and i'll update with mysqli and pdo later, now i'm going to explain the basics of php mysql functions, how to insert, fetch, delete, update like all operations. New with Codeigniter insert edit view update delete
FILES
- db.php
- index.php
- insert.php
- view.php
- edit.php
These are the files needed to execute the basic insert fetch edit delete operation, let see what the file contains. i already posted how to create database and insert code and AJAX insert without refresh the page.
DB.PHP
first we have to connect the db file.
first we have to connect the db file.
<?php ob_start(); define('DB_SERVER', 'localhost'); define('DB_USERNAME', 'root'); define('DB_PASSWORD', ''); define('DB_DATABASE', '2my4edge'); $connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error()); $database = mysql_select_db(DB_DATABASE) or die(mysql_error()); ?>
i believe you can get clear idea from the color variation, RED all database fields, and the other all separated one with other same.
DATABASE NAME --> 2my4edge
TABLE NAME --> sample
TABLE FIELDS --> id(int, primary key, auto increment), username(varchar), emailid(varchar), mobileno(varchar), created(timestamp)
INDEX.PHP
here i'm just viewing the view.php file below the form but you can make that separate page for that. the action will be on insert.php page. method is post.
INSERT.PHP
the index.php page form is run in the insert.php.
if the data is successfully registered, the header location will be to index.php
VIEW.PHP
View page contains edit and delete link in the while loop, here we are passing the values though id for edit and delete, like
we have to give this link before the while loop ends,TABLE NAME --> sample
TABLE FIELDS --> id(int, primary key, auto increment), username(varchar), emailid(varchar), mobileno(varchar), created(timestamp)
INDEX.PHP
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Insert form</title> <link type="text/css" media="all" rel="stylesheet" href="style.css"> </head> <body> <div class="display"> <form action="insert.php" method="post" name="insertform"> <p> <label for="name" id="preinput"> USER NAME : </label> <input type="text" name="username" required placeholder="Enter your name" id="inputid"/> </p> <p> <label for="email" id="preinput"> EMAIL ID : </label> <input type="email" name="usermail" required placeholder="Enter your Email" id="inputid" /> </p> <p> <label for="mobile" id="preinput"> MOBILE NUMBER : </label> <input type="text" name="usermobile" required placeholder="Enter your mobile number"
id="inputid" /> </p> <p> <input type="submit" name="send" value="Submit" id="inputid1" /> </p> </form> </div> <?php include('view.php'); ?> </body> </html>
INSERT.PHP
the index.php page form is run in the insert.php.
<?php ob_start(); include("db.php"); if(isset($_POST['send'])!="") { $username=mysql_real_escape_string($_POST['username']); $usermail=mysql_real_escape_string($_POST['usermail']); $usermobile=mysql_real_escape_string($_POST['usermobile']); $update=mysql_query("INSERT INTO sample(username,emailid,mobileno,created)VALUES ('$username','$usermail','$usermobile',now())"); if($update) { $msg="Successfully Updated!!"; echo "<script type='text/javascript'>alert('$msg');</script>"; header('Location:index.php'); } else { $errormsg="Something went wrong, Try again"; echo "<script type='text/javascript'>alert('$errormsg');</script>"; } } ob_end_flush(); ?>
VIEW.PHP
<?php include('db.php'); $select=mysql_query("SELECT * FROM sample order by id desc"); $i=1; while($userrow=mysql_fetch_array($select)) { $id=$userrow['id']; $username=$userrow['username']; $usermail=$userrow['emailid']; $usermobile=$userrow['mobileno']; $created=$userrow['created'] ?> <div class="display"> <p> USER NAME : <span><?php echo $username; ?></span> <a href="delete.php?id=<?php echo $id; ?>" onclick="return confirm('Are you sure you wish to delete this Record?');"> <span class="delete" title="Delete"> X </span></a> </p> <br /> <p> EMAIL ID : <span><?php echo $usermail; ?></span> <a href="edit.php?id=<?php echo $id; ?>"><span class="edit" title="Edit"> E </span></a> </p> <br /> <p> MOBILE NO : <span><?php echo $usermobile; ?></span> </p> <br /> <p> CREATED ON : <span><?php echo $created; ?></span> </p> <br /> </div> <?php } ?>
View page contains edit and delete link in the while loop, here we are passing the values though id for edit and delete, like
<a href="delete.php?id=<?php echo $id; ?>" onclick="return confirm('Are you sure you wish to delete this Record?');"> X </a>
<a href="edit.php?id=<?php echo $id; ?>"> E </a>
DELETE.PHP
<?php ob_start(); include("db.php"); if(isset($_GET['id'])!="") { $delete=$_GET['id']; $delete=mysql_query("DELETE FROM sample WHERE id='$delete'"); if($delete) { header("Location:index.php"); } else { echo mysql_error(); } } ob_end_flush(); ?>
EDIT.PHP
this edit page contains both edit form and also update code.
<?php ob_start(); include('db.php'); if(isset($_GET['id'])) { $id=$_GET['id']; if(isset($_POST['update'])) { $eusername=$_POST['eusername']; $eusermail=$_POST['eusermail']; $emobile=$_POST['eusermobile']; $updated=mysql_query("UPDATE sample SET username='$eusername', emailid='$eusermail', mobileno='$emobile' WHERE id='$id'")
or die(); if($updated) { $msg="Successfully Updated!!"; header('Location:index.php'); } } } //update ends here ob_end_flush(); ?> <!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Edit form</title> <link type="text/css" media="all" rel="stylesheet" href="style.css"> </head> <body> <?php if(isset($_GET['id'])) { $id=$_GET['id']; $getselect=mysql_query("SELECT * FROM sample WHERE id='$id'"); while($profile=mysql_fetch_array($getselect)) { $username=$profile['username']; $usermail=$profile['emailid']; $usermobile=$profile['mobileno']; ?> <div class="display"> <form action="" method="post" name="insertform"> <p> <label for="name" id="preinput"> USER NAME : </label> <input type="text" name="eusername" required placeholder="Enter your name" value="<?php echo $username; ?>" id="inputid" /> </p> <p> <label for="email" id="preinput"> EMAIL ID : </label> <input type="email" name="eusermail" required placeholder="Enter your Email" value="<?php echo $usermail; ?>" id="inputid" /> </p> <p> <label for="mobile" id="preinput"> MOBILE NUMBER : </label> <input type="text" name="eusermobile" required placeholder="Enter your mobile number" value="<?php echo $usermobile; ?>" id="inputid" /> </p> <p> <input type="submit" name="update" value="Update" id="inputid1" /> </p> </form> </div> <?php } } ?> </body> </html>
REALATED POSTS :
I am beginner for php. tutorial was helped me lot. thank u .
ReplyDeletethank for coding
ReplyDeleteGood script, this can be the beginning of something great
ReplyDeletewow !!!!!
ReplyDeletereally too much useful and helpful too
thanx Arunkumar Maha for these work
hgjhgjhgj
Deletenotbad
ReplyDeleteGood
Deleteyour site is too much loading when it was open...................
ReplyDeletethank you your site is very useful
ReplyDeleterealy
Deletewhen will you release the codes for pdo? mysql is already depreciated. thanks!
ReplyDeletethanks for information and the artikel is not bad
ReplyDeleteThanks for the nice documentation. Its really helpful for the beginner.......
ReplyDeleteHi, i'm a newbie in PHP coding. May I ask which part does the form validation located?
ReplyDeleteThanks for the nice documentation. Its really helpful for the beginner....... thanks very muchhhh
ReplyDeleteThank Q so much. these examples are very useful.
ReplyDeletewow thanks for this tutorial. this the best tutorial that i see in the web so far.
ReplyDeletedo you know what is panel manager in vb.net because i think that is the best way to create a tabulated form using panel and a button hide or unhide, that panel is too good to use than using tabcontrol in a 1 form in vb.net
ReplyDeletehey guys
ReplyDeleteI'm a first year computer science student and it's help me a lot.
ReplyDeleteThank you.
Aww To Easy for me you Know That
DeleteTHANKS FOR THE USEFUL CODES
ReplyDeleteIts Ok
DeleteThanks for the useful post.
ReplyDeleteThanks for the post. Keep it up guys.
ReplyDeleteThank You so much. I'm a beginner, it helped me a lot..
ReplyDeletei am really appreciated with your academic help. PS
ReplyDeleteHow to view the data from db and modify the data then update data?
ReplyDeletethis example is helped me a lot.......
ReplyDeleteIts Clear ...good job and Thanks
ReplyDeletereally this is help for beginners...
ReplyDeletethank you!!!!
I have a problem I cannot figure out. I used the above scripts, I can add but I cannot edit, I get a blank screen. I have found other examples on the net and have the same issue. I would think it is permissions but I can add and I get the response back after adding. Any idea's?
ReplyDeletesame problem with me
DeleteThanks For sharing
ReplyDeletewww.rationaltechnologies.com
i am not able to insert data. it display nothing.
ReplyDeleteBHAI PLS CHECK THE TABLE NMBR IS WRONG ON EDIT PAGE, PLS CHECK IN THESE 2 Vriables
Delete1- $updated
2-$getselect
Please give me solution of this Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Final\edit.php on line 37
DeletePlease give me solution of this Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Final\edit.php on line 37
DeleteI experience this warning.Please chech your select query,Data is get or not
DeleteNice tutorials for beginner, thanks for sharing this tutorial.
ReplyDeleteThanks so much..this tutorial.
ReplyDeleteAll the Best.
juber khan
Thanks !
ReplyDeleteAll the Best
Juber khan
its a very good code program i help fully ..
ReplyDeleteThanks a lot. Your code is really helpful.
ReplyDeleteAll the best.
Thanks for your coding, very helpful, I do however have 1 question. Can these 4 files (view, add, edit, delete) be combined in 1 file?
ReplyDeleteThanks for your coding, very helpful, I do however have 1 question. Can these 4 files (view, add, edit, delete) be combined in 1 file?
ReplyDeletethank u so much...really helpful this tutorial
ReplyDeleteNice Code
ReplyDeleteWow Thank you so much. This is just what i was looking for. And it worked on my first try! Thanks again
ReplyDeletesimple example in insert delete in php
ReplyDeletesir....i did like this
ReplyDeletebut edit form is completely white....help me
Very help full INFORMATION,,,,SIR
ReplyDeletestyle.css file is needed.
ReplyDeleteYour tutorials inspired me to start a blog.
ReplyDeleteInsert Update Delete in PHP
Thanks for the tutorial.
ReplyDeleteAhmad
Miri, Sarawak, Malaysia.
You are a gifted programmer and a good teacher. Thanks a lot.
ReplyDeleteaction=""
ReplyDeleteIn edit.php what action we need to give
this is piece of shit
ReplyDeletehow to get single record of table in database by using login page(php)
ReplyDeletewhat is the table called iedu
ReplyDeleteFatal error: Call to undefined function mysql_connect() in C:\xampp\htdocs\database\db.php on line 7
ReplyDeleteHi i am newbie this is my problem
ReplyDelete( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\gelo\EDIT.PHP on line 37
Call Stack
# Time Memory Function Location
1 0.0010 145560 {main}( ) ..\EDIT.PHP:0
2 0.0410 169096 mysql_fetch_array ( ) ..\EDIT.PHP:37
can you help me please? thank you
thank u so much.
ReplyDeletethank u for sharing this
ReplyDeleteEdit.php is not workig
ReplyDeletethank you so much
ReplyDeletethank you so much
ReplyDeletecan u do this in MVC ?
ReplyDeleteWarning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\edit\edit.php on line 37
ReplyDeletehow to view data in edit page for editing
ReplyDeleteGiven so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ..Best PHP Training in Chennai|PHP Training in Chennai
ReplyDeleteA lot of many many thank to u sir because i have many problem in crud function .so i learned all crud function......
ReplyDeleteplease in the edit.php i want to know what the iedu stands for
ReplyDeleteSorry @T-Rex xx, That is actually table name, i have changed now. that `sample`. table name.
DeleteWhat is the function ob_start(); for ?
ReplyDeleteIts really good documentation for both fresher and experience also
ReplyDeleteGreat work....!
Thank mr this cod is vre useful
ReplyDeleteThank you This cod is very useful
ReplyDeletethank you so much ... really it's very easy and simple code .....very very useful... thanks again
ReplyDelete