正如書名所描述的,我遇到了一個SQL注入錯誤:錯誤嘗試更新PHP/MySQL的代碼更改爲新密碼
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 '1' at line 1
我該如何解決這個問題?以下提供的是我的PHP代碼和HTML代碼
PHP:
if($_POST['submit']=='Change')
{
$err = array();
if(!$_POST['password1'] || !$_POST['passwordnew1'])
$err[] = 'All the fields must be filled in!';
if(!count($err))
{
$_POST['password1'] = mysql_real_escape_string($_POST['password1']);
$_POST['passwordnew1'] = mysql_real_escape_string($_POST['passwordnew1']);
$row = mysql_fetch_assoc(mysql_query("SELECT id,username FROM members WHERE username='{$_SESSION['username']}' AND pass='".md5($_POST['password1'])."'"));
if($row['username'])
{
$querynewpass = mysql_query("UPDATE members SET pass='".md5($_POST['passwordnew1'])."' WHERE username='{$_SESSION['username']}'");
$result = mysql_query($querynewpass) or die(mysql_error());
}
else $err[]='Wrong Password To Start With!';
}
if($err)
$_SESSION['msg']['passwordchange-err'] = implode('<br />',$err);
header("Location: members.php?id=" . $_SESSION['username']);
exit;
}
HTML:
<form action="" method="post">
<?php
if($_SESSION['msg']['passwordchange-err'])
{
echo '<div class="err">'.$_SESSION['msg']['passwordchange-err'].'</div>';
unset($_SESSION['msg']['passwordchange-err']);
}
if($_SESSION['msg']['passwordchange-success'])
{
echo '<div class="success">'.$_SESSION['msg']['passwordchange-success'].'</div>';
unset($_SESSION['msg']['passwordchange-success']);
}
?>
<label class="grey" for="password1">Current Password:</label>
<input class="field" type="password" name="password1" id="password1" value="" size="23" />
<label class="grey" for="password">New Password:</label>
<input class="field" type="password" name="passwordnew1" id="passwordnew1" size="23" />
<input type="submit" name="submit" value="Change" class="bt_register" style="margin-left: 382px;" />
</form>
我有工作,其中用戶能夠改變/更新其密碼,但是當,他們點擊表單上的更改按鈕,將它們定向到我在上面發佈的錯誤消息,並且如果他們單擊刷新按鈕,只有它們被重定向回到其配置文件並且已做出更改。所以我的主要問題在於,如何在沒有mysql錯誤消息的情況下完全工作?任何幫助將非常感激!
''{$ _SESSION ['username']}''我有一個預感它是這些引號。退出字符串並嘗試「'」。$ _ SESSION ['username']。''' –
試試這個WHERE username ='「。$ _ SESSION ['username']。''' –
我會給你一個鏡頭,一秒我會讓你知道它是否工作... – Michael