为什么我的MYSQL UPDATE语句不会更新?

时间:2014-04-19 02:11:01

标签: javascript php mysql

<script>
function enter_edit(column)
{
var username = '<?php echo $username ;?>';
var password = '<?php echo $password ;?>';
var input = prompt("Please enter a new " + column , "");
//window.alert(input);
//window.alert(column);
//window.alert(username);
//window.alert(password);
 $.ajax({
        type: "POST",
        url: "edit_account.php",
        data: { input:input , username:username , password:password, column:column }

    });
window.alert("Account has been edited");
window.location = 'manage_account.php';
}
</script>

我有这个功能,它既包含先前查询过的帐户的用户名和密码,又获取用户希望将所选列更改为的内容,依此类推。我将这些变量发送到edit_account.php来执行UPDATE查询。据我所知,当我为所有4个变量执行window.alert()时,所有变量都是正确的。

<?php

    require ('connect.php');

    $input = $_POST['input'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $column = $_POST['column'];


    $link = my_connect(); 

    $editAccount = $link->query("update AdvancedUser set '".$column."' = '".$input."' where UserName = '".$username."' and UserPassword = '".$password."'");
    //$deleteAccount = $link->query("delete from AdvancedUser where UserName = '".$username."' AND UserPassword = '".$password."'");

    $editAccount->close();
    $link->close();


?>

这是edit_account.php,其中我尝试使用mysql UPDATE进行更改但是使用此代码它什么都不做。当我尝试输入删除查询时(只是为了测试我的连接是否工作)它工作得很好。有任何想法吗?再次感谢

3 个答案:

答案 0 :(得分:1)

变化:

$editAccount = $link->query("update AdvancedUser set '".$column."' = '".$input."' where UserName = '".$username."' and UserPassword = '".$password."'");

为:

$editAccount = $link->query("update AdvancedUser set $column = '".$input."' where UserName = '".$username."' and UserPassword = '".$password."'");
                                                    ^_______^--without quotes

答案 1 :(得分:1)

试试这个

<?php
        require ('connect.php');

        $input = $_POST['input'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $column = $_POST['column'];

        $link = my_connect(); 

        $editAccount = $link->query("update AdvancedUser set $column = '".$input."' where 
        UserName = '".$username."' and UserPassword = '".$password."'");
        //$deleteAccount = $link->query("delete from AdvancedUser where UserName = 
        '".$username."' AND UserPassword = '".$password."'");

        $editAccount->close();
        $link->close();
    ?>

答案 2 :(得分:1)

删除$ column周围的引号。

而不是:

$editAccount = $link->query("update AdvancedUser set '".$column."' = '".$input."' where UserName = '".$username."' and UserPassword = '".$password."'");

试试这个:

$editAccount = $link->query("update AdvancedUser set ".$column." = '".$input."' where UserName = '".$username."' and UserPassword = '".$password."'");

当您回显此查询时,它看起来像:

update AdvancedUser set ColumnName = 'ColumnValue' where UserName = 'username' and UserPassword = 'password'