Pdo查询只插入一个pic的名称

时间:2013-02-09 10:50:02

标签: php mysql

此代码可以将多个图像上传到文件夹,但是当它将图像信息插入数据库时​​,它只会插入1个名称。如有任何帮助,我将不胜感激

#target folder
$target = 'image_uploads/';
  if(isset($_FILES['image_name'])===true){
      $files = $_FILES['image_name'];
      for($x = 0 ; $x < count($files['name']); $x++){
          $name = $files['name'][$x] ;
          $temp_name = $files['tmp_name'][$x];
          $move = move_uploaded_file($temp_name,$target.$name);
      }
  }
?>
<?php
try{
    $con = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf-8','root','');
    $query="INSERT INTO tish_images(image_name)
VALUES(:image_name)";
    $insert = $con->prepare($query);
    $insert->execute(array(
                          ':image_name'=>$name));
}catch(PDOException $e){
    echo $e->getMessage();
}

3 个答案:

答案 0 :(得分:1)

我不确定但您需要在for循环中定义插入查询。

答案 1 :(得分:1)

您的INSERT&amp; execute array语句超出for周期。

答案 2 :(得分:0)

@Devang Rathod和@revoua。我非常感谢你的帮助,这是你们帮助之后的代码 它以我想要的方式工作

<?php 
#target folder 
$target = 'image_uploads/';
  if(isset($_FILES['image_name'])===true){
  $files = $_FILES['image_name'];
  for($x = 0 ; $x < count($files['name']); $x++){
  $name = $files['name'][$x] ;
  $temp_name = $files['tmp_name'][$x];  
  $move = move_uploaded_file($temp_name,$target.$name); 
?>
<?php
try{
$con = new PDO('mysql:host=localhost;dbname=tish_database;charset=utf-8','root','');
$query="INSERT INTO tish_images(image_name)
VALUES(:image_name)";
$insert = $con->prepare($query);
$insert->execute(array(
':image_name'=>$name));
}catch(PDOException $e){
echo $e->getMessage();
}
 } 
}
?>