insert_id 的值不断返回 0 [重复]

Value of insert_id keeps returning 0 [duplicate]

提问人:Cedric Woo 提问时间:2/11/2021 最后编辑:DharmanCedric Woo 更新时间:2/11/2021 访问量:189

问:

我正在尝试创建一个注册表单,我希望让用户知道他的 ID 的值。我能够成功注册用户,但即使数据库中有多行,insert_id也一直返回 0。

数据库

public function database()
{
    $this->servername = "localhost";
    $this->username = "root";
    $this->password = "";
    $this->dbname = "dba3";

    $conn = new mysqli($this->servername, $this->username,$this->password,$this->dbname);
    
    if($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $this->tablename = "User";
    $checktable = $conn->query("SHOW TABLES LIKE '$this->tablename'");
    $table_exists = $checktable->num_rows >= 1;

    if(!$table_exists) {
        $sql = "CREATE TABLE $this->tablename( 
                id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
                firstname VARCHAR(30) NOT NULL,
                lastname VARCHAR(30) NOT NULL,
                phone VARCHAR(20) NOT NULL,
                email VARCHAR(50) NOT NULL,
                type VARCHAR(20) NOT NULL)";

        if($conn->query($sql)===TRUE){
            echo "Table sucessfully created";
        } else {
            echo "Error creating table: " . $conn->error;
        }
    }
    return $conn;
    $conn->close();
}

PHP代码

public function checkEmpty($fname, $lname, $num, $email, $gettype)
{
    $this->fname = $fname;
    $this->lname = $lname;
    $this->num = $num;
    $this->email = $email;
    $this->gettype = $gettype;
    $sql = "INSERT INTO User
                    (firstname, lastname, phone, email, type)
            VALUES ('$this->fname', '$this->lname', 
                    '$this->num', '$this->email', '$this->gettype')";

    if($this->database()->query($sql)!==FALSE) {
        $last_id = $this->database()->insert_id;
        echo "<h3>Congratulations ". $this->fname. " " .$this->lname. ", account successfully registered</h3>";

        echo "Your ID Number is " . $last_id;
    }
}

An example of the output I'm getting

php mysqli mysql-insert-id 插入 id

评论

1赞 Nico Haase 2/11/2021
您尝试过什么来调试问题?另外,请注意,您的代码对 SQL 注入是广泛开放的
0赞 Nico Haase 2/11/2021
另外:在 after 调用任何内容都没有意义,因为 return 语句之后的代码不会被执行return $conn
2赞 Definitely not Rafal 2/11/2021
你打电话然后你打电话,这里发生了什么?每当调用 时,都会创建一个新实例。正因为如此,新实例不知道您之前执行了另一个查询。if($this->database()->query($sql)!==FALSE)$last_id = $this->database()->insert_id;mysqli$this->database()
0赞 Definitely not Rafal 2/11/2021
您编辑了关键细节,以了解为什么会出现此错误。
1赞 RiggsFolly 2/11/2021
@DefinitelynotRafal我把它放回了修订版

答:

3赞 Alive to die - Anant 2/11/2021 #1

基本问题

多次使用:$this->database()

它每次都会创建新的连接,这就是为什么它总是返回 0 的原因:

正确代码:

public function checkEmpty($fname, $lname, $num, $email, $gettype)
{
    $this->fname = $fname;
    $this->lname = $lname;
    $this->num = $num;
    $this->email = $email;
    $this->gettype = $gettype;
    $db = $this->database();
    $sql = "INSERT INTO User(firstname, lastname, phone, email, type) VALUES ('$this->fname', '$this->lname', '$this->num', '$this->email', '$this->gettype')";

    if($db->query($sql)!==false)
    {
        $last_id = $db->insert_id;
        echo "<h3>Congratulations ". $this->fname. " " .$this->lname. ", account successfully registered</h3>";

        echo "Your ID Number is " . $last_id;
    }
 $db->close(); //close db connection too it's very important
}

注意:

a) 您的脚本容易受到 SQL 注入攻击。您应该始终在 或 API 中使用准备好的参数化语句,而不是将用户提供的值连接到查询中。永远不要相信任何用户输入!MYSQLI_PDO

b) 之后是不必要的。$conn->close();return $conn;

c) 确保在使用结束后关闭连接(在您使用的每个功能中)

评论

0赞 Alive to die - Anant 2/11/2021
@RiggsFolly如果我没记错的话,您建议创建单独的数据库连接文件并将其包含在其他 php 文件中。
1赞 RiggsFolly 2/11/2021
好吧,这很困难,因为 OP 没有显示足够的代码。但是在我看来,存储为该类属性的 ONE Connection 会更好
0赞 Alive to die - Anant 2/11/2021
@RiggsFolly同意了