简体   繁体   中英

Updating database from a form post

I have the following form:

        <form action="fixedsubmit.php" method="post">
<table  class="table-sm" style="width:600px;" >
    <tr align="center" >
<?php
foreach ($rows as $x){
?>

      <td><b><?php echo $x['name'] ?></b></td>
<?php
}
?>
    </tr>
    <tr>
<?php
foreach ($rows as $x){
?>
      <td><input type text name="amount[]" size="7" style="text-align:center;font-weight:bold;" value="<?php echo $x['amount'] ?>"></td>
<?php
}
?>
    </tr>
    <tr>
<?php
foreach ($rows as $x){
if ($x['status'] == '1'){
$status = "Active";
}
elseif ($x['status'] == '0'){
$status = "Inactive";
}
else{
echo 'Problem';
}
?>
      <td><?php echo $status ?><br />
<?php
if ($status == 'Active'){
?>
Change&nbsp;&nbsp;<input type="hidden" name="status[]" value="0">
                  <input type="checkbox" name="status[]" value="1">  
    <input type="hidden" name="id[]" value="<?php echo $x['id'] ?>"></td>
<?php
}
elseif ($status == 'Inactive'){
?>
Change&nbsp;&nbsp;<input type="hidden" name="status[]" value="0">
                  <input type="checkbox" name="status[]" value="1">
  
    <input type="hidden" name="id[]" value="<?php echo $x['id'] ?>"></td>

<?php
}
}
?>
 
    </tr>
    <tr>
      <td colspan="10" align="center">
        <input type="submit" value="Update Prices" />
      </td>
    </tr>
</table>
</form>

And the fixedsubmit.php look like this:

    // check if form has been submitted
if (isset($_POST['amount'])) {
//var_dump($_POST) . '<br />';
$chkbox = $_POST['amount'];

$i = 0;

while($i<sizeof($chkbox)){

            $sql_fixed = "UPDATE fixed_costs SET amount='".$_POST['amount'][$i]."', status='".$_POST['status'][$i]."' WHERE id='".$_POST['id'][$i]."'";
            $result_fixed = mysqli_query($con, $sql_fixed);


    $i++;
    }
    }

What I am trying to do is update the status[] fields with either a 1 or a zero. They have to match up to the database id. I know this is simple, but I can't seem to figure it out. If I change it from active to inactive, I want to post a 0 for inactive and a 1 for active.

For this purpose, I solved my problem by just creating a link with the id and status and submitting each one individually.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM