PHP , error in updating multiple columns

93 views Asked by At

My code doesn't update the rows in my database, there is no syntax error, so it must be a logical error, it might be the positions of the braces. I want to update multiple columns in the same row. Here is the code -

<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

There are 1 answers

4
Sougata Bose On

$_POST will contain the values which are present in the current form. When the second form get submitted the values of first form is overwritten. There is no field - select2 inside the second form. Try with adding a hidden field with posted value as its value -

<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>