gpt4 book ai didi

php - mysqli_real_escape_string创建SQL语法错误

转载 作者:行者123 更新时间:2023-11-29 13:04:42 24 4
gpt4 key购买 nike

当我需要更改页面上的信息时,我正在编写一个更新数据库的脚本,如果文本中没有特殊字符(例如 ' 或 "),则脚本工作正常,如果有任何特殊字符我出现这样的错误

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 'angry' song, the music will incite surrounding characters to become more aggressive. As' at line 1

我希望得到帮助,看看我在这个脚本中可能错过了什么,提前感谢您的宝贵时间。

用于选择要更新的数据的初始页面

<?php
include('../connect/connect-mysql.php');

$sql="SELECT * FROM table Order by Appeared asc";
$result=mysql_query($sql);
?>
<h4>Update/Edit Tool</h4>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<th align="center"><font face="Verdana" size="1">Update</font></th>
<th align="center"><font face="Verdana" size="1">Power</font></th>
<th align="center"><font face="Verdana" size="1">Power2</font></th>
<th align="center"><font face="Verdana" size="1">Power3</font></th>
<th align="center"><font face="Verdana" size="1">Power4</font></th>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center"><font face="Verdana" size="1"><a href="p_update.php?id=<? echo $rows['IDNumber']; ?>">update</a></font></td>
<td><font face="Verdana" size="1"><div style="height:20px; width:160px; overflow:hidden"><? echo $rows['Power']; ?></div></font></td>
<td><font face="Verdana" size="1"><div style="height:20px; width:160px; overflow:hidden"><? echo $rows['Power2']; ?></div></font></td>
<td><font face="Verdana" size="1"><div style="height:20px; width:160px; overflow:hidden"><? echo $rows['Power3']; ?></div></font></td>
<td><font face="Verdana" size="1"><div style="height:20px; width:160px; overflow:hidden"><? echo $rows['Power4']; ?></div></font></td>
</tr>

<?php
}
?>

</table>

编辑页面(p_update.php):

$id=$_GET['id'];

$sql="SELECT * FROM table WHERE IDNumber='$id'";
$result=mysql_query($sql) or die(mysql_error());

$rows=mysql_fetch_array($result);
?>
<body>
<h1>Update Data</h1>
<form name="form1" method="post" action="p_update_ac.php">
<fieldset>
<legend>Update Data</legend>
<table border="1" width="100%" style="border-collapse: collapse">
<tr><th><font face="Verdana" size="1"><label>Database ID: </label></font></th><td><font size="1" face="Verdana"><input name="IDNumber" type="text" id="IDNumber" value="<?php echo $rows['IDNumber']; ?>" size="10"><b>DO NOT CHANGE THIS FIELD</b></font></td></tr>
<tr><th><font face="Verdana" size="1"><label>Power Category: </label></font></th><td><font size="1" face="Verdana"><input name="Powcategory" type="text" id="Powcategory" value="<?php echo $rows['Powcategory']; ?>" size="50"></font></td></tr>
<tr><th><font face="Verdana" size="1"><label>Power: </label></font></th><td><font size="1" face="Verdana"><input name="Power" type="text" id="Power" value="<?php echo $rows['Power']; ?>" size="150"></font></td></tr>
<tr><th><font face="Verdana" size="1"><label>Power 2: </label></font></th><td><font size="1" face="Verdana"><input name="Power2" type="text" id="Power2" value="<?php echo $rows['Power2']; ?>" size="150"></font></td></tr>
<tr><th><font face="Verdana" size="1"><label>Power 3: </label></font></th><td><font size="1" face="Verdana"><input name="Power3" type="text" id="Power3" value="<?php echo $rows['Power3']; ?>" size="150"></font></td></tr>
<tr><th><font face="Verdana" size="1"><label>Power 4: </label></font></th><td><font size="1" face="Verdana"><input name="Power4" type="text" id="Power4" value="<?php echo $rows['Power4']; ?>" size="150"></font></td></tr>
</table>
</fieldset>
<br>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>"><input type="submit" name="Submit" value="Update Record">
</form>
<?php
// close connection
mysql_close();
?>

