PHP表单 - 一键更新许多行

时间:2014-07-29 18:40:32

标签: php forms sql-update

如何只需点击一下即可更新所有显示的表单行(无整个表格)?

通过此代码只更新一行表单。

    if(isset($_POST['update'])){

        $UpraveneQuery = "UPDATE uctovnictvo SET meno='$_POST[meno]', datum='$_POST[datum]', obchod='$_POST[obchod]', druh='$_POST[druh]', cena='$_POST[cena]', Poznamka='$_POST[poznamka]' WHERE  uct_id='$_POST[hidden]' ";   //--------------
        mysql_query($UpraveneQuery, $con);
    };

以下是我使用的数据

    $sql = "SELECT * FROM uctovnictvo WHERE meno IN ('" . implode('\', \'', $option_meno) . "') AND obchod IN ('" . implode('\', \'', $option_obchod) . "') AND druh IN ('" . implode('\', \'', $option_druh) . "') AND  datum BETWEEN '$date_start' AND '$date_end'  ORDER BY $order";                 
    $mojeData = mysql_query($sql,$con) or die($sql."<br/><br/> Chyba 1 je:".mysql_error());//run query a ulozit to premennej

在这里你可以看到你每行只能进行一次更新的形式(ulozit zmenu)。

    while($zaznam = mysql_fetch_array($mojeData)){
    echo "<form action=index-4.php method=post>";
    echo "<tr>";
    echo "<td>" . "<input type=text name=meno value=" . $zaznam['meno'] . ">" . "</td>";
    echo "<td>" . "<input type=date name=datum value=" . $zaznam['datum'] . ">" . "</td>";
    echo "<td>" . "<input type=text name=obchod value=" . $zaznam['obchod'] . ">" . "</td>";
    echo "<td>" . "<input type=text name=druh value=" . $zaznam['druh'] . ">" . "</td>";
    echo "<td>" . "<input step=any type=number name=cena value=" . $zaznam['cena'] . ">" . "</td>";
    echo "<td>" . "<input type=text name=poznamka value=" . $zaznam['poznamka'] . ">" . "</td>";
    echo "<td>" . "<input type=hidden name=hidden value=" . $zaznam['uct_id'] . ">" . "</td>";
    echo "<td>" . "<input type=text name=hidden value=" . $zaznam['uct_id'] . ">" . "</td>";
    echo "<td>" . "<input type=submit name=update value='ulozit zmenu'" . ">" . "</td>";
    echo "<td>" . "<input type=submit name=zmazat value=zmazat" . ">" . "</td>";
    echo "</tr>";
    echo "</form>";
    }

谢谢

2 个答案:

答案 0 :(得分:0)

首先,我建议您使用PDO预处理语句而不是mysql_queries http://php.net/manual/en/pdo.prepared-statements.php

现在回答您的初始问题,您必须更改输入的名称,以便将它们恢复为数组。 这里有很好的解释:Multiple inputs with same name through POST in php

这是更改输入名称后代码的样子:

while($zaznam = mysql_fetch_array($mojeData))
{
    echo "<form action=index-4.php method=post>";
    echo "<tr>";
    echo "<td>" . "<input type=text name='meno[]' value='" . $zaznam['meno'] . "'>" . "</td>";
    echo "<td>" . "<input type=date name='datum[]' value='" . $zaznam['datum'] . "'>" . "</td>";
    echo "<td>" . "<input type=text name='obchod[]' value='" . $zaznam['obchod'] . "'>" . "</td>";
    echo "<td>" . "<input type=text name='druh[]' value='" . $zaznam['druh'] . "'>" . "</td>";
    echo "<td>" . "<input step=any type='number[]' name=cena value='" . $zaznam['cena'] . "'>" . "</td>";
    echo "<td>" . "<input type=text name='poznamka[]' value='" . $zaznam['poznamka'] . "'>" . "</td>";
    echo "<td>" . "<input type=hidden name='hidden[]' value='" . $zaznam['uct_id'] . "'>" . "</td>";
    echo "<td>" . "<input type=text name='hidden[]' value='" . $zaznam['uct_id'] . "'>" . "</td>";

    echo "</tr>";
}
echo "<input type=submit name="update" value='update'" . ">;
echo "</form>";

然后恢复您必须编写类似

的输入
$i=0;
while(isset($_POST['meno'][$i]))
{
    $meno = $_POST['meno'][$i];
    $datum = $_POST['datum'][$i];
    $obchod = $_POST['obchod'][$i];
    // etc....

    /*
    write your magic here in order to save the entries in the database
    */
}

答案 1 :(得分:0)

如果这是第二部分看起来如何,那么它不起作用:

    if(isset($_POST['update'])){
    $i=0;
    while(isset($_POST['meno'][$i]))
    {
        $meno = $_POST['meno'][$i];
        $datum = $_POST['datum'][$i];
        $druh = $_POST['druh'][$i];
        $number = $_POST['number'][$i];
        $poznamka = $_POST['poznamka'][$i];
        /*
        $UpraveneQuery = "UPDATE uctovnictvo SET meno='$_POST[meno]', datum='$_POST[datum]', obchod='$_POST[obchod]', druh='$_POST[druh]', cena='$_POST[cena]', Poznamka='$_POST[poznamka]' WHERE  uct_id='$_POST[hidden]' ";   //--------------
        mysql_query($UpraveneQuery, $con);}
        */
        $UpraveneQuery = "UPDATE uctovnictvo SET meno='$meno', datum='$datum', obchod='$obchod', druh='$druh', cena='$cena', Poznamka='$poznamka' WHERE  uct_id='$_POST[hidden]' ";   //--------------
        mysql_query($UpraveneQuery, $con);} 
    };