如何在同一时间循环遍历两个不同的表

时间:2017-04-30 01:39:55

标签: php

我需要在我的数据库中插入一些内容,但我需要循环遍历两个表来执行类似这样的操作

<?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));
    }
}

?>

但这不起作用,我收到此错误

  

解析错误:语法错误,意外&#39;和&#39; (T_LOGICAL_AND),   期待::(T_PAAMAYIM_NEKUDOTAYIM)in   第24行的C:\ wamp64 \ www \ website \ user \ sumbit_answers.php

1 个答案:

答案 0 :(得分:0)

您一次只能迭代一个数组(每个&#39; foreach&#39;语句)。因此,您收到的错误来自&#39;和&#39;你的foreach声明中的运算符(以及它后面的所有内容); PHP不支持。

你只能这样做:

foreach (array_expression as $value)

或:

foreach (array_expression as $key => $value)

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

为了实现你想要的,我认为你只需要修改你的逻辑,这样就可以对数据进行两次传递,以构建你想要插入数据库的最终数组。