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


Mysql_insert_id() unable to get value from first query and use it in second query..both queries used for insertion but in two different tables

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

但它给出了这样的错误信息:

警告:mysql_insert_id((要求参数1为资源,第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>

无需在mysql_insert_id()中传递参数。您可以使用获得last inserted id,而无需传递$propertyqueryrun值。

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

更改此

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

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

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

您应该在插入查询后立即声明它,并且您为它指定了错误的参数

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