0

I'm having an issue with a PHP form doing an update one row at a time. It will work for the first row no problem, but the next rows don't get all the variables. For example, row 1 will have company1, checked paid and not checked for deactivate and it gets and updates all variables. But company2's row only gets the ID and the default values for the checkboxes. For some reason it doesn't pull the info for the whole row, just the ID. Any help would be appreciated!

This is my Form (edited):
           <html>
    <body>

<form action="next.php" method="POST">
<table border="1" align="center">
<tr>
    <th>
        Name
    </th>
    <th>
        Address
    </th>
    <th>
        Paid
    </th>
    <th>
        Active
    </th>
</tr>
<?php
require_once ("connect.php");
require_once ("phpfunctions.php");
error_reporting(0);

    $cd=companyDisplay();

$result = mysqli_query($mysqli,$cd);


while($res=mysqli_fetch_array($result))
{
 $final[]=$row;


?>

<tr>
    <td class="name">
        <a href='edit.php?ID="<?php echo $res['ID']; ?>"'>
            <?php echo $res['name'];?>
        </a>
    </td>
    <td class="Address">
        <?php echo $res['Address'];?>
    </td>
    <td class="paid">
    <?php foreach($final as $k => $v){ ?>
        <input type="checkbox" value="0" name="results[<?= $k ?>][paid]"
        <?php } ?>
            <?php 
                if($res["paid"]==0)
                {
                echo "checked";
                }               
            ?>>

    </td>
    <td class="active">
    <?php foreach($final as $k => $v){ ?>
        <input type="checkbox" value="0" name="results[<?= $k ?>][active]"
        <?php } ?>
            <?php 
                if($res["active"]==0)
                {
                    echo "checked";
                }        
            ?> >
    </td>

    <td class="update">
        <?php foreach($final as $k => $v){ ?>
        <input type="hidden" name="results[<?= $k ?>][ID]" value="<?php echo $res['ID']; ?>">
            <?php } ?>
        <input type="submit" name="submit" value="Update">

    </td>
</tr>

<?php } ?>

</table>
</form>

    </body>
</html>


This is my next php page where the variables are posted and the query is run:

<?php
require_once ("connect.php");
require_once ("phpfunctions.php");
error_reporting(0);
var_dump($_POST);


//info to store from index.php
$ID=$_POST['ID'];
$ID= str_replace('"', '', $ID);
$paid=(isset($_POST['paid']))? 0 : 1;
$active = (isset($_POST['active']))? 0 : 1;


//adds updated info to database
$uc=updateCompany($paid,$active,$ID);
$r = mysqli_query($mysqli,$uc); 
echo "Information stored successfully";
echo $paid;
echo $active;
echo $ID;
?>

And here is my function:

function updateCompany($paid,$active,$ID)
{
    $uc="UPDATE
            `company`
        SET
            `paid`='$paid',
            `active`='$active'
        WHERE
            `ID`='$ID'";

    return $uc;
}

Output for first row:

array(3) { ["paid"]=> string(1) "0" ["ID"]=> string(2) "12" ["submit"]=> string(6) "Update" } Information stored successfully 0(paid) 1(deactivate) 12(id)

output for next row:

array(2) { ["ID"]=> string(2) "13" ["submit"]=> string(6) "Update" } Information stored successfully 1(paid) 1(deactivate) 13(ID)

Both have same choices set and should be same except for the ID. Where am I going wrong here?

iggulds
  • 21
  • 6

1 Answers1

0

Multiple problems here:

  • your forms do not need enctype="multipart/form-data" as this pertains to file uploads

  • <tr> tags missing. Table headers should be inside <tr> tags

  • echo "checked=='checked'"; has an extra = sign, not needed, i would just place the word checked there

  • indexVerify.php?ID= Why are you using ?ID= if your form's method is POST not GET.

  • <form action='indexVerify.php?ID="<?php echo $res['ID']; ?>"' method="post"> is inside another form. That is not allowed and will break your html code.

  • you are iterating your results and setting your inputs the wrong way. This is what you are basically doing now:

    <form>
    <?php while ($res = mysqli_fetch_array($result)){ ?>
      <input type="checkbox" value="0" name="paid" />
    <?php } ?>
    </form>
    

    When you submit your form, you will end up with the last : $_POST[paid] = ""; ignoring all the other results. To fix it set your inputs should be set like this:

    <?php
    while ($res = mysqli_fetch_array($result)){
      $final[] = $row;
    }
    ?>
    <form>
    <?php foreach($final as $k => $v)){ ?>
         <input type="checkbox" value="0" name="results[<?= $k ?>][paid]" />
    <?php } ?>
    </form>
    

    Doing it like the above will give you a result of an array like this:

    $_POST[results] = 
     (array)
        [0] => [
                 "paid" => ""
               ]
        [1] => [
                 "paid" => ""
               ]
    

    which you can then iterate and submit it to your database.

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
  • How would an iteration from that look to a database? – iggulds Aug 18 '15 at 16:05
  • changed the form but the arrays are still giving me the same IDs of the last one. I posted changes, maybe I did something wrong – iggulds Aug 18 '15 at 16:13
  • hi and welcome to StackOverflow. This is a lot to explain, perhaps I can help you remotely through Teamviewer. Let me know. – CodeGodie Aug 18 '15 at 16:13
  • Thats a possibility, any good links or references you think might help then? I'm still getting the same IDs in the arrays – iggulds Aug 18 '15 at 16:33
  • im assuming you mean "thas NOT a possibility"? if not no problem. – CodeGodie Aug 18 '15 at 16:34
  • its a possibility, the code example you gave me still has issues. ID's are all the same and I have an increasing row of checkboxes. As well it doesnt take the a default value for checkboxes. It might be more beneficial to others to post the answers here anyway – iggulds Aug 20 '15 at 17:20