![](/img/trans.png)
[英]Getting syntax error in mysql-php. You have an error in your SQL syntax;
[英]Getting “You have an error in your SQL syntax” error - PHP, MySQL
我正在編寫一個“編輯”頁面,該頁面從數據庫獲取信息並以表格形式顯示它,並允許用戶單擊“編輯”對其進行修改。 但是,我收到此錯誤:
您的SQL語法有誤; 檢查與您的MySQL服務器版本相對應的手冊,以在第5行的'notes ...'WHERE id = 2'附近使用正確的語法
我已經檢查了很多代碼以弄清楚它,但是沒有運氣。 這是我的代碼:
<?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代碼中找不到語法錯誤,請查看一下。 提前致謝。
您需要轉義查詢中使用的所有變量:
$ind_name = mysql_real_escape_string($_POST["ind_name"]);
$age = intval($_POST["age"]);
$gender = mysql_real_escape_string($_POST["gender"]);
$notes = mysql_real_escape_string($_POST["notes"]);
$ind = intval($_GET["ind"]);
$query = "UPDATE individual SET
ind_name = '$ind_name',
age = $age,
gender = '$gender',
notes = '$notes'
WHERE id = $ind";
您可以從輸出查詢開始。
您確定$_GET["ind"]}
是整數嗎?
將WHERE id = {$_GET["ind"]} ";
更改為此:
WHERE id = ". $_GET["ind"];
請注意,它對SQL注入開放!
這是您的問題:
WHERE id = {$_GET["ind"]} ";
您正在使用雙引號。
您真的不應該這樣玩$_GET
:
$query = "UPDATE individual SET
ind_name = '{$ind_name}',
age = {$age},
gender = '{$gender}',
notes = '{$notes}'
WHERE id = {$_GET["ind"]} ";
我強烈建議您像WHERE id = " . $_GET['ind'] . "";
您應該對剛剛發布的代碼非常小心,因為它很容易被惡意代碼注入。 始終清理變量 。 用戶提供的任何輸入都假定您輸入的內容是錯誤的,並且他們想對您的應用程序使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.