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

时间:2013-04-18 04:29:28

标签: php mysql

我想使用 MySQL_insert_id 从第一个查询中获取一个值,该值具有名称为 PropertyImageID 的自动增量,这是我属性表,我的 PropertyImageID 在我的第二个表中是 propertyimages 第一列,并在第二个查询中使用它来插入第二个表中的值。

但是它给出了这个错误信息:

  

警告:mysql_insert_id()期望参数1为   resource,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>

3 个答案:

答案 0 :(得分:2)

无需在mysql_insert_id()中传递参数。您可以在不传递last inserted id值的情况下使用$propertyqueryrun

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

答案 1 :(得分:1)

更改此

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

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

无需传递查询参数,只需要可选的链接参数

答案 2 :(得分:0)

你应该在插入查询之后声明它并且你为它指定了错误的参数

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

    }

    }
    ?>