PHP,更新多列时出错

时间:2015-06-16 08:53:44

标签: php mysql multiple-columns

我的代码不更新数据库中的行,没有语法错误,所以它必须是逻辑错误,它可能是大括号的位置。我想更新同一行中的多个列。这是代码 -

<table border="1" align = "center" >

<form method="POST" action="testupdate.php">

<?php

    while ($row=mysqli_fetch_assoc($q)){

?>      <tr><td><input name="select2" type="radio" value="<?php echo $row['c_id']; ?>"></td>

<?php

    echo "<td>".$row['c_id']."</td>";

    echo "<td>".$row['no_computer']."</td>";

    echo "<td>".$row['c_type']."</td>";

    echo "<td>".$row['c_location']."</td></tr>";
    }

    ?>

<input type="submit" name="update" value="update" ></table></form>

<form method="POST" action="testupdate.php">

<?php

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

    ?>

<form method="POST" action="testupdate.php">

no of computers: <br /><input type="text" name="no_computer2"><br /><br />

type: <br /><input type="text" name="c_type2"><br /><br />

location: <br/><input type="text" name="c_location2"><br /><br />

technical status: <br /><input type="text" name="tech_status2"><br /><br />

<input type="submit" name="ok" value="okay">

</form>

<?php

    if (isset($_POST['ok'])){
        $nc2= $_POST['no_computer2'];
        $ct2= $_POST['c_type2'];
        $cl2= $_POST['c_location2'];
        $ts2= $_POST['tech_status2'];
        $i=$_POST['select2'];

        $s2="UPDATE `tech_computer` SET `no_computer`='$nc2' , `c_type`='$ct2' , `c_location`='$cl2', `tech_status`='$ts2' where `c_id` like '$i'";

        $q2=mysqli_query($s2);

        header ('location:testupdate.php');
    }}}
    ?>

1 个答案:

答案 0 :(得分:0)

$_POST将包含当前form中存在的值。提交第二个表单后,将覆盖第一个form的值。第二个select2内没有字段form。尝试添加带有过帐值的hidden字段作为其值 -

<form method="POST" action="testupdate.php">

<input name="select2" type="hidden" value="<?php echo $_POST['select2']; ?>">
no of computers: <br /><input type="text" name="no_computer2"><br /><br />

type: <br /><input type="text" name="c_type2"><br /><br />

location: <br/><input type="text" name="c_location2"><br /><br />

technical status: <br /><input type="text" name="tech_status2"><br /><br />

<input type="submit" name="ok" value="okay">

</form>