-2

我尝试singer id在另一个表中插入数据后生成,当我使用准备语句时出现问题:

警告:mysqli_insert_id() 期望参数 1 为 mysqli,对象 > 在 /Applications/XAMPP/xamppfiles/htdocs/admin/singer.php 中给出 > 第 22 行警告:mysqli::prepare() [mysqli.prepare]: couldn' t fetch > mysqli in /Applications/XAMPP/xamppfiles/htdocs/admin/singer.php on > line 24

<?php
include('../db_inc.php');    
$singer_name =$_POST['singer_name'];
$singer_gender=$_POST['singer_gender'];
$singer_des=$_POST['singer_description'];
$singer_genre=$_POST['genre_list'];

        if($stmt=$connection->prepare("INSERT INTO singers(singer_name,singer_gender,singer_description) VALUES (?,?,?)")){
            $stmt->bind_param('sss',$singer_name,$singer_gender,$singer_des);
            $result1=$stmt -> execute();
            $singer_id=mysqli_insert_id($stmt);
            $stmt->close();
            }

  if($stmt2=$connection->prepare("INSERT INTO genre_singer(f_singer_id,f_genre_id) VALUES (?,?)")){
      $stmt2->bind_param('fs',$singer_id,$singer_genre);
            $result2=$stmt2 -> execute();
            $stmt2->close();
            $connection->close();
      }
if($result1 & $result2){
    echo "insert successfully"; 
    };
?>
4

2 回答 2

7

改变:

$singer_id=mysqli_insert_id($stmt);

至:

$singer_id=mysqli_insert_id($connection);
于 2013-11-13T15:08:56.870 回答
7

你没有mysqli_insert_id正确使用。它应该是:

mysqli_insert_id($connection);

或者更好:

$connection->insert_id

请参阅文档: http: //php.net/mysqli_insert_id

于 2013-11-13T15:09:03.993 回答