0

hi everyone i get the arrays after the exceution how can i store that array in the database coding

if (isset($_POST['submit'])) {

    $data_t1 = $_POST['t1'];

    foreach ($data_t1 as $key => $value) {

        echo 'T1: ' . $value . '<br />';
        echo 'T2: ' . $_POST['t2'][$key] . '<br />';
          echo 'T3: ' . $_POST['a1'][$key] . '<br />';
        echo 'Username: ' . $_POST['username'][$key] . '<br /><br />';


 echo 'T11: ' . $_POST['t11'] . '<br />';
          echo 'T22: ' . $_POST['t22'][$key] . '<br />';
              echo 'T33: ' . $_POST['a11'][$key] . '<br />';
        echo 'name: ' . $_POST['name'][$key] . '<br /><br />';


    }
      $data_t2 = $_POST['t2'];
      $data_t3=$_POST['a1'];
      $data_t4=$_POST['username'];
      $data_t11 = $_POST['t11'];
      $data_t22=$_POST['t22'];
      $data_t33=$_POST['a11'];
       $data_t44=$_POST['name'];
}
var_dump($data_t1);
var_dump($data_t2);
var_dump($data_t3);
var_dump($data_t4);
var_dump($data_t11);
var_dump($data_t22);
var_dump($data_t33);
var_dump($data_t44);

?>

array are display in ths form

 $data_t1( 12  12)
    $data_t2( 4    3)
    $data_t3( 5    6)
    $data_t4( 1.44  1.33)
    $data_t11( 12  12)
     $data_t22( 4    3)
     $data_t33( 5    6)
     $data_t44( 1.44  1.33)

i need to store theses array in the database in ths form

row1 12   4  5 1.44 12 4 5 1.44
row2 12   3  6 1.33 12 3 6 1.33 

i edited my question

3
  • 1
    What have you tried? Shouldn't it just be a for loop that performs an INSERT query? Commented May 26, 2013 at 6:04
  • sir i dont get idea how can i explicity mention the format in for loop as i am new in ths php Commented May 26, 2013 at 6:16
  • How can you know how to print things in a for loop, but not how to create a MYSQL INSERT statement in a for loop? It's the same idea. Commented May 26, 2013 at 6:21

2 Answers 2

1

This is like Tifa's answer, but uses a for loop so it will work with any number of values.

$db = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

//query
$sql = "INSERT INTO table (c1, c2, c3, c4) VALUES (:c1, :c2, :c3, :c4)";
$stmt = $db->prepare($sql);
foreach ($data_t1 as $i => $value) {
    $stmt->execute(array(
       ':c1'=>$data_t1[$i],
       ':c2'=>$data_t2[$i],
       ':c3'=>$data_t3[$i],
       ':c4'=>$data_t4[$i],
    ));
}
Sign up to request clarification or add additional context in comments.

4 Comments

Does your HTML have explicit array indexes on the input elements, so they don't start from 0 like normal arrays?
I changed the loop to for ... as so it will use whatever array indexes you have in the HTML, like you do in your printing loop.
i used the same code but error come Parse error: syntax error, unexpected T_AS, expecting ';' in C:\wamp\www\E\kl2.php on line 105 at for loop
Sorry, I forgot to change for to foreach.
1

Here's a snippet that does what you want:

//database connection
$db = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

//query
$sql = "INSERT INTO table (c1, c2, c3, c4) VALUES (:c1, :c2, :c3, :c4)";
$stmt = $db->prepare($sql);
$stmt->execute(array(
   ':c1'=>$data_t1[0],
   ':c2'=>$data_t2[0],
   ':c3'=>$data_t3[0],
   ':c4'=>$data_t4[0],
));

//this is for row1:
+--------------------+
| 12  | 4 | 6 | 1.33 |
+--------------------+   

+++++++++++++++++++++++++++++++++++++++++ 

//and for row2
$stmt->execute(array(
   ':c1'=>$data_t1[1],
   ':c2'=>$data_t2[1],
   ':c3'=>$data_t3[1],
   ':c4'=>$data_t4[1],
));

//this is for row2:
+----------------------+
| 12  |  3  | 5 | 1.44 |
+----------------------+

NB: I didn't do a test run of this code so you might encounter some errors.

EDIT

Using Barmar's idea just add extra columns c5, c6, c7, c8 and bind them to new values $data_t11[$i], $data_t22[$i], $data_t33[$i], $data_t44[$i]

$sql = "INSERT INTO table (c1, c2, c3, c4, c5, c6, c7, c8) VALUES (:c1, :c2, :c3, :c4, :c5, :c6, :c7, :c8)";
$stmt = $db->prepare($sql);
foreach ($data_t1 as $i => $value) {
    $stmt->execute(array(
       ':c1'=>$data_t1[$i],
       ':c2'=>$data_t2[$i],
       ':c3'=>$data_t3[$i],
       ':c4'=>$data_t4[$i],
       ':c5'=>$data_t11[$i],
       ':c6'=>$data_t22[$i],
       ':c7'=>$data_t33[$i],
       ':c8'=>$data_t44[$i],
    ));
}

2 Comments

Post the the code giving the error as well as the error you are getting
I did a test run of the code here and it works just fine. Substitute the following $dbhost, $dbname, $dbuser, $dbpass and table with appropriate values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.