PHP / HTML表单不更新MySQL [英] PHP/HTML Form not updating MySQL

查看:173
本文介绍了PHP / HTML表单不更新MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找到解决方案,我也寻找类似的线程,但没有运气。

基本上这是我的代码,当你点击Update它的意思是在表单字段中显示当前名称,然后您可以覆盖它们并提交更改,但不幸的是它不会更新,它只显示最初设置的名字和姓氏,并且不更新数据库,因此不显示新集名称。

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

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

$ b $ 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>编辑详情< / h3>

< form action =edit_profile.phpmethod =POST>

< input type =hiddenvalue =<?php echo $ row [0];?> NAME = ID/>

< h2>名字< / h2>
< input type =textname =newFirstvalue =<?php echo $ row [1];?>>

< h2>姓氏< / h2>
< input type =textname =newLastvalue =<?php echo $ row [2];?>>

< input type =submitvalue =更新>

< / form>


< / div>

任何帮助都将不胜感激:)

亲切的问候



〜Matt 您必须连接使用

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

还有其他一些错误,例如您必须使 $ POST ['newFirst '] as $ _ POST ['newFirst'] 像这样

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

并将查询改为

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

因为您在查询结束时出错 id ='first_name ='$ id'这是错误的

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

解决方案

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

这篇关于PHP / HTML表单不更新MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