我已经创建了一个HTML表单,我希望让用户更新他/她的数据。所有插入和删除功能都运行良好,但更新函数返回错误说, "错误:您的SQL语法出错;查看与您的MariaDB服务器版本对应的手册,以获得正确的语法,以便在' WHERE regNumber =' IT17456734''在第1行。"无论我输入的注册号是什么,它都不允许我编辑现有数据。有人可以发现错误吗?由于我是PHP的初学者,我无法继续前进。这是我的PHP代码。
<?php
require 'db_connect.php';
&GT;
关于图书馆服务的评论
<div align = "center">
<h4>
<?php
if(!empty($_POST["regNo"]) && !empty($_POST["fullname"]) && !empty($_POST["comment"]) && !empty($_POST["faculty"]) && !empty($_POST["year"]))
{
$regNumber = $_POST["regNo"];
$name = $_POST["fullname"];
$comment = $_POST["comment"];
$year = $_POST["year"];
$faculty = $_POST["faculty"];
//echo "Successfully values collected from form.html";
}
else
{
$error = "One or more fields are not filled";
echo $error;
}
//insert data to the database
if(isset($_POST["btnInsert"]))
{
$insertString = "INSERT INTO library_comment VALUES('$regNumber', '$name', '$comment')";
$connection = mysqli_connect("127.0.0.1","root","","iwt");
if(!mysqli_query($connection, "INSERT INTO library_comment VALUES('$regNumber', '$name', '$comment')"))
{
die('Error: '.mysqli_error($connection));
}
else
{
echo '<br/>';
echo '1 record added...';
}
}
//update data in the database
else if(isset($_POST["btnUpdate"]))
{
$query = "UPDATE library_comment SET regNumber = '$regNumber', name = '$name', comment = '$comment' WHERE regNumber = '$regNumber'";
$connection = mysqli_connect("127.0.0.1","root","","iwt");
$result = mysqli_query($connection, $query);
if (!$result)
{
die('Error: '.mysqli_error($connection));
}
else
{
echo '<br/>';
echo '1 record updated...';
}
}
else if(isset($_POST["delete"]))
{
$updateString = "DELETE FROM library_comment WHERE regNo = '$regNumber'";
$connection = mysqli_connect("127.0.0.1","root","","iwt");
if(!mysqli_query($connection, $updateString))
{
die('Error: '.mysqli_error($connection));
}
else
{
echo '<br/>';
echo '1 record deleted...';
}
}
?>
<hr/>
</h4>
<div>
<!DOCTYPE html>
<html>
<head>
<style>
.btn{
border-radius: 5px;
}
</style>
</head>
<body>
<div align="center">
<form name = "comments" method = "POST" action = "addComment.php">
<table>
<caption><b><h2>Comments on Library Service </h2></b></caption>
<tr></tr>
<tr>
<td>Registration Number</td>
<td><input type="text" name="regNo" /></td>
</tr>
<tr>
<td>Name with initials</td>
<td><input type="text" name="fullname" placeholder="Kamal.P"/></td>
</tr>
<tr>
<td>Year :</td>
<td>
<input list="year" name="year" />
<datalist id="year">
<option value="Please choose">
<option value="Year 1">
<option value="Year 2">
<option value="Year 3">
<option value="Year 4">
</datalist>
</td>
</tr>
<tr>
<td>Faculty :</td>
<td>
<input list="faculty" name="faculty" />
<datalist id="faculty">
<option value="Please choose">
<option value="Faculty of Computing">
<option value="Faculty of Business">
<option value="Faculty of Engineering">
</datalist>
</td>
</tr>
<tr>
<td>Comments on the Service :</td>
<td><textarea name="comment" row="5" cols="30"></textarea></td>
</tr>
<tr>
<td>
<input class="btn" type="submit" name="btnInsert" value="Insert"/>
<input class="btn" type="submit" name="btnUpdate" value="Update"/>
<input class="btn" type="submit" name="delete" value="Delete"/>
</td>
<td>
<input class="btn" type="button" name="btnView" value="View"/>
<input class="btn" type="reset" name="btnReset" value="Reset"/>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
&#13;
答案 0 :(得分:0)
我已经解决了这个问题。问题在于where子句应该更正为&#34; [....] WHERE regNo =&#39; $ regNumber&#39;&#34;