简体   繁体   English

PHP / HTML表单未更新MySQL

[英]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 还有其他一些错误,例如您必须像这样使$POST['newFirst']成为$_POST['newFirst']

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 因为您在查询id='first_name='$id'末尾有错误,这是错误的

You are using $POST wrong in your if-condition. 您在if条件中使用$POST错误。

It must be called $_POST[..] . 它必须称为$_POST[..]

Also you should take a look at your WHERE in your update query. 另外,您还应该在更新查询中查看WHERE

I think you mean: WHERE id= '$id' 我认为您的意思是: WHERE id= '$id'

You should get your id from $_POST['id']; 您应该从$ _POST ['id']获取您的ID; which is your row ID i suppose and also the update query must be where id=$id. 我想这是您的行ID,更新查询也必须在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. 在发生更新之前,在选择期间使用的row [0],row [1],row [2]将具有旧的一组值。 can you have the mysqli_fetch_array($res) after the update call? 您可以在更新调用后使用mysqli_fetch_array($ res)吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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