简体   繁体   中英

PHP/HTML Form not updating MySQL

I can't seem to find a solution to this and i've looked for similar threads too but no luck

Basically here's my code, when you click Update it's meant to display your current name in the form fields then you can overwrite them and submit the changes, however sadly it will not update, it only displays the originally set first name and last name and does not update the database so therefore not displaying the new set names.

<?php 
include('../connect_db.php'); 

$res = mysqli_query($dbconnection, "SELECT * FROM users");
$row = mysqli_fetch_array($res);


if(isset($_POST['newFirst']) && isset($_POST['newLast'])){
    $newFirst = $_POST['newFirst'];
    $newLast = $_POST['newLast'];
    $id = $_POST['id'];
    $sql = "UPDATE users SET first_name='$newFirst', last_name='$newLast' WHERE id='$id'";
    $res = mysqli_query($dbconnection, $sql);
}

?>

<div id="editSection">
<h3>Edit Details</h3>   

<form action="edit_profile.php" method="POST">

<input type="hidden" value="<?php echo $row[0];?>" name="id"/>

<h2>First Name</h2>
<input type="text" name="newFirst" value="<?php echo $row[1];?>">

<h2>Last Name</h2>
<input type="text" name="newLast" value="<?php echo $row[2];?>">

<input type="submit" value="Update">

</form>


</div>

Any help would be greatly appreciated :)

Kind Regards

~ Matt

I see some error in the query

$sql = "UPDATE users SET first_name='$newFirst', 
last_name='$newLast' WHERE id='first_name='$id'";

should be

$sql = "UPDATE users SET first_name='$newFirst', 
last_name='$newLast' WHERE id= '$id'";

also

if(isset($POST['newFirst']) && isset($POST['newLast'])){

should be

if(isset($_POST['newFirst']) && isset($_POST['newLast'])){

You have to connect to DB before updating.so use

$con=mysqli_connect("localhost","my_user","my_password","my_db"); 

There are several other errors like you have to make $POST['newFirst'] as $_POST['newFirst'] like this

if(isset($_POST['newFirst']) && isset($_POST['newLast'])){

And change the query to

$sql = "UPDATE users SET first_name='$newFirst',last_name='$newLast' WHERE id= '$id'";

beacuse you have error at end of query id='first_name='$id' which is wrong

You are using $POST wrong in your if-condition.

It must be called $_POST[..] .

Also you should take a look at your WHERE in your update query.

I think you mean: WHERE id= '$id'

You should get your id from $_POST['id']; which is your row ID i suppose and also the update query must be where id=$id.

$id = $_POST['id'];
$sql = "UPDATE users SET first_name='$newFirst', last_name='$newLast' WHERE id=$id";

Also have you checked in DB after the update? the row[0], row[1], row[2] used will have old set of values used during select before the update happened. can you have the mysqli_fetch_array($res) after the update call?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM