Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回上次插入的id在php中不起作用_Php_Mysql_Mysqli - Fatal编程技术网

返回上次插入的id在php中不起作用

返回上次插入的id在php中不起作用,php,mysql,mysqli,Php,Mysql,Mysqli,我的post_id是一个自动递增字段。它给了我一个“0”,而不是上次插入的id。如果您的查询确实有效,请验证。。。您可能混合了过时的mysql连接器和mysqli连接器。是来自mysql连接器的函数。改用 试一试 插入新行时不要包含post\u id,因为它是自动递增的。数据库 if($result) { echo $db->insert_id; }else{ echo "Something is wrong. Insert failed.."; } 为什么要在post

我的post_id是一个自动递增字段。它给了我一个“0”,而不是上次插入的id。

如果您的查询确实有效,请验证。。。您可能混合了过时的mysql连接器和mysqli连接器。是来自mysql连接器的函数。改用

试一试

插入新行时不要包含post\u id,因为它是自动递增的。

数据库

if($result) { 
    echo $db->insert_id;
}else{
    echo "Something is wrong. Insert failed..";
}

为什么要在post_id中插入空白值?也许删除它你是什么MySQL API?您在第一行中使用了一个OO接口,它建议使用mysqli或PDO。以后必须使用相应的函数,而不是mysql\u XXX。@如果删除它,gwillie将无法工作。post_id是AI
if($result) { 
    echo $db->insert_id;
}else{
    echo "Something is wrong. Insert failed..";
}
    CREATE TABLE MyGuests (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP
    )
      last inserted ID:

-------------------------------------------------------------------
  1.   Example (MySQLi Object-oriented)

-------------------------------------------------------------------
    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";

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

    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";

    if ($conn->query($sql) === TRUE) {
        $last_id = $conn->insert_id;
        echo "New record created successfully. Last inserted ID is: " . $last_id;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
    ?>

------------------------------------------------------------------- 
   2.  Example (MySQLi Procedural)

-------------------------------------------------------------------
    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', 'john@example.com')";

    if (mysqli_query($conn, $sql)) {
        $last_id = mysqli_insert_id($conn);
        echo "New record created successfully. Last inserted ID is: " . $last_id;
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

    mysqli_close($conn);
    ?>

------------------------------------------------------------------- 
  3.  Example (PDO)

-------------------------------------------------------------------
    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDBPDO";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "INSERT INTO MyGuests (firstname, lastname, email)
        VALUES ('John', 'Doe', 'john@example.com')";
        // use exec() because no results are returned
        $conn->exec($sql);
        $last_id = $conn->lastInsertId();
        echo "New record created successfully. Last inserted ID is: " . $last_id;
        }
    catch(PDOException $e)
        {
        echo $sql . "<br>" . $e->getMessage();
        }

    $conn = null;
    ?>