Mysql_insert_id()无法从第一个查询获取值,并在第二个查询中使用它..两个查询用于插入,但在两个不同的表

问题描述:

我想使用MySQL_insert_id从第一个查询中获得一个值,它具有一个自动-Increment与PropertyImageID的名字是最后一列在我的性能表,我也有我的第二个表是propertyimages第一列,并用它在第二次查询中插入值PropertyImageID相同第二张桌子。Mysql_insert_id()无法从第一个查询获取值,并在第二个查询中使用它..两个查询用于插入,但在两个不同的表

但它给这个错误信息:

警告:mysql_insert_id()预计参数1是 资源,布尔在on..line 31

这里给出的是我的代码(我没有写我的查询中的自动递增列):

<?php 
    require_once('db.php'); 
    @$PropertyName=$_POST['pname']; 
    @$PropertyStatus=$_POST['pstatus']; 
    @$PropertyID=$_POST['propertyid']; 
    if(isset($_FILES['file_upload'])) 
    { 

    $propertyquery="INSERT INTO properties(PropertyID, PropertyName, PropertyStatus) 
    VALUES('$PropertyID', '$PropertyName', '$PropertyStatus')"; 
    $propertyqueryrun=mysql_query($propertyquery) or die(mysql_error()); 

     if($propertyqueryrun) 
     { 
     echo '<br><br> The Property Information Insertion was Successfully'; 
     } 
       else 
     { 
        echo '<br><br> Property Insertion Failed'; 
     } 

    $shuff=str_shuffle("ABD6565LSLFKDSAJFD"); 

    mkdir("upload/$shuff"); 

    $files=$_FILES['file_upload']; 


    for($x = 0; $x < count($files['name']); $x++) 
    { 
     $name=$files['name'][$x]; 

     $tmp_name=$files['tmp_name'][$x]; 

     if(move_uploaded_file($tmp_name, "upload/$shuff/".$name)) 
     { 
     $result=mysql_query($propertyquery)------>First query; 

     $id=mysql_insert_id($result) or die(mysql_error()); 

     $imagequery="INSERT INTO propertyimages(PropertyImageID, ImageName, 

    ImagePath) VALUES('**$id**', '$name', 'upload/$name')"; 

      $imagequeryrun=mysql_query($imagequery); 

      echo 'Image '. $name .' Uploaded Successfully <br>'; 
     } 
     else 
     { 
      echo 'uploading images failed failed'; 
     } 

    } 

    } 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Untitled Document</title> 
</head> 
<body> 
    <form action="" method="POST" enctype="multipart/form-data"> 
    <label>PropertyName:<br /></label> 
    <input type="text" name="pname" /><br /> 
    <label>PropertyStatus:<br /></label> 
    <input type="text" name="pstatus" /><br /> 
    <label>PropertyID:<br /></label> 
    <input type="text" name="propertyid" /><br /> 
    <input type="file" name="file_upload[]" multiple="multiple"/size="7"><br /><br /> 
    <input type="submit" value="Submit Form" /> 
    </form> 
    <a href="home.php">Display Images</a> 
</body> 
</html> 
+1

哪里是你mysql_insert_id()我不能找到 – 2013-04-18 04:32:00

+0

这意味着,'mysql_query'失败。 – 2013-04-18 04:33:38

+1

@YadavChetan在浏览器中使用ctrl + f'$ id = mysql_insert_id($ propertyqueryrun)' – 2013-04-18 04:33:47

你应该只插入查询后声明,并指定错误的参数给它

<?php 
    require_once('db.php'); 
    @$PropertyName=$_POST['pname']; 
    @$PropertyStatus=$_POST['pstatus']; 
    @$PropertyID=$_POST['propertyid']; 
    if(isset($_FILES['file_upload'])) 
    { 

    $propertyquery="INSERT INTO properties(PropertyID,PropertyName,PropertyStatus) 
    VALUES('$PropertyID', '$PropertyName', '$PropertyStatus')"; 
    $propertyqueryrun=mysql_query($propertyquery) or die(mysql_error()); 
    $insert_id=mysql_insert_id(); 
     if($propertyqueryrun) 
     { 
     echo '<br><br> The Property Information Insertion was Successfully'; 
     } 
       else 
     { 
        echo '<br><br> Property Insertion Failed'; 
     } 

    $shuff=str_shuffle("ABD6565LSLFKDSAJFD"); 

    mkdir("upload/$shuff"); 

    $files=$_FILES['file_upload']; 


    for($x = 0; $x < count($files['name']); $x++) 
    { 
     $name=$files['name'][$x]; 

     $tmp_name=$files['tmp_name'][$x]; 

     if(move_uploaded_file($tmp_name, "upload/$shuff/".$name)) 
     { 
     $id= $insert_id; 

     $imagequery="INSERT INTO propertyimages(PropertyImageID,ImageName, 

    ImagePath) VALUES('$id','$name','$name')"; 

      $imagequeryrun=mysql_query($imagequery); 

      echo 'Image '. $name .' Uploaded Successfully <br>'; 
     } 
     else 
     { 
      echo 'uploading images failed failed'; 
     } 

    } 

    } 
    ?> 
+0

我编辑你写的代码,但我得到'列计数不匹配在第1行值计数'; – user2279037 2013-04-18 04:41:14

+0

然后在插入查询错误 – 2013-04-18 04:41:47

+0

现在尝试我编辑了查询 – 2013-04-18 04:44:19

改变这个

$id=mysql_insert_id($propertyqueryrun) or die(mysql_error()); 

$id=mysql_insert_id() or die(mysql_error()); 

无需通过查询参数,只需要可选链路参数

无需通过论点mysql_insert_id()。您可以使用last inserted id而不通过$propertyqueryrun值。

$id = mysql_insert_id() or die(mysql_error());