Here's the code from where I am sending id
In this code I am getting ID by using $_GET['id']
. At start the $_GET['id']
fetches id and the first query of SQL works to fetch the data in input fields but soon as I press EDIT button to update the data than the data stays same in database. I used echo after isset($_POST['edit_user'])
but there is nothing in $id.
<?php
session_start();
include("config.php");
//echo $id=$_SESSION['id'];
$id = isset($_GET['id']) ? $_GET['id'] : '';
$sql = "SELECT * FROM phone WHERE id='$id'";
$res = $conn->query($sql);
$row = $res->fetch_assoc();
echo $n = isset($row['name']) ? $row['name'] : '';
echo $phone = isset($row['contacts']) ? $row['contacts'] : '';
if (isset($_POST['edit_user'])) {
$name = $_POST['fname'];
$number = $_POST['num'];
//var_dump($name);
$sql = "UPDATE phone SET name='$name', contacts='$number' WHERE id='$id'";
//var_dump($sql);
//var_dump($res);
if ($conn->query($sql) == TRUE) {
echo "<script>alert('You have successfuly updated user') </script>";
header("location:index.php");
} else {
echo "<script>alert('Failed to edit user') </script>";
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>EDIT USER</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-md-12">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4" jumbotron my-5>
<h4 class="text-center my-2">Edit User</h4>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<label>Name</label>
<input type="text" name="fname" class="form-control" autocomplete="off" required value="<?php echo $n ?>">
<label>Number</label>
<input type="text" name="num" class="form-control" autocomplete="off" required value="<?php echo $phone ?>">
<input type="submit" name="edit_user" value="Edit User" class="edituser">
</form>
</div>
</div>
</div>
</div>
</body>
</html>