2017-04-16 64 views
0

我将输入表单插入到两个不同的表中。第一个表是用于信息的,而另一个表是用于信息的图像。php mysql多次上传最后插入id for循环

当我提交信息时,我想要在单独的表格的第一个表格的每一行中上传多个图像,并且还要将last_insert_id插入到每个图像到第二个表格中。

table1 
location ID | address | contact information 

table2 
pictureID | location ID | filepath 

使用以下代码,它只会插入一个图像,但会将所有图像上传到文件夹中。

<?php 

    include_once 'dbconnect.php'; 
    mysql_query("SET NAMES UTF8"); 
    session_start(); 
    $tbl_name="location"; 
    $tbl_image="image"; 

    if(isset($_POST['btn-upload'])){ 

     $name2=$_POST['name2']; 
     $phone=$_POST['phone']; 
     $email=$_POST['email']; 
     $type=$_POST['type']; 
     $other=$_POST['other']; 
     $description=$_POST['description']; 
     $address=$_POST['address']; 
     $name=$_POST['name']; 
     $lat=$_POST['lat']; 
     $lng=$_POST['lng']; 
     $country=$_POST['country']; 
     $administrative_area_level_1=$_POST['administrative_area_level_1']; 
     $place_id=$_POST['place_id']; 
     $url=$_POST['url']; 
     $website=$_POST['website']; 




    $sql="INSERT INTO $tbl_name(name2, phone, email, type, other, description, address, name, lat, lng, country, administrative_area_level_1, place_id, url, website) VALUES ('$name2', '$phone', '$email', '$type','$other', '$description', '$address', '$name', '$lat', '$lng', '$country', '$administrative_area_level_1', '$place_id', '$url', '$website')"; 
    $result=mysql_query($sql); 

     for($i=0; $i<count($_FILES['image']['tmp_name']); $i++) 
     { 
      $file = rand(1000,100000)."-".$_FILES['image']['name'][$i]; 
      $file_loc = $_FILES['image']['tmp_name'][$i]; 
      $file_size = $_FILES['image']['size'][$i]; 
      $file_type = $_FILES['image']['type'][$i]; 
      $folder="uploads/"; 

      // new file size in KB 
      $new_size = $file_size/1024; 
      // new file size in KB 

      // make file name in lower case 
      $new_file_name = strtolower($file); 
      // make file name in lower case 

      $final_file=str_replace(' ','-',$new_file_name); 
      $res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']); 
      $userRow=mysql_fetch_array($res); 
      $userID=$userRow['userID']; 

      if(move_uploaded_file($file_loc,$folder.$final_file)) 
      { 
       $sql="INSERT INTO $tbl_image(user_ID, Location_ID, file) VALUES('$userID', LAST_INSERT_ID(), '$final_file')"; 
       mysql_query($sql); 
       ?> 
       <script> 
       alert('successfully uploaded'); 
       window.location.href='index.php?success'; 
       </script> 
       <?php 
      } 
      else 
      { 
       ?> 
       <script> 
       alert('error while uploading file'); 
       window.location.href='index.php?fail'; 
       </script> 
       <?php 
      } 

     } 

     if($result){ 
     echo "Successful"; 
     echo "<BR>"; 
     echo "<a href='index.php'>Back to main page</a>"; 
     } 

     else { 
     echo "ERROR"; 
     } 

     if($result1){ 
     echo "Upload Successful"; 
     echo "<BR>"; 
     echo "<a href='locationform.php'>Back to main page</a>"; 
     } 

     else { 
     echo "ERROR"; 
     } 


    } 




?> 

<?php 
// close connection 
mysql_close(); 
?> 
+1

