php更新无法正常工作

时间:2016-08-29 06:04:44

标签: php mysql

我正在尝试更改表单数据并更新mysql中的数据。但是,当我尝试更新时,数据并没有改变。表单页面如下:

while ($list5 = mysql_fetch_array($result)) {
        $reqitem=$list5['id'];
    echo "<td width='34%' id='addinput'><input type='text' size='70' id='item_name$i' 
name='item_name[$i]' placeholder='{$list5['prod_description']}' 
value='{$list5['prod_description']}'></td>";
}

保存页面脚本如下:

foreach($_POST['id'] as $key=>$value)
{
    $item_name= $_POST['item_name'][$i];
   $q = "UPDATE poto_customer_items prod_description='$item_name' WHERE tender_id='$tender_id' and id='$value' ";
    mysql_query($q) or die(mysql_error());
}

我知道脚本存在问题。我是php的新手,所以无法掌握我犯错误的地方。任何人都可以帮助我。

以下是html代码:

<html>
<head>
    <title></title>
</head>
<body>
    <h3>&nbsp;</h3>
    <form action='generate_quot_cust_edititems_save_1.php?tender_id=1400692'
    class='single' id="cart" method='post' name='cart'>

        <div class="base">
            <div align="left">
             <table border="1" cellpadding="3" cellspacing="3" style=
                "border-collapse: collapse;" width="100%">
                    <tr>
                        <td id='addinput' width='34%'>154<input id='item_name0'
                        name='item_name[0]' placeholder='Packing Charges.'
                        size='70' type='text' value='Packing Charges.'></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="base">
            <div align="left">
                <table border="1" cellpadding="3" cellspacing="3" style=
                "border-collapse: collapse;" width="100%">
                    <tr>
                        <td id='addinput' width='34%'>155<input id='item_name1'
                        name='item_name[1]' placeholder='Packing Charges.'
                        size='70' type='text' value='Packing Charges.'></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="base">
            <div align="left">
            <table border="1" cellpadding="3" cellspacing="3" style=
                "border-collapse: collapse;" width="100%">
                    <tr>
                        <td id='addinput' width='34%'>156<input id='item_name2'
                        name='item_name[2]' placeholder='Packing Charges.'
                        size='70' type='text' value='Packing Charges.'></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="base">
            <div align="left">
                 <table border="1" cellpadding="3" cellspacing="3" style=
                "border-collapse: collapse;" width="100%">
                    <tr>
                        <td id='addinput' width='34%'>157<input id='item_name3'
                        name='item_name[3]' placeholder='Packing Charges.'
                        size='70' type='text' value='Packing Charges.'></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="base">
            <div align="left">
                <table border="1" cellpadding="3" cellspacing="3" style="border-collapse: collapse;" width="100%">
                    <tr>
                        <td id='addinput' width='34%'>158<input id='item_name4'
                        name='item_name[4]' placeholder='Packing Charges.'
                        size='70' type='text' value='Packing Charges.'></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="base">
            <div align="left">
                <table border="1" cellpadding="3" cellspacing="3" style=
                "border-collapse: collapse;" width="100%">
                    <tr>
                        <td id='addinput' width='34%'>159<input id='item_name5'
                        name='item_name[5]' placeholder='Packing Charges.'
                        size='70' type='text' value='Packing Charges.'></td>
                    </tr>
                </table>
            </div>
        </div>
        <div class="base">
            <div align="left">
                <table border="1" cellpadding="3" cellspacing="3" style=
                "border-collapse: collapse;" width="100%">
                    <tr>
                        <td id='addinput' width='34%'>160<input id='item_name6'
                        name='item_name[6]' placeholder='Packing Charges.'
                        size='70' type='text' value='Packing Charges.'></td>
                    </tr>
                </table>
            </div>
        </div><br>
<input type='submit' value='--Update Data--' id='continue'/>
        </form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

你错过了set条款:

  $q = "UPDATE poto_customer_items set prod_description='$item_name' WHERE tender_id='$tender_id' and id='$value' ";

提示:停止使用已弃用的mysql_* API。将mysqli_*PDO与准备好的语句一起使用。 mysql_*将在下一版本的php中删除,准备好的语句会阻止您进行SQL注入。

答案 1 :(得分:0)

替换您的以下

$q = "UPDATE poto_customer_items prod_description='$item_name' WHERE tender_id='$tender_id' and id='$value' ";

$q = "UPDATE poto_customer_items SET prod_description='$item_name' WHERE tender_id='$tender_id' and id='$value' ";

答案 2 :(得分:0)

save.php中的以下位看起来很可疑:

foreach($_POST['id'] as $key=>$value)
{
    //$item_name= $_POST['item_name'][$i];
   $q = "UPDATE poto_customer_items set prod_description='$value' WHERE tender_id='$tender_id' and id='$key' ";
    mysql_query($q) or die(mysql_error());
}

你不应该循环$_POST['item_name']吗?类似的东西:

foreach($_POST['item_name'] as $key=>$value)
{

    $item_name= $_POST['item_name'][$i];
   $q = "UPDATE poto_customer_items set prod_description='$value' WHERE tender_id='$tender_id' and id='$key' ";
    mysql_query($q) or die(mysql_error());
}

如果是这种情况,您甚至可以考虑更改表单页面逻辑,如下所示:

while ($list5 = mysql_fetch_array($result)) {
        $reqitem=$list5['id'];
    echo "<td width='34%' id='addinput'><input type='text' size='70' id='item_name$i' 
name='item_name[$reqitem]' placeholder='{$list5['prod_description']}' 
value='{$list5['prod_description']}'></td>";
}