2015-02-07 112 views
-2

我有一個表單,使用戶可以將多個數據插入到數據庫中。我一直試圖插入他們,但無濟於事。將多條記錄插入到數據庫中

下面是形式

<tr> 
<td> 
    <select title = "Please choose" name="menutype[]"> 
     <option value="-1" >--Select--</option> 
      <?php 
      $query = "SELECT * FROM MenuType"; 
      $result = $mysqli->query($query); 
      if ($result==false) { 
        die(mysqli_error($mysqli)); 
      } 

      while ($menutype = mysqli_fetch_array($result)) { 
       ?> 
       <option value="<?php echo $menutype['MenuTypeID'] ?>"><?php echo $menutype['MenuTypeName'] ?></option> 
       <?php 
      } 
      ?> 
</select> 
</td> 
<td><input type="text" name="name[]" /></td> 
<td><input type="file" name="picture[]" /></td> 
<td><textarea rows="4" cols="40" name="description[]"></textarea></td> 
<td><input type="text" name="price[]" value=""/></td> 
<td> 
    <select title="Please choose" name="status[]"> 
     <option value="-1" />--Select--</option> 
     <option value="Available" />Available</option> 
     <option value="Limited" />Limited</option> 
    </select> 
</td> 
</tr> 

下面是PHP代碼

$name = $_POST['name']; 
$description = $_POST['description']; 
$price = $_POST['price']; 
$status = $_POST['status']; 
$menutypeid = $_POST['menutype']; 

if(isset($_FILES['picture'])){ 
$name_array = $_FILES['picture']['name']; 
$tmp_name_array = $_FILES['picture']['name']; 

for($i = 0; $i < count($tmp_name_array); $i++){ 
    if(move_uploaded_file($tmp_name_array[$i], "images/menu/".$name_array[$i])){ 
     echo $name_array[$i]." upload is complete<br>"; 
    } else { 
     echo "move_uploaded_file function failed for ".$name_array[$i]. "<br>"; 
    } 
} 

$imageup = $tmp_name_array; // save the whole url address of the uploaded file into variable 
foreach ($name as $value){ 
$query = "INSERT INTO menu (MenuID, MenuName, MenuPicture, MenuDescription, MenuPrice, MenuStatus, MenuTypeID) 
       VALUES ('', '$name', '$imageup', '$description', '$price', '$status', '$menutypeid')"; 

    $mysqli->query($query) or die(mysqli_error($mysqli)); 
    //header('Location:view_menu_for_manager.php');}} 
?> 

我也有麻煩上傳多個文件/圖片。幫助

編輯查詢

foreach ($name as $value){ 
$query = "INSERT INTO menu (MenuName, MenuPicture, MenuDescription, MenuPrice, MenuStatus, MenuTypeID) 
       VALUES ('$name', '$imageup', '$description', '$price', '$status', '$menutypeid')"; 
+0

插入菜單(menuname)值('$ name')像這樣,請刪除您的舊評論。 – 2015-02-07 13:42:03

回答

0

嘗試這樣

<?php  
$name = $_POST['name']; 
$description = $_POST['description']; 
$price = $_POST['price']; 
$status = $_POST['status']; 
$menutypeid = $_POST['menutype']; 

$data= array('name'=>$name,     
       'description'=>$description, 
       'price'=>$price, 
       'menutypeid'=>$status , 
       'status'=>$menutypeid); 
if(isset($_FILES['picture'])){ 
$name_array = $_FILES['picture']['name']; 
$tmp_name_array = $_FILES['picture']['name']; 

for($i = 0; $i < count($tmp_name_array); $i++){ 
    if(move_uploaded_file($tmp_name_array[$i], "images/menu/".$name_array[$i])){ 
     echo $name_array[$i]." upload is complete<br>"; 
    } else { 
     echo "move_uploaded_file function failed for ".$name_array[$i]. "<br>"; 
    } 
} 

$imageup = $tmp_name_array; // save the whole url address of the uploaded file into variable 
$i=0; 
foreach($data as $value){ 
$name=$value[$i]['name']; 
$description=$value[$i]['description']; 
$price=$value[$i]['price']; 
$status=$value[$i]['status']; 
$menutypeid=$value[$i]['menutypeid']; 
$query = "INSERT INTO menu (MenuName, MenuPicture, MenuDescription, MenuPrice, MenuStatus, MenuTypeID) 
       VALUES ('$name', '$imageup', '$description', '$price', '$status', '$menutypeid')"; 

    $mysqli->query($query) or die(mysqli_error($mysqli)); 
    //header('Location:view_menu_for_manager.php');} 
} 
?> 

刪除的foreach,因爲你是從HTML表單只得到了一排。我認爲沒有必要這樣做。在插入數據庫之前回應每個變量,只是爲了檢查您是否獲得了corrct值。 請檢查這個鏈接。 Link

+0

表格看起來像這樣 - > http://i.imgur.com/TBHg6ul.png做我必須刪除[]? – Fatimah 2015-02-07 14:20:22

+0

我得到了未定義的索引名稱,描述等(在foreach後面($ data爲$ value){ – Fatimah 2015-02-07 14:43:47

相關問題