php update sql database from form

Solutions on MaxInterview for php update sql database from form by the best coders in the world

showing results for - "php update sql database from form"
Arya
25 May 2016
1<?php
2
3include "config.php"; // Using database connection file here
4
5$id = $_GET['id']; // get id through query string
6
7$qry = mysqli_query($db,"select * from emp where id='$id'"); // select query
8
9$data = mysqli_fetch_array($qry); // fetch data
10
11if(isset($_POST['update'])) // when click on Update button
12{
13    $fname = $_POST['fname'];
14    $lname = $_POST['lname'];
15	
16    $edit = mysqli_query($db,"update emp set fname='$fname', lname='$lname' where id='$id'");
17	
18    if($edit)
19    {
20        mysqli_close($db); // Close connection
21        header("location:all_records.php"); // redirects to all records page
22        exit;
23    }
24    else
25    {
26        echo mysqli_error();
27    }    	
28}
29?>
30
31<h3>Update Data</h3>
32
33<form method="POST">
34  <input type="text" name="fname" value="<?php echo $data['fname'] ?>" placeholder="Enter Full Name" Required>
35  <input type="text" name="lname" value="<?php echo $data['lname'] ?>" placeholder="Enter Last Name" Required>
36  <input type="submit" name="update" value="Update">
37</form>