1

I have three arrays and I want to add the arrays in position [0], then [1], then [2] etc of the arrays so I can insert the values for the three fields into my database.

First I connect to my DB and get my results:

<?php
require_once("con.php");
$p = payment();
$result = mysqli_query($mysqli, $p);
?>

Then I build my form:

<form>
    <table>
        <?php while ($res = mysqli_fetch_array($result)) { ?>
            <tr>
                <td class="paid">
                    <input type="hidden" value="1" name="paid[<?php echo $res['name']; ?>]">
                    <input type="checkbox" value="0" name="paid[<?php echo $res['name']; ?>]"
                        <?php
                        if ($res["paid"] == 0) {
                            echo "checked";
                        }
                        ?>>
                </td>
                <td class="active">
                    <input type="hidden" value="1" name="active[<?php echo $res['name']; ?>]">
                    <input type="checkbox" value="0" name="active[<?php echo $res['name']; ?>]"
                        <?php
                        if ($res["active"] == 0) {
                            echo "checked";
                        }
                        ?> >
                </td>
                <input type="hidden" name="ID[<?php echo $res['name']; ?>]" value="<?php echo $res['ID']; ?>">
            </tr>
        <?php } ?>
        <tr>
            <td>
                <input type="submit" name="submit" value="Update">
            </td>
        </tr>
    </table>
</form>

And this is my PHP handler:

<?php

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

$paid = $_POST['paid'];
$active = $_POST['active'];

foreach ($_POST as $key => $value) {
    $ID = $ID[$key];
    $paid = $paid[$key];
    $active = $active[$key];

    $up = updatePayment($paid, $active, $ID);
    $r = mysqli_query($mysqli, $up);
    echo "Information stored successfully";

}


?>

Right now it's not inserting anything, I can see the arrays have the proper data in them with var_dump($_POST); but I can't seem to get it to insert into the database. I've tried some for loops, foreach but not sure I'm doing it right.

Any ideas?

var_dump shows this :

array(4) { 
    ["paid"]=> array(3) { 
        ["CompanyA"]=> string(1) "0" 
        ["CompanyB"]=> string(1) "0" 
        ["CompanyC"]=> string(1) "0" 
    } 
    ["active"]=> array(3) { 
        ["CompanyA"]=> string(1) "1" 
        ["CompanyB"]=> string(1) "1" 
        ["CompanyC"]=> string(1) "0" 
    } 
    ["ID"]=> array(3) { 
        ["CompanyA"]=> string(2) "12" 
        ["CompanyB"]=> string(2) "13" 
        ["CompanyC"]=> string(2) "14" 
    } 
    ["submit"]=> string(6) "Update" 
} 

Is there a way to group all CompanyA together to into an array to update or an array of all arrays [o] position, then [1], etc?

Rangad
  • 2,110
  • 23
  • 29
iggulds
  • 21
  • 6
  • You don't have an insert function in place. You only have one to update data, not insert it. Depending on your table structure you could change the update query to insert or update https://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html – Funk Doc Aug 20 '15 at 16:53
  • why do you have both your `input:hidden` and `input:checkbox` set to the same name? When you submit this, the last name will overwrite the prior. – CodeGodie Aug 20 '15 at 16:58
  • looks like data in multidimensional array and data access in loop is incorrect can you please show how data listed in var_dump()? – Yogendra Aug 20 '15 at 16:58
  • same name for hidden and checkbox because: http://stackoverflow.com/questions/32105188/multiple-values-1-or-0-for-checkbox-array-with-same-name – iggulds Aug 20 '15 at 17:37
  • Doesn't really answer your question (sorry!), but as your code might be vulnerable to sql-injections you might find it worth to read [some of the answers to this question](http://stackoverflow.com/q/60174/2912456) – Rangad Aug 20 '15 at 17:50

1 Answers1

1

This is the answer I came up with. Hopefully it helps someone in the future:

My php handler:

<?php
require_once("con.php");

$ID=$_POST['ID'];
$paid=$_POST['paid'];
$active=$_POST['active'];
$count=count($ID);

 $n= implode(", ", $ID);
 $ID=explode(',',$n);

 $p=implode(", ", $paid);
 $paid=explode(',',$p);

 $a=implode(", ", $active);
 $active=explode(',',$a);

$i=0;
for ($i; $i<$count; $i++)
{

   echo $paid[$i];
   echo $active[$i];
   echo $ID[$i]; 



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


 } 
iggulds
  • 21
  • 6