-1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Keith Holm
  • 11
  • 3
  • 1
    So what exact issue are you having? – ADyson May 04 '21 at 19:33
  • 2
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman May 04 '21 at 20:21
  • @ADyson....When i do the post, if I am trying to change 1 only, it only changes the first id (1) to a one and not the one I have updated id (6). Or if I change multiple it will not change them by the where id-$id statement. – Keith Holm May 04 '21 at 20:45
  • @Dharman.... I will investigate the use of the prepared statements. I have never done that, but am not to old to learn. – Keith Holm May 04 '21 at 20:48
  • 1
    Ok. question: why do you have a hidden field _and_ a checkbox both with name="status"? That doesn't seem to make a lot of sense and I wonder if it has something to do with your issue – ADyson May 04 '21 at 21:16
  • Also you have `if ($status == 'Active'){` and `elseif ($status == 'Inactive'){` but in both cases you output the exact same HTML regardless, so it's unclear what the purpose of the `if/else if` is supposed to be...did you intend to vary the output in some way, but forgot? – ADyson May 04 '21 at 21:18
  • So from what I can tell, If I don't have a 0 or 1 assigned to an id, it will only post the first however many. i.e I have 9 items total, of them I want to activate the last 8, if I only send 8 1's it will add a 1 to the first 8 id's and id (9) will be a zero. Like I said, is what I am trying to do is at random times activate and deactivate an item from showing when I query the database. I want to be able to update the 0 or 1 as it corresponds to the id chosen. Does that help? – Keith Holm May 04 '21 at 21:22
  • When I update it now, it changed the one I select to active, then deactivates the next one. – Keith Holm May 04 '21 at 21:28

1 Answers1

0

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

Keith Holm
  • 11
  • 3