如何在mysql中上传带有链接的多个图像

时间:2014-03-19 14:07:29

标签: php html mysql image

目前我的代码可以完美地用于一次图片上传。但我想一次上传多个图像,图像标题,图像描述上传图像组,因为这些图像将用于照片幻灯片放映(请参阅下面的图片)

我的当前代码如下 -

PHP代码 -

    $bsq->connect_db();
    $fileName = $_FILES["image"]["name"]; 
    $fileNameNew = preg_replace('/\s+/', '_', $fileName);
    $fileTmpLoc = $_FILES["image"]["tmp_name"];
    // Path and file name
    $pathAndName = "uploads_admin/".$fileNameNew;
    // Run the move_uploaded_file() function here
    $moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
    // Evaluate the value returned from the function if needed


    if($_POST['action']=="add"){
           $all_columns[]="image_subject";
           $all_columns[]="image_name"; 
           $all_columns[]="clinic"; 
           $all_columns[]="image_link";



//Get All values to insert in to table      
           $all_values[]=addslashes($_POST["image_subject"]);
           $all_values[]=addslashes($_POST["image_name"]);  
           $all_values[]=addslashes($_POST["clinic"]);
           $all_values[]=addslashes($pathAndName );


//=====================

$qry=$bsq->webdreaminsert("sa_galleryuploads_by_admin",$all_columns,$all_values,'');
echo mysql_error();

header("location:upload_file_for_downloading_list.php");
    ///////////////////////////////////////////////////
    }   

和HTML表格上传图片如下 -

      <form action="" method="post" enctype="multipart/form-data" name="addtwebinar1" id="addtwebinar1" onsubmit="javascript:return validateimage1();" >
      <input type="hidden" value="add" name="action" />

      <table width="90%" align="center" border="0" cellpadding="0" cellspacing="0" class="ListTable1">
      <tr class="HeadBr">
      <td colspan="4"><div align="center"><strong>Add Images For Photo Gallery</strong></div></td>
      </tr>

      <tr >
      <td>Image Title*</td>
      <td><input name="image_name" id="image_name" type="text" size="40" value="" /></td>
      </tr>
      <tr>   
      <td>Image Description In Short*</td>
      <td><input name="image_subject" id="image_subject" type="text" size="40" value="" /></td>
      </tr>
      <tr >
      <td>Clinic Name*</td>
      <td>
           <select name="clinic" id="message" >
        <option value="">Select Clinic</option>
        <option value="arogya">1. Arogyawardhini Ayurved Clinic</option>
        <option value="smruti">2. Smruti Ayurved Clinic</option>
        <option value="tarpan">3. Tarpan Ayurved Clinic</option>
        <option value="vishwa">4. Vishwawardhini Ayurved Clinic</option>
        </select>
              </td>
              </tr>                
              <tr >
                <td>Your Image For Upload* </td>
                <td><label for="image">File To Upload: </label><br>
                <input type="file" size="40" name="image"  id="image" /><br />
               </td>
               </tr>    

               <tr>
               <td></td>
               <td><button >Upload</button></td>
              </tr>
            </table>
            </form>

我的活动图片上传表格的当前外观 - enter image description here 我想要下面(以图形方式创建) enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用for循环。

对于表单,请执行以下操作:

for($i = 1; $i <= 4; $i++) {
        echo "<input type=\"file\" size=\"40\" name=\"image{$i}\"  id=\"image{$i}\" /><br />";
}

对于处理,只需将所有内容放入for循环中。

   for($i = 1; $i <= 4; $i++) { 
   $fileName = $_FILES["image".$i]["name"]; 
   $fileNameNew = preg_replace('/\s+/', '_', $fileName); 
   $fileTmpLoc = $_FILES["image".$i]["tmp_name"]; 
    // Path and file name 
   $pathAndName = "uploads_admin/".$fileNameNew; 
   // Run the move_uploaded_file() function here 
   $moveResult = move_uploaded_file($fileTmpLoc, $pathAndName); 
  // Evaluate the value returned from the function if needed 


  if($_POST['action']=="add"){ 

  $image_name = mysql_real_escape_string($_POST['image_name']); 
  $image_subject = mysql_real_escape_string($_POST['image_subject']); 
  $clinic = mysql_real_escape_string($_POST['clinic']); 
  $image_link = "http://".$_SERVER['HTTP_HOST'].rtrim(dirname($_SERVER['REQUEST_URI']), "\\/")."/".$pathAndName; 

  //===================== 

  mysql_query("INSERT INTO `sa_galleryuploads_by_admin` VALUES ('', '{$image_name}',  '{$image_subject}', '{$clinic}', '{$image_link}' )") or die(mysql_error()); 

  if(!mysql_error()) { 
     echo "success"; 
   } 

   } 

您可以编辑循环前进的数字,以匹配您要显示的字段/图像的数量。祝你好运!

编辑:您还需要清理验证输入。至少,使用mysql_real_escape_string()。

此外,不推荐使用mysql_ *函数。你应该切换到使用mysqli或pdo。我建议启动mysqli,因为它还提供了一种程序方法,而pdo完全是面向对象的。