As we already see “how to insert data in mysql using php form“. But if want to edit some information in that form how will I do that? Should I directly update form without seeing the previous data? Or should I check my previous data first and then update specific data?
Answer is obvious that first I have to look at previous data then I will update that. But how to get data and display in form
Don’t worry in this post I am going to show how to retrieve data from database and display in php form.
I am taking my previous example of insert data. In that post I created a form with title and content field only. So this time I am not going to create database and table because i already created both of them in my previous post. Now let’s start
Create database connection:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $host = "localhost"; $user = "root"; $pass = ""; $db = "demo"; $conn = mysqli_connect($host,$user,$pass, $db); if(!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?> |
Retrieve Data from Database:
1 2 3 4 5 6 7 8 |
<?php $sql = "select * from posts where id = 1"; $rs = mysqli_query($conn, $sql); $fetchRow = mysqli_fetch_assoc($rs); ?> |
In above php code I get the row which id is 1. mysqli_fetch_assoc()
function will return that specific row as an array (which holds title and content) and I store that array in $fetchRow
variable.
FORM HTML:
Now I get the row from the database it’s time to show each column data into its respective fields.
1 2 3 4 5 6 7 8 9 10 |
<div class="container"> <h3>Add Post</h3> <form action="" method="post"> <input type="text" name="title" value="<?php echo $fetchRow['post_title']?>" required> <textarea cols="40" placeholder="Post Content" rows="8" name="post_content" required><?php echo $fetchRow['post_content']?></textarea> <button type="submit" name="submit">Submit</button> </form> </div> |
After that above changes your form will look like this.
Apply CSS:
1 2 3 4 5 6 7 8 |
body{font-family:verdana;} .container{width:500px;margin: 0 auto;} h3{line-height:20px;font-size:20px;} input{display:block;width:350px;height:20px;margin:10px 0;} textarea{display:block;width:350px;margin:10px 0;} button{background:green; border:1px solid green;width:70px;height:30px;color:#ffffff} |
All Together
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 |
<?php $host = "localhost"; $user = "root"; $pass = ""; $db = "demo"; $conn = mysqli_connect($host,$user,$pass, $db); if(!$conn) { die("Connection failed: " . mysqli_connect_error()); } ?> <?php $sql = "select * from posts where id = 1"; $rs = mysqli_query($conn, $sql); //get row $fetchRow = mysqli_fetch_assoc($rs); ?> <!DOCTYPE html> <html> <head> <title> Retrieve data from database and display in php form</title> <style> body{ font-family:verdana; } .container{width:500px;margin: 0 auto;} h3{line-height:20px;font-size:20px;} input{display:block;width:350px;height:20px;margin:10px 0;} textarea{display:block;width:350px;margin:10px 0;} button{background:green; border:1px solid green;width:70px;height:30px;color:#ffffff} </style> </head> <body> <div class="container"> <h3>Edit Post</h3> <form action="" method="post"> <input type="text" name="title" value="<?php echo $fetchRow['post_title']?>" required> <textarea cols="40" placeholder="Post Content" rows="8" name="post_content" required><?php echo $fetchRow['post_content']?></textarea> <button type="submit" name="submit">Submit</button> </form> </div> </body> </html> |