2013-04-18 146 views
-1

我想使用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

回答

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

    } 

    } 
    ?> 
+0

我編輯你寫的代碼,但我得到'列計數不匹配在第1行值計數'; – user2279037 2013-04-18 04:41:14

+0

然後在插入查詢錯誤 – 2013-04-18 04:41:47

+0

現在嘗試我編輯了查詢 – 2013-04-18 04:44:19

1

改變這個

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

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

無需通過查詢參數,只需要可選鏈路參數

2

無需通過論點mysql_insert_id()。您可以使用last inserted id而不通過$propertyqueryrun值。

$id = mysql_insert_id() or die(mysql_error()); 
相關問題