BlondePainter BlondePainter - 6 months ago 12
PHP Question

PDO in PHP not working

Every time I click the button, the update does not work as expected. However, if I change the

where id = 1
it works. Can someone please help?

require 'db.php';
echo 'sqsas'.$x;
$stmt=$conn->prepare("select * from tbluser where id=:id");

$stmt=$conn->prepare('update tbluser set username = :un, password = :pw,email = :em where id=:id');

and here is the HTML form:

<?php while($row = $stmt->fetch(PDO::FETCH_OBJ)) {?>
<form method="post" action="edit.php">
<td><?php echo $row->id; ?></td>

<td><input type="text" name="txtEmail" value="<?php echo $row->email; ?>" /></td>

<td><input type="text" name="txtUsername" value="<?php echo $row->username; ?>" /></td>
<?php } ?>
<td><input type="text" name="txtPassword" /></td>

<td><input type="submit" name="btnEdit" value="SAVE CHANGES" /></td>


The reason the ID is not being passed to your PHP is because it is never declared as a form element. In your HTML, you simply echo it on the screen. I assume this is because you don't want the user to edit the ID, just the name, password and email. However, if you want that to be passed to the PHP script when the form is submitted, it has to be declared as a form element such as <input>. You can hide it to prevent the user from editing it, such as below:

<input type="hidden" name="id" value="<?php echo $row->id; ?>">

You can put this anywhere, since it will be hidden, but logically a good place to put it would be in the same <tr></tr> block that you use for displaying the ID.