使用foreach循环发布另一个表的相同id

时间:2014-03-18 10:00:40

标签: php mysql sql post

我想将evenement表的id发布到evenementontvanger.idEvent: 我以为我可以使用Last_insert_id。但是当我插入多个$ User_werknemer时,由于foreach循环它不起作用。所以我想使用db->insert_id()。但是我无法做到这一点。 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."') ";
            $tester = $this->db->insert_id();

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

    mysql_query($q);

    }


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'])){
        $tester=$_POST['tester'];
        $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); 

}*/

?>

1 个答案:

答案 0 :(得分:1)

在第二次查询之前,您只能在运行查询时获取last_insert_id。

你不需要在函数中设置每一个postvariable .....单独的数组就足够了;)

顺便说一下。不推荐使用mysql_query,使用mysqli或PDO左右;)