修复我的代码后出现错误,请考虑数据库中的用户ID全部设置为' 0'!
Outprint:
userID:' 23' itemID:' 8204'
更新预订SET userID =' 23' WHERE itemID =' 8204'
(错误:) 无法更新数据:您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在#1;'附近使用正确的语法。在第1行
第二次更新PHP代码:
<?php
$con = mysql_connect("localhost", "root", "") OR die(' Could not connect');
$db = mysql_select_db('book1', $con);
if (isset($_GET["userID"]) && isset($_GET["itemID"])){
$userID= (int)$_GET["userID"];
$itemID= (int)$_GET["itemID"];
$test = "userID: '$userID' itemID: '$itemID'";
echo $test;
echo "<br>";
}
if (!$con) {
die('Could not connect: '.mysql_error());
}
echo("UPDATE booking SET userID ='$userID' WHERE itemID ='$itemID'");
echo "<br>";
$upd = mysql_query("UPDATE booking SET userID ='$userID' WHERE itemID ='$itemID'");
$retval = mysql_query($upd, $con);
if(!$retval){
die('Could not update data: '.mysql_error());
}
echo "Updated data successfully\n";
$sql = mysql_query("SELECT * FROM booking");
if($sql === FALSE) {
die(mysql_error());
}
echo '<table class="fixed">
<tr>
<th>itemID</th>
<th>EMPLOYEE ID</th>
</tr>';
while($row = mysql_fetch_array($sql)) {
echo "<tr>";
echo"<td>".$row['itemID']."</td>";
echo"<td>".$row['userID']."</td>";
echo "</tr>";
}
&GT?;
答案 0 :(得分:3)
变量插值不适用于单引号。变化
mysql_query('UPDATE booking SET userID ="$userID" WHERE itemID ="$itemID"');
到
mysql_query("UPDATE booking SET userID ='$userID' WHERE itemID ='$itemID'");
但请注意,此代码易受SQL注入攻击。请参阅此主题如何防止它:How can I prevent SQL injection in PHP?
答案 1 :(得分:0)
如果您想按用户ID显示数据,那么您将更改代码---
从
更改此行$sql = mysql_query(" SELECT * FROM booking WHERE 1");
到
$sql = mysql_query(" SELECT * FROM booking WHERE 'userID' = '1'");
或者,如果您想按项目ID显示数据,那么您将更改代码----
$sql = mysql_query(" SELECT * FROM booking WHERE 'itemID' = '1'");
答案 2 :(得分:0)
确保您完全获得$userid
和$itemid
。
然后看看$upd
变量。您已在运行mysql_query
。将$upd
更改为
$upd = "UPDATE booking SET userID ='$userID' WHERE itemID ='$itemID'";
那么条件,
$retval = mysql_query($upd, $con);
if(!$retval){
die('Could not update data: '.mysql_error());
} else {
....
}
它可能会修复您的错误。