以下代码是p_update_ac.php,由p_update.php脚本调用以运行。

<?php
include('../connect/connect-mysql.php');

$IDNumber = mysqli_real_escape_string($dbcon, $_POST['IDNumber']);
$Power = mysqli_real_escape_string($dbcon, $_POST['Power']);
$Power2 = mysqli_real_escape_string($dbcon, $_POST['Power2']);
$Power3 = mysqli_real_escape_string($dbcon, $_POST['Power3']);
$Power4 = mysqli_real_escape_string($dbcon, $_POST['Power4']);

$sql="UPDATE table SET Power='$Power',Power2='$Power2',Power3='$Power3',Power4='$Power4' WHERE IDNumber='$IDNumber'";
$result=mysql_query($sql) or die(mysql_error());

if($result){
echo "Successful";
echo "<p>";
echo "<a href='p_list_records.php'>View result</a>";

}

else {
echo "ERROR";
}

?>

最佳答案

您的 UPDATE 代码将不起作用,因为您混合了两个不同的 API,并且它们不会混合在一起。

我所说的 API 是指 mysqli_*mysql_* 函数。

我的印象是您的数据库连接实际上是 mysql_* 因为否则您不会收到错误消息,因此我在下面提供了两个版本,以防万一(参见脚注):

旁注:您应该将 mysqli_* 函数与准备好的语句或 PDO 结合使用。

如果mysql,请使用以下内容

<?php
include('../connect/connect-mysql.php');

$IDNumber = mysql_real_escape_string($_POST['IDNumber']);
$Power = mysql_real_escape_string($_POST['Power']);
$Power2 = mysql_real_escape_string(_POST['Power2']);
$Power3 = mysql_real_escape_string($_POST['Power3']);
$Power4 = mysql_real_escape_string($_POST['Power4']);

$sql="UPDATE table SET Power='$Power',Power2='$Power2',Power3='$Power3',Power4='$Power4' WHERE IDNumber='$IDNumber'";
$result=mysql_query($sql,$dbcon) or die(mysql_error());

if($result){
echo "Successful";
echo "<p>";
echo "<a href='p_list_records.php'>View result</a>";

}

else {
echo "ERROR";
}

?>
<小时/>

如果mysqli,请使用以下内容

<?php
include('../connect/connect-mysql.php');

$IDNumber = mysqli_real_escape_string($dbcon, $_POST['IDNumber']);
$Power = mysqli_real_escape_string($dbcon, $_POST['Power']);
$Power2 = mysqli_real_escape_string($dbcon, $_POST['Power2']);
$Power3 = mysqli_real_escape_string($dbcon, $_POST['Power3']);
$Power4 = mysqli_real_escape_string($dbcon, $_POST['Power4']);

$sql="UPDATE table SET Power='$Power',Power2='$Power2',Power3='$Power3',Power4='$Power4' WHERE IDNumber='$IDNumber'";
$result=mysqli_query($dbcon,$sql) or die(mysqli_error());

if($result){
echo "Successful";
echo "<p>";
echo "<a href='p_list_records.php'>View result</a>";

}

else {
echo "ERROR";
}

?>

要切换到 mysqli_* 连接,请将 xxx 替换为您的数据库凭据。

DEFINE ('DB_USER', 'xxx');
DEFINE ('DB_PASSWORD', 'xxx');
DEFINE ('DB_HOST', 'xxx');
DEFINE ('DB_NAME', 'xxx');

$dbcon = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die("could not connect");
<小时/>

脚注:

mysql_* 函数弃用通知:

http://www.php.net/manual/en/intro.mysql.php

从 PHP 5.5.0 开始,此扩展已被弃用,并且不建议用于编写新代码,因为它将在将来被删除。相反,要么 mysqliPDO_MySQL应使用扩展名。另请参阅MySQL API Overview在选择 MySQL API 时获取更多帮助。

这些函数允许您访问 MySQL 数据库服务器。有关 MySQL 的更多信息,请访问 » http://www.mysql.com/ .

MySQL 文档可以在 » http://dev.mysql.com/doc/ 找到.

关于php - mysqli_real_escape_string创建SQL语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22886038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com