PHP更新MYSQL表单无效

时间:2021-01-07 00:13:13

Im trying to edit the lastname (lname) but its not working ,

我试着编辑lastname (lname),但是不行,

im getting this error :

我得到这个错误:

ERROR: Could not able to execute UPDATE tablename SET fname = '', lname = '' WHERE fname = . You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

错误:无法执行更新tablename设置fname = ", lname = " WHERE fname =。您的SQL语法有错误;请检查与MySQL服务器版本对应的手册,以获得在“第1行”附近使用的正确语法

<?php
$link = mysqli_connect("IP","DB","PASS (hiden ofc)", "DBN");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// Attempt update query execution
$sql = "UPDATE tablename SET fname = '$nfname', lname = '$nlname' WHERE fname = $fname";
if(mysqli_query($link, $sql)){
    echo "Records were updated successfully.";
} else {
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

--- HTML CODE ---

推荐- - - - - - - - - - - - HTML代码

<html>
<body>
<h1>Test editing </h1>
<form action="edit.php" method="post">
OrginalFirstname: <input type="text" name="fname" /><br><br>
Lastname: <input type="text" name="lname" /><br><br>
NewFirstname: <input type="text" name="nfname" /><br><br>
 
<input type="submit" />
</form>
</body>
</html>

1 个解决方案

#1


4  

You forgot to put apostrophes on the last part of your query:

您忘记在查询的最后部分加上撇号:

$sql = "UPDATE tablename SET fname = '$nfname', lname = '$nlname' WHERE fname = '$fname'";

This should work.

这应该工作。

Make sure you escaped all the variables with mysqli_real_escape_string. If one of the variables has a non-escaped apostrophe, the query will fail again.

确保您使用mysqli_real_escape_string对所有变量进行了转义。如果其中一个变量有一个非转义的撇号,那么查询将再次失败。

If the PHP code in your question is the entire code, then you are not getting the values from the $_POST[].

如果问题中的PHP代码是整个代码,那么您不会从$_POST[]中获取值。

You can get the values into your variables using extract($_POST); on the beggining of your code.

您可以使用extract($_POST)将值输入到变量中;在你的代码的乞丐。

#1


4  

You forgot to put apostrophes on the last part of your query:

您忘记在查询的最后部分加上撇号:

$sql = "UPDATE tablename SET fname = '$nfname', lname = '$nlname' WHERE fname = '$fname'";

This should work.

这应该工作。

Make sure you escaped all the variables with mysqli_real_escape_string. If one of the variables has a non-escaped apostrophe, the query will fail again.

确保您使用mysqli_real_escape_string对所有变量进行了转义。如果其中一个变量有一个非转义的撇号,那么查询将再次失败。

If the PHP code in your question is the entire code, then you are not getting the values from the $_POST[].

如果问题中的PHP代码是整个代码,那么您不会从$_POST[]中获取值。

You can get the values into your variables using extract($_POST); on the beggining of your code.

您可以使用extract($_POST)将值输入到变量中;在你的代码的乞丐。