0

I need to insert something in my data-base but i need to loop through two tables to do something like this

<?php 
include("../db_config.php"); //it contains the data-base informations
include("../refCode.php"); //it contains a functiont which gives a randomString

$refCode = RandomString(20);

if(!empty($_POST)){
    $qid = 8;
    $SQL = "INSERT INTO `saisie` (`sid`, `reference`) VALUES (NULL,?)";
    $set = $db->prepare($SQL);
    $result = $set->execute(array($refCode));
    $sid = $db->lastInsertId();

    $SQL = "SELECT cid FROM champs WHERE qid = ? ORDER BY ordre";
    $set = $db->prepare($SQL);
    $cids = $set->execute(array($qid));

    $SQL = "INSERT INTO `donnees` (`sid`, `cid`, `valeur`) VALUES (?,?,?)";
    $set = $db->prepare($SQL);
    foreach ($_POST as $key => $value and $cids as $id => $cid) { //this is the line 24
       $result = $set->execute(array($sid,$cid,$value));
    }
}

?>

but this doesn't work, i get this error

Parse error: syntax error, unexpected 'and' (T_LOGICAL_AND), expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in C:\wamp64\www\website\user\sumbit_answers.php on line 24

1 Answers1

0

You can only iterate through one array at a time (per 'foreach' statement). So the error you're getting is from the 'and' operator you have in your foreach statement (and everything that follows it); PHP doesn't support that.

You can only do:

foreach (array_expression as $value)

or:

foreach (array_expression as $key => $value)

http://php.net/manual/en/control-structures.foreach.php

To accomplish what you want, I think you just need to rework your logic so that you make two passes over the data to build the final array that you want to insert into the DB.

CJG
  • 176
  • 1
  • 10
  • i understood the first fact when i saw the error, and the second fact after 3hours of search so i reworked my logic and got the solution so fast –  Apr 30 '17 at 13:23