-1

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

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

警告: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>
4

3 回答 3

2

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

$id = mysql_insert_id() or die(mysql_error());
于 2013-04-18T04:33:48.053 回答
1

改变这个

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

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

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

于 2013-04-18T04:33:20.867 回答
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';
         }

    }

    }
    ?>
于 2013-04-18T04:34:33.560 回答