April 22, 2016
How to Edit/Update Records From MYSQLi Via Textbox in PHP

Hi, friends in this tutorial I show you how to edit/update records from a MySQL database via textbox in PHP. Using fetch_assoc function we are going to display records in the textbox from MySQL database. To update a record can be updated into MySQL tables by executing an SQL UPDATE statement through PHP function mysql_query.
disp.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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<?php //Connection for database include 'conn.php'; //Select Database $sql = "SELECT * FROM data"; $result = $conn->query($sql); ?> <!doctype html> <html> <body> <h1 align="center">Employee Details</h1> <table border="1" align="center" style="line-height:25px;"> <tr> <th>Employee ID</th> <th>Name</th> <th>Gender</th> <th>Department</th> <th>Address</th> <th>Mobile Number</th> <th>Email</th> </tr> <?php //Fetch Data form database if($result->num_rows > 0){ while($row = $result->fetch_assoc()){ ?> <tr> <td><?php echo $row['empid']; ?></td> <td><?php echo $row['name']; ?></td> <td><?php echo $row['gender']; ?></td> <td><?php echo $row['department']; ?></td> <td><?php echo $row['address']; ?></td> <td><?php echo $row['mobile']; ?></td> <td><?php echo $row['email']; ?></td> <!--Edit option --> <td><a href="edit.php?edit_id=<?php echo $row['empid']; ?>" alt="edit" >Edit</a></td> </tr> <?php } } else { ?> <tr> <th colspan="2">There's No data found!!!</th> </tr> <?php } ?> </table> </body> </html> |
edit.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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?php //Database Connection include 'conn.php'; //Get ID from Database if(isset($_GET['edit_id'])){ $sql = "SELECT * FROM data WHERE empid =" .$_GET['edit_id']; $result = mysqli_query($conn, $sql); $row = mysqli_fetch_array($result); } //Update Information if(isset($_POST['btn-update'])){ $name = $_POST['name']; $gender = $_POST['gender']; $department = $_POST['department']; $address = $_POST['address']; $mobile = $_POST['mobile']; $email = $_POST['email']; $update = "UPDATE data SET name='$name', gender='$gender',department='$department',address='$address',mobile='$mobile',email='$email' WHERE empid=". $_GET['edit_id']; $up = mysqli_query($conn, $update); if(!isset($sql)){ die ("Error $sql" .mysqli_connect_error()); } else { header("location: disp.php"); } } ?> <!--Create Edit form --> <!doctype html> <html> <body> <form method="post"> <h1>Edit Employee Information</h1> <label>Name:</label><input type="text" name="name" placeholder="Name" value="<?php echo $row['name']; ?>"><br/><br/> <label>Gender:</label><input type="text" name="gender" placeholder="Gender" value="<?php echo $row['gender']; ?>"><br/><br/> <label>Department:</label><input type="text" name="department" placeholder="Department" value="<?php echo $row['department']; ?>"><br/><br/> <label>Address:</label><input type="text" name="address" placeholder="Address" value="<?php echo $row['address']; ?>"><br/><br/> <label>Mobile:</label><input type="text" name="mobile" placeholder="Mobile" value="<?php echo $row['mobile']; ?>"><br/><br/> <label>Email:</label><input type="text" name="email" placeholder="email" value="<?php echo $row['email']; ?>"><br/><br/> <button type="submit" name="btn-update" id="btn-update" onClick="update()"><strong>Update</strong></button> <a href="disp.php"><button type="button" value="button">Cancel</button></a> </form> <!-- Alert for Updating --> <script> function update(){ var x; if(confirm("Updated data Sucessfully") == true){ x= "update"; } } </script> </body> </html> |
Download
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]