mysqli_insert_id不会返回任何内容

时间:2014-10-25 16:18:12

标签: php

<?php  

>here is database connection code    

    $connection = mysqli_connect($server_name,$user_name,$password,$database_name);  

>the post variable comes from another page    

    $first_name = $_POST['first_name'];  

    $last_name = $_POST['last_name'];  

    $birth_date = $_POST['birth_date'];  

    if($connection){  

         $insert = "insert into describee(first_name,last_name,birth_date) 
                    values ('$first_name','$last_name','$birth_date')";  

         $execute_insert = mysqli_query($connection,$insert) or die("insert query error");  

>here i want to select the lase inserted row from database to show it in textarea tag       

    if( !empty($first_name) && !empty($last_name) && !empty($birth_date) ){  

        $last_id = mysqli_insert_id();  

        $select = "select * from describee where id = $last_id";  

        $execute_select = mysqli_query($connection,$select) or die("select query error");  

>$last_id doesn't  hold any data so it return error in the select query.   

?>

1 个答案:

答案 0 :(得分:2)

您错过了与MySQL的连接,应将其作为参数传递给mysqli_insert_id()

$last_id = mysqli_insert_id($connection);