How to edit or update Table values in HTML webpage using php
update.php
<!DOCTYPE html>
<html>
<head>
<title>Edit/Update Data</title>
</head>
<body>
<?php
include_once('config.php');
//Fetching all data (rows or tuples) from the student_details table
$roll_no = $_GET['id'];
$query = "SELECT * FROM students_details WHERE roll_no=$roll_no";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
$name= $row['name'];
$email = $row['email'];
$address = $row['address'];
$phone_number = $row['phone_number'];
?>
<h1 align="center">Edit/Update Data</h1>
<form action="config.php" method="POST">
<table border='1px' align="center" style="width:90%;">
<tr>
<th>Roll No</th>
<td>
<input type="text" name="roll_no" value="<?php echo $roll_no; ?>">
</td>
</tr>
<tr>
<th>Name</th>
<td>
<input type="text" name="name" value="<?php echo $name; ?>">
</td>
</tr>
<tr>
<th>Email</th>
<td>
<input type="text" name="email" value="<?php echo $email; ?>">
</td>
</tr>
<tr>
<th>Address</th>
<td>
<input type="text" name="address" value="<?php echo $address; ?>">
</td>
</tr>
<tr>
<th>Phone Number</th>
<td>
<input type="text" name="phone_number" value="<?php echo $phone_number; ?>">
</td>
</tr>
<tr>
<th></th>
<td>
<input type="submit" name="editprofile" value="Save">
</td>
</tr>
</table>
</form>
</body>
</html>
Config.php
<?php
//Database connection
$server = "localhost";
$usrename = "root";
$password = "";
$database = "handson_demo";
$conn = mysql_connect($server,$usrename,$password);
$db = mysql_select_db($database,$conn);
if (!$db) {
die("Database connection failed ! >> ".mysqli_connect_error());
}else
{
//echo "Database connection Success";
}
?>
<?php
// ---------------------------Update Profile --------------------------------------------------
if(isset($_POST['editprofile'])){
$roll_no= $_POST['roll_no'];
$name= $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$phone_number = $_POST['phone_number'];
$q="UPDATE students_details SET
roll_no='$roll_no',
name='$name',
email='$email',
address='$address',
phone_number = '$phone_number'
WHERE roll_no='$roll_no'";
echo $q;
$result=mysql_query($q);
if($result){
echo "<script>alert('Profile updated Successfully!');</script>";
//echo "<script>location.href='50_myprofile.php';</script>";
}
else{
echo "<script>alert('Failed!');</script>";
//echo "<script>location.href='50_myprofile.php';</script>";
}
}
?>

Comments
Post a Comment