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
ReplyDeleteI believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
ReplyDeletedatascience training in chennai
ReplyDeleteIt's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.
Php course in chennai
this is a very helpful post. it's really code.
ReplyDeleteAt first, you have to download the most recent rendition of Adobe Acrobat Reader to get to the shape, and further this product encourages you to see, download, finish and print the frame. https://w9.pdffiller.com
ReplyDeleteI accept there are numerous more pleasurable open doors ahead for people that took a gander at your site.we are providing ReactJs training in Chennai.
ReplyDeleteFor more details: ReactJs training in Velachery | ReactJs training in chennai
The programmers must mention the required columns specifically in the SQL query to keep data secure and reduce resource consumption.plakatų spausdinimas
ReplyDeleteGood job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
ReplyDeleteBlue Prism Training in Chennai
Blue Prism Training Chennai
Blue Prism Training in OMR
Blue Prism Training in TNagar
Blue Prism Training in Annanagar
Blue Prism Training in Porur
This was really one of the best blog i have read recently. Thanks for sharing this post with us.
ReplyDeleteIELTS Classes in Mumbai
IELTS Coaching in Mumbai
IELTS Mumbai
Best IELTS Coaching in Mumbai
IELTS Center in Mumbai
IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
IELTS Training in Chennai
IELTS Chennai
Best IELTS Coaching in Chennai
Thank you This code is very useful but Here’s a basic tutorial on creating pagination on php https://bit.ly/2IPvdE1
ReplyDeletethanks for great help.
ReplyDelete, here i found some tutorial on Code for Insert View Update Delete In Php MySQL for more detail Click Here
thanks for great help.
ReplyDelete, here i found some tutorial on Code for Insert View Update Delete In Php MySQL for more detail Click Here
Thanks for posting this information it really useful for everyone.
ReplyDeleteFrench Classes in Chennai
french courses in chennai
Spoken English Classes in Chennai
TOEFL Coaching in Chennai
pearson vue test center in chennai
German Language Course in Chennai
IELTS Coaching in anna nagar
ielts coaching in chennai anna nagar
Just seen your Article, it amazed me and surpised me with god thoughts that eveyone will benefit from it. It is really a very informative post for all those budding entreprenuers planning to take advantage of post for business expansions. You always share such a wonderful articlewhich helps us to gain knowledge .Thanks for sharing such a wonderful article, It will be deinitely helpful and fruitful article.
Thanks
DedicatedHosting4u.com
This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
ReplyDeleteIELTS Coaching in Coimbatore
IELTS Coaching Center in Coimbatore
German Classes in Coimbatore
Spoken English Class in Coimbatore
Digital Marketing Training in Coimbatore
Digital Marketing Course in Coimbatore
Embedded course in Coimbatore
Java Training in Coimbatore
Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
ReplyDeleteJava Training in Chennai
Java Training in Coimbatore
Java Training in Bangalore
Nice information, want to know about Selenium Training In Chennai
ReplyDeleteSelenium Training In Chennai
Data Science Course In Chennai
Protractor Training in Chennai
jmeter training in chennai
Rpa Training Chennai
Rpa Course Chennai
Selenium Training institute In Chennai