我希望在帖子后得到一个id(自动增量)并将其发布到其他表中

时间:2014-03-18 08:41:31

标签: php mysql sql

我想将evenement表的id发布到evenementontvanger.idEvent列中。我试图使用mysql_insert_id(),但这不起作用。我试过这样的事情:

    <?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."') ";
            $qo = "INSERT INTO evenementontvanger (idWerknemer,idProject,idEvent,idKlant,idTaak) VALUES ('".$user_werknemer."','".$user_project."','','".$user_klant."','".$user_taak."')";


        mysql_query($q);
        $id = mysql_insert_id();

$query = 'INSERT INTO evenementontvanger("idEvent") VALUES('.$id.')';
        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); 

    }*/

    ?>

我也尝试过使用last_insert_id。但这并没有把id放在idEvent中。

2 个答案:

答案 0 :(得分:1)

尝试回显$ id = mysql_insert_id(); 如果您有$ id值或使用empty()

查看

尝试替换

$query = 'INSERT INTO evenementontvanger("idEvent") VALUES('.$id.')';

$query = "INSERT INTO evenementontvanger(idEvent) VALUES('".$id."')";

答案 1 :(得分:1)

在以下查询中

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

您正在从字段''设置空字符串idEvent 您可以直接使用LAST_INSERT_ID()代替它,这是在执行为$q定义的查询后生成的;

喜欢:

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

执行mysql_query($q);后,
以下陈述不是必要的。

$id = mysql_insert_id();

$query = 'INSERT INTO evenementontvanger("idEvent") VALUES('.$id.')';