使用select PHP和MySQL更新多行

时间:2013-04-11 02:58:13

标签: php mysql select

下面是我试图更改多行中一列值的代码,但我有点卡住了。

文件1

<form name="update-location" method="post" action="recibos_location_update.php">

<div align="right"><button type="submit">Actualizar Estado</button></div>
<hr />

<ul>
    <?php
        $get_logs = mysql_query("SELECT * FROM recibos ORDER BY id DESC");
        while ($logs = mysql_fetch_array($get_logs)){

        $serial = $logs['serial'];
        $check = $logs['location'];
        $problem = $logs['problem'];
    ?>

    <li class="<?php 
        if ($check == $location1){echo 'recibos-container-shop-li';}
        if ($check == $location2){echo 'recibos-container-haji-li';}
        if ($check == $location3){echo 'recibos-container-return-li';} ?>">

        <?php echo $serial . " | " . $logs['model'] . " | " . $problem; ?>

        <div align="right">
            <?php
                if ($check != 'Returned'){
            ?>
                <input type="hidden"  name="serial[]" value="<?php echo $serial; ?>" />
                <select name="location">
                    <option <?php if ($check == $location1){echo 'selected';} ?> value="<?php echo $location1; ?>" style="background: #fff8bf;">Mobils Calfont</option>
                    <option <?php if ($check == $location2){echo 'selected';} ?> value="<?php echo $location2; ?>" style="background: #ffc1bf;">Haji Jorda</option>
                    <option <?php if ($check == $location3){echo 'selected';} ?> value="<?php echo $location3; ?>"style="background: #ecffbf;">Devuelto</option>
                </select>
            <?php
            }
            else {
                echo "<i><font color='#5e8029'>Devuelto</font></i>";
            }
            ?>
        </div>
    </li>
    <?php } ?>
</ul>

文件2

<?php
include "connect.php";
include "links.php";


foreach($_POST["serial"] as $serial){

    $location = $_POST['location'];
    echo $location . " " . $serial . "<br>";
}
mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");

//header("Location: " . $_SERVER['HTTP_REFERER']);

&GT;

我得到的输出是:

Haji 900005
Haji 900004
Haji 900002

但是对于所有3个输出行,它应该给出不同的名称,因为3个选择框中的名称是不同的。

我需要的是流程表格更新每一行,选择框选项中包含什么值。

2 个答案:

答案 0 :(得分:0)

foreach($_POST["serial"] as $serial){

    $location = $_POST['location'];
    echo $location . " " . $serial . "<br>";
    mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");//update
}
//    mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");

答案 1 :(得分:0)

HTML

<select name="location[]">

PHP

foreach($_POST["serial"] as $k=> $serial){

    $location = $_POST['location'][$k];
    mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");
}