php - MySQLi 程序查询,出了什么问题?

标签 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");

那是我的数据库设置。我是否将变量放在引号中?

最佳答案

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

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

您在 $databse = 'testdb'; 中有错字。将其重命名为 $database

关于php - MySQLi 程序查询,出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14062834/

相关文章:

php - 延迟页面滚动不起作用,无法找出代码中的问题

mysql - 如何在报告中获得条件输出?

mysql - 无法通过套接字 Homebrew 程序连接到本地 MySQL 服务器

php - SELECT 1 FROM 和 if 语句

php - 在 ubuntu 中启用 php-curl 功能

php - 如何使用 Server Sent Events 使用 AJAX 和 PHP 从 OpenAI 的 API 流式传输数据?

php - 如何计算 MySQL 查询的大小?

php - 由于未安装 MySQLnd,MySQLi 准备好的语句显示错误

javascript - 如何删除一个json元素?

mysql - 创建 View 和性能