对于每个循环,不使用多个变量

时间:2014-03-18 09:22:09

标签: php mysql sql

我通过多选项面板有一个包含多个变量的foreach循环。但是当我输入多个变量时,它只会以某种方式插入1个变量。

process.php:

<?php
//include db configuration file
include 'connection.php';
function user_joined($user_werknemer,$user_project,$user_klant,$user_taak,$user_name,$user_desc, $user_start, $user_startdate, $user_starttime, $user_end, $user_enddate, $user_endtime, $user_color){

        $q = "INSERT INTO evenement (id,title,description,start,startdate,starttime,end,enddate,endtime,color) VALUES 
            ('','".$user_name."','".$user_desc."','".$user_start."','".$user_startdate."','".$user_starttime."','".$user_end."','".$user_enddate."','".$user_endtime."','".$user_color."') ";
            foreach ($user_werknemer as $test){
        $qo = "INSERT INTO evenementontvanger (idWerknemer,idProject,idEvent,idKlant,idTaak) VALUES ('".$test."','".$user_project."',LAST_INSERT_ID(),'".$user_klant."','".$user_taak."')";
}

    mysql_query($q);

    mysql_query($qo);}


if(isset($_POST['user_werknemer'],$_POST['user_project'],$_POST['user_klant'],$_POST['user_taak'],$_POST['user_name'],$_POST['user_desc'],$_POST['user_startdate'],$_POST['user_start'],$_POST['user_starttime'],$_POST['user_enddate'],$_POST['user_endtime'],$_POST['user_end'],$_POST['user_color'],$_POST['action'])){

        $user_werknemer=$_POST['user_werknemer'];
        $user_color=$_POST['user_color'];
        $user_name=$_POST['user_name'];
        $user_desc=$_POST['user_desc'];
        $user_project=$_POST['user_project'];
        $user_klant=$_POST['user_klant'];
        $user_taak=$_POST['user_taak'];
        $user_start=$_POST['user_startdate']." ".$_POST['user_starttime'];
        $user_startdate=$_POST['user_startdate'];
        $user_starttime=$_POST['user_starttime'];
        $user_end=$_POST['user_enddate']." ".$_POST['user_endtime'];
        $user_enddate=$_POST['user_enddate'];
        $user_endtime=$_POST['user_endtime'];
        $action=$_POST['action'];
        if ($action=='joined'){
            user_joined( $user_werknemer, $user_project, $user_klant, $user_taak, $user_name, $user_desc, $user_start, $user_startdate, $user_starttime, $user_end, $user_enddate, $user_endtime, $user_color);
            }
    }
/*if ( (isset($_POST["id"]) && strlen($_POST["id"]) >= 3 && strlen($_POST["id"]) <= 60) &&
    (isset($_POST["name"]) && strlen($_POST["name"]) >= 3 && strlen($_POST["name"]) <= 50) &&
    (isset($_POST["age"]) && strlen($_POST["age"]) >= 3 && strlen($_POST["age"]) <= 40) ) 
{   //check $_POST["name"] and $_POST["address"] and $_POST["city"] are not empty

    $id   = $_POST["id"];
    $name = $_POST["name"];
    $age   = $_POST["age"];

    $q = "INSERT INTO tbltest ( id, name, age) VALUES 
            ('".$id."','".$name."','".$age."')";
    mysql_query($q); 

}*/

2 个答案:

答案 0 :(得分:2)

尝试在foreach循环中执行查询,在foreach循环外调用查询运行

 foreach ($user_werknemer as $test){
        $qo = "INSERT INTO evenementontvanger (idWerknemer,idProject,idEvent,idKlant,idTaak) VALUES ('".$test."','".$user_project."',LAST_INSERT_ID(),'".$user_klant."','".$user_taak."')";


$execute_query = mysql_query($qo);
}

尝试此操作将插入数据。

答案 1 :(得分:2)

您的查询是在foreach循环之外执行的。因此它只插入一个值。

     foreach ($user_werknemer as $test){
        $qo = "INSERT INTO evenementontvanger (idWerknemer,idProject,idEvent,idKlant,idTaak) VALUES ('".$test."','".$user_project."',LAST_INSERT_ID(),'".$user_klant."','".$user_taak."')";
 mysql_query($qo);
}

那应该......