Add 2 sets of arrays in the database

Firstly let me present you my script.

This is index.php

<?php
if(isset($_GET["values"]))
{
?>
<form action="action.php" method="POST"> 
<?php
for($i = 0; $i < $_GET["values"]; $i++)
{
   Echo ' <input type="text" name="values[]" >';
}
?>
<input type="submit" name="submit" value="Add">
</form>
<?php
}   
?>

When i go to index.php?values=3 it will display me 3 textboxes. After i complete them the scripts does that:

action.php

<?php
$connect = mysql_connect("localhost","root","") or die("Conexiune nereusita <br>".mysql_error());
mysql_select_db("teste") or die("Baza de date nu exista!<br>".mysql_error());


if(isset($_POST["values"]))
{
foreach($_POST["values"] as $value)
{
    //$value = someSanitizationMethod($value);

    $query = mysql_query("INSERT INTO test_a VALUES('', $value, 'x')") or die(mysql_error());

}   
     }
?>

so in table test_a it will insert that data

table:test_a
id value1 secondvalue
1 value1  x
2 value2  x
3 value3  x

Everything is fibe, but what if i want to add a second textbox inserting a second value in the column "secondvalue" ?

Echo ' <input type="text" name="values[]" >';
Echo ' <input type="text" name="secondvalues[]" >';

this would be the modification in index.php but in action.php, how can it be done?

This is the supposed result..

$query = mysql_query("INSERT INTO test_a VALUES('', $value, $secondvalue)");

 table:test_a
 id value1 secondvalue
 1 value1  secvalue1
 2 value2  secvalue2
 3 value3  secvalue3

I am out of ideas. I tried multiple arrays, i also tried 2 foreacrh operations but it could suffle my data...Values from the first and second textbox should be completed each set in theyr row...value 1 with second value1 and so on...

Answers


Try this:

Considering that the ith element of $values corresponds to the ith element of $secondvalues, and that both arrays has the same number of elements:

foreach($_POST["values"] as $key => $value)
{
    //$value = someSanitizationMethod($value);

    $query = mysql_query("INSERT INTO test_a VALUES('', $value, '".$_POST['secondvalues'][$key]."')") or die(mysql_error());

} 

Need Your Help

How to interface OpenACC with cublasDgetrfBatched in Fortran?

cuda fortran hpc cublas openacc

I've been working on a Fortran code which uses the cuBLAS batched LU and cuSPARSE batched tridiagonal solver as part of a BiCG iterative solver with ADI preconditioner.I'm using a Kepler K20X with

change structure of $_FILES

php arrays file

I try write universal method which change structure of multidimensional $_FILES.