MySQLi程序查询,有什么不对?

时间:2012-12-27 23:52:10

标签: php mysql mysqli

//Successful connection to $db

function insert_users($db, $username, $password, $email)
{
    echo "FUNCTION CALLED"; //This is outputted successfully

    $query = "INSERT INTO `users` (`id`, `username`, `password`, `email`) VALUES ('', ?, ?, ?)";
    $stmt = mysqli_stmt_init($db);

    if(mysqli_stmt_prepare($stmt, $query))
    {
        echo "QUERY PREPARED"; // rest of code was snipped (will put up upon request)
    } else {
        echo "QUERY DENIED"; //This is outputted successfully
    }
}


//The $user $pass and $mail are defined and then the function is called

insert_users($db, $user, $pass, $mail);

数据库结构:

testdb (database)
    -> users (table)
        -> id            //Primary key, unique key
        -> username      //unique key
        -> password
        -> email

为什么查询不会通过它的操作? 它回应“查询被拒绝”而不是“查询准备”。我相信查询无效。如果需要更多信息,我们很乐意编辑此问题

修改

我添加了

mysqli_stmt_error($stmt);之后

echo "QUERY DENIED"; 什么都没发生......

编辑2

$server = 'localhost';
$user = 'root';
$password = '';
$databse = 'testdb';

$db = @mysqli_connect($server, $user, $password, $database) or die("Could not connect to Database server. Please inform an administrator");

这是我的数据库设置。我把变量放在引号中吗?

2 个答案:

答案 0 :(得分:2)

如果id是auto_increment列,您可以将其从插入中删除

INSERT INTO `users` (`username`, `password`, `email`) VALUES (?, ?, ?)

$databse = 'testdb';中有拼写错误。将其重命名为$database

答案 1 :(得分:2)

我想这是因为你总是试图输入一个空字符串作为主键(id)。如果该字段是自动递增字段,则您甚至都不应该尝试为其设置值。

$query = "INSERT INTO `users` (`username`, `password`, `email`) VALUES (?, ?, ?)";