UPDATE .. SET .. WHERE SQL查询失败

时间:2013-07-04 06:19:51

标签: php sql

我正在尝试对我正在制作的网络应用程序进行排序,这会导致失败并且我不知道为什么!代码如下:

$update = $_GET['update'];
if($update == "true"){
    $setDetails="UPDATE users SET email='{$_POST['email']}', api_key='{$_POST['api_key']}', api_secret='{$_POST['api_secret']}' WHERE username={$_POST['username']}";
    if(mysql_query($setDetails)){
        $updatemsg = '<div class="alert alert-success"><a href="#" class="close" data-dismiss="alert">×</a><strong>Success!</strong> Your details have been updated in our database.</div>';
    }else{
        $updatemsg = '<div class="alert alert-error"><a href="#" class="close" data-dismiss="alert">×</a><strong>Failure!</strong> Your details could not be updated in our database. Please try again later or contact us if this keeps happening.</div>';
    }
}else if($update == "false"){
    $updatemsg = '<div class="alert alert-success"><a href="#" class="close" data-dismiss="alert">×</a><strong>Success!</strong> Your changed were discarded.</div>';
}

任何想法,帮助或提示?请注意,在我的网络应用程序的下方,我SELECT * FROM users WHERE username='$username'工作正常,因此数据库连接没有问题。

2 个答案:

答案 0 :(得分:3)

$update = $_GET['update'];
if($update == "true"){
    $setDetails="UPDATE users SET email='{$_POST['email']}', api_key='{$_POST['api_key']}', api_secret='{$_POST['api_secret']}' WHERE username='{$_POST['username']}'";
    if(mysql_query($setDetails)){
        $updatemsg = '<div class="alert alert-success"><a href="#" class="close" data-dismiss="alert">×</a><strong>Success!</strong> Your details have been updated in our database.</div>';
    }else{
        $updatemsg = '<div class="alert alert-error"><a href="#" class="close" data-dismiss="alert">×</a><strong>Failure!</strong> Your details could not be updated in our database. Please try again later or contact us if this keeps happening.</div>';
    }
}else if($update == "false"){
    $updatemsg = '<div class="alert alert-success"><a href="#" class="close" data-dismiss="alert">×</a><strong>Success!</strong> Your changed were discarded.</div>';
}

<强>详细信息: 你的代码:

$setDetails="UPDATE users SET email='{$_POST['email']}', api_key='{$_POST['api_key']}', api_secret='{$_POST['api_secret']}' WHERE username={$_POST['username']}";

正确代码:

 $setDetails="UPDATE users SET email='{$_POST['email']}', api_key='{$_POST['api_key']}', api_secret='{$_POST['api_secret']}' WHERE username='{$_POST['username']}'";

在用户名字符串中缺少'。

答案 1 :(得分:1)

请尝试这样。输入username

的引号
$setDetails="UPDATE users 
            SET email='{$_POST['email']}', 
                api_key='{$_POST['api_key']}', 
                api_secret='{$_POST['api_secret']}' 
            WHERE username='{$_POST['username']}' ";

由于已弃用,请尽量避免使用mysql_*语句。请勿使用mysqli_*语句或PDO语句