返回最后插入的id在php中不起作用

时间:2014-02-10 05:09:10

标签: php mysql mysqli

$result = $db->query("INSERT INTO post_items(`post_id`,`content`,`date`,`user_id`,`category_id`) 
    VALUES ('', '".$content."', '".$date."', '".$user_id."', '".$category_id."')");

if($result) { 
    echo mysql_insert_id();
}else{
    echo "Something is wrong. Insert failed..";
}

我的post_id是一个自动增量字段。它给了我一个'0'而不是最后插入的id。

3 个答案:

答案 0 :(得分:0)

如果您的查询确实有效(请验证...),您可能正在混合过时的mysql连接器和mysqli连接器。 mysql_insert_id()是mysql连接器的函数。请改用mysqli_insert_id()

尝试

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

答案 1 :(得分:0)

数据库

    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;
    ?> 

答案 2 :(得分:-1)

在插入新行时不要包含post_id,因为它是auto_increment