作为mysql_ *是PHP 5.5弃用(请参考[PHP DOC](http://php.net/manual/en/function.mysql-connect。 PHP))你应该**真的**考虑使用[PPS:Prepared Parameterized Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。这将有助于[阻止SQL注入](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)。 **绝不**信任用户的数据!请使用'error_reporting(E_ALL); ini_set('display_errors',1);'在你的页面顶部,让我们知道是否有任何错误抛出 – OldPadawan

+0

此外,你不应该认为你的SQL调用是成功的。在每个sql查询之后添加错误处理并打印出任何错误消息 – Shadow

+0

您的代码是正确的,但在第一次成功映像表插入后,您正在重定向代码。所以你在图像表中只有一个插入。 – user3064810

回答

0
$b=$pdo->prepare(" INSERT INTO `table1` SET `address`=:address, `contact`=:contact "); 
$b->bindParam(":address",$address); 
$b->bindParam(":contact",$contact); 
$b->execute(); 
$LastId = $pdo->lastInsertId(); 

if($LastId > 0){ 
    $b=$pdo->prepare(" INSERT INTO `table2` SET `pictureID`=:pictureID, `filepath`=:filepath "); 
    $b->bindParam(":pictureID",$LastId); 
    $b->bindParam(":filepath",$filepath); 
    $b->execute(); 
} 
0

想通了最后。这是最后一个插入ID有问题。 Last_instert_ID必须在For循环之外声明。

$locationID=mysql_insert_id($conn); 

上面是在第一个插入查询的正下方添加的。

$sql="INSERT INTO $tbl_location(name2, phone, email, type, other, description, address, name, lat, lng, country, administrative_area_level_1, place_id, url, website) 
    VALUES ('$name2', '$phone', '$email', '$type','$other', '$description', '$address', '$name', '$lat', '$lng', '$country', '$administrative_area_level_1', '$place_id', '$url', '$website')"; 
    $result=mysql_query($sql); 

    $locationID=mysql_insert_id($conn); 

    for($i=0; $i<count($_FILES['image']['name']); $i++) 
    { 
     $file = rand(1000,100000)."-".$_FILES['image']['name'][$i]; 
     $file_loc = $_FILES['image']['tmp_name'][$i]; 
     $file_size = $_FILES['image']['size'][$i]; 
     $file_type = $_FILES['image']['type'][$i]; 
     $folder="uploads/"; 

     // new file size in KB 
     $new_size = $file_size/1024; 
     // new file size in KB 

     // make file name in lower case 
     $new_file_name = strtolower($file); 
     // make file name in lower case 

     $final_file=str_replace(' ','-',$new_file_name); 
     $res=mysql_query("SELECT * FROM users WHERE userId=".$_SESSION['user']); 
     $userRow=mysql_fetch_array($res); 
     $userID=$userRow['userID']; 


     if(move_uploaded_file($file_loc,$folder.$final_file)) 
     { 
      $sql2="INSERT INTO $tbl_image(user_ID, Location_ID, file) VALUES('$userID', '$locationID', '$final_file')"; 
      $result = mysql_query($sql2); 
      ?> 
      <script> 
      alert('successfully uploaded'); 
      window.location.href='index.php?success'; 
      </script> 
      <?php 
     } 
     else 
     { 
      ?> 
      <script> 
      alert('error while uploading file'); 
      window.location.href='index.php?fail'; 
      </script> 
      <?php 
     } 

    } 
-1

// how to upload multiple image in database but image save in folder with upload table with last insert id with loop // 
 

 

 
table1 - table1 
 
ID | field1 | field2 
 

 
table2 - upload 
 
pictureID | ID | uploadimage 
 

 

 
<?php 
 

 
include("config.php"); 
 

 

 
if(isset($_POST['submit'])){ 
 
$field1 =$_POST['field1']; 
 
$field2 =$_POST['field2']; 
 
\t 
 
$sql=mysql_query("INSERT INTO table1(field1,field2) VALUES ('field1','field2')"); // insert record table 1 
 

 

 

 
if($sql){ 
 
$last = mysql_insert_id(); } \t 
 
if(isset($_FILES['uploadimage']['name'])) // uploadimage file name and table column name same used here 
 
{ 
 
    $file_name_all=""; 
 
    for($i=0; $i<count($_FILES['uploadimage']['name']); $i++) 
 
    { 
 
     $tmpFilePath = $_FILES['uploadimage']['tmp_name'][$i];  
 
     if ($tmpFilePath != "") 
 
     {  
 
      $path = "uploadimages/"; // create folder name 
 
      $name = $_FILES['uploadimage']['name'][$i]; 
 
      $size = $_FILES['uploadimage']['size'][$i]; 
 

 
      list($txt, $ext) = explode(".", $name); 
 
      $file= time().substr(str_replace(" ", "_", $txt), 0); 
 
      $info = pathinfo($file); 
 
      $filename ='image'. $file.".".$ext; 
 
     } 
 
\t if(move_uploaded_file($_FILES['uploadimage']['tmp_name'][$i], $path.$filename)) 
 
      { 
 
      $sql_image=mysql_query("insert into upload(ID,uploadimage) values('".$last."','".$filename."')"); //insert record table 2(upload) 
 

 
      } 
 
\t } 
 
\t 
 
} 
 
} 
 
?> 
 
<form method="post" enctype="multipart/form-data"> 
 
<label>field - 1</label><input type="text" name="field1"> 
 
<label>field - 2</label><input type="text" name="field2"> 
 
<label>mutiple image upload</label> <input id="uploadimage" type="file" name="uploadimage[]" multiple accept="image/*" /> 
 
<button type="submit" name="submit">submit</button> 
 
    </form>

+0

你需要解释你的答案 – garfbradaz