This question already has an answer here:
Im required to create a form to update the data. I've created my form and made my php coding, there are no errors appear but the data are not updated.
After i clicked submit, there are no error message instead there is a successful message saying my records has been updated when it's not updated.
Im still a student who are new in php can someone help me look at my coding and see what I've missed? thank you o much in advance.
I would appreciate any help that you can give. Ive gotten so far with this thing and yet I cant get it to update the record.
>>>Edit<<<
I have found a solution to this question, my answer is below:
Page9.php:
<?php
require('db.php');
include("auth.php");
$id=$_REQUEST['id'];
$query = "SELECT * from new_record where id='".$id."'";
$result = mysqli_query($con, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Update Record</title>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="form">
<p><a href="dashboard.php">Dashboard</a>
| <a href="insert.php">Insert New Record</a>
| <a href="logout.php">Logout</a></p>
<h1>Update Record</h1>
<?php
$status = "";
if(isset($_POST['new']) && $_POST['new']==1)
{
$id=$_REQUEST['id'];
$trn_date = date("Y-m-d H:i:s");
$name =$_REQUEST['name'];
$age =$_REQUEST['age'];
$submittedby = $_SESSION["username"];
$update="update new_record set trn_date='".$trn_date."', name='".$name."', age='".$age."', submittedby='".$submittedby."' where id='".$id."'";
mysqli_query($con, $update) or die(mysqli_error());
$status = "Record Updated Successfully. </br></br>
<a href='view.php'>View Updated Record</a>";
echo '<p style="color:#FF0000;">'.$status.'</p>';
}else {
?>
<div>
<form name="form" method="post" action="">
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['id'];?>" />
<p><input type="text" name="name" placeholder="Enter Name" required value="<?php echo $row['name'];?>" /></p>
<p><input type="text" name="age" placeholder="Enter Age" required value="<?php echo $row['age'];?>" /></p>
<p><input name="submit" type="submit" value="Update" /></p>
</form>
<?php } ?>
</div>
</div>
</body>
</html>
Credits: http://www.allphptricks.com/simple-user-registration-login-script-in-php-and-mysqli/
The above link that I credited is suitable for some new students who just started php.
</div>
You should use POST to retrieve the ID since the HTML is sending via POST method.
form action="page9.php" method="POST"
Based on the code you have posted, the $_GET
parameter noID
is never set. You don't define it in the URL your action
property of your form, which is where it would need to be if you want to pass it as a GET parameter. That said, you probably meant to use it as a POST parameter. Either way, it isn't set.
This means your SQL contains ... WHERE noID = ''
, which most likely matches nothing, so nothing gets updated.
Also, you are wide open to SQL injection. You need to use prepared statements, rather than concatenating variables into your query. See How can I prevent SQL injection in PHP?.