【发布时间】:2011-08-08 03:34:39
【问题描述】:
我正在编写一个“编辑”页面,该页面从数据库中获取信息并将其显示在表单中,并让用户通过单击编辑来修改它。但是,我收到此错误:
您的 SQL 语法有错误;检查手册 对应于您的 MySQL 服务器版本,以便使用正确的语法 靠近'notes ...' WHERE id = 2' 在第 5 行
为了弄明白,我已经检查了很多代码,但没有运气。这是我的代码:
<?php
if (isset($_POST["submit"])) {
$ind_name = $_POST["ind_name"];
$age = $_POST["age"];
$gender = $_POST["gender"];
$notes = $_POST["notes"];
$query = "UPDATE individual SET
ind_name = '{$ind_name}',
age = {$age},
gender = '{$gender}',
notes = '{$notes}'
WHERE id = {$_GET["ind"]} ";
$result = mysql_query($query);
if (mysql_affected_rows () == 1) {
header("Location: edit_ind.php?ind={$_GET["ind"]}");
} else {
echo "ERROR : " . mysql_error();
}
}
?>
这是我的 html 表单代码:
<form action="edit_ind.php?ind=<?php echo $_GET["ind"]; ?>" method ="post" >
<div id="formWrapper_ind">
<label for="ind_name">Individual Name : </label>
<?php $ind_name_form = get_ind_info_ind("ind_name"); ?>
<input type="text" placeholder="Individual Name" name="ind_name" value="<?php echo $ind_name_form; ?>" required>
<br/>
<label for="age">Age : </label>
<?php $ind_age_form = get_ind_info_ind("age"); ?>
<input type="text" name="age" placeholder="Age" value="<?php echo $ind_age_form; ?>" required>
<br/>
<label for="gender">Gender : </label>
<div id="radios">
<?php $ind_gender_form = get_ind_info_ind("gender"); ?>
<p><input type="radio" name="gender" value="Male" <?php if ($ind_gender_form== 'Male') {echo "checked"; } ?>> Male
<input type="radio" name="gender" value="Female" <?php if ($ind_gender_form == 'Female') {echo "checked"; } ?>> Female</p>
</div>
<br/>
<label for="notes">Notes : </label>
<?php $ind_notes_form = get_ind_info_ind("notes"); ?>
<textarea placeholder="Individual Notes..." name="notes"><?php echo $ind_notes_form; ?></textarea>
<div id="buttons">
<input type="reset" value="Reset">
<input type="submit" name="submit" value="Done">
</div>
</div>
</form>
我在我的 SQL 代码中找不到语法错误,请看一下。提前致谢。
【问题讨论】:
-
您的代码似乎对SQL Injection 开放。
-
@Brian - 这就是为什么我把它作为评论而不是答案。我仍然认为应该提及这一点,以防 OP 不知道此漏洞。
-
@Brain,没有代码比拥有可注入 SQL 的代码好。 @rafael 在将该 var 输入 MySQL 查询之前使用 "$var = mysql_real_escape_string($var)。它将保护您免受 SQL 注入。
-
@rafael - 不,你不需要全部逃避。您需要使用参数化查询。
-
@rafael - 很高兴听到这个消息。祝你学习顺利:)