将多个记录插入数据库

时间:2015-02-07 02:00:18

标签: php mysqli

我有一个表单,允许用户将多个数据插入数据库。我一直试图插入它们但无济于事。

以下是表格

<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' )";

1 个答案:

答案 0 :(得分: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表单中获取一行。我认为不需要foreach。在插入数据库之前回显每个变量只是为了检查您是否获得了corrct值。 请检查此链接。 Link