简体   繁体   中英

PHP inserting NULL into MySQL database when variables store real values

I am using the code below to insert the values from the form into the MySQL database, however after the code executes the MySQL tables displays NULL, NULL instead of the actual value.

I have checked to see whether the values have been correctly obtained from the GET function, and there is no problem in that either

<?php
    $vac_name=$_GET['vac_name'];
    $vac_comment=$_GET['vac_comment'];
    echo $vac_name;
    echo $vac_comment;
    $con=mysql_connect("localhost","root","");  
    if($con==true){
        echo "Connected to the database";
     if(isset($_GET['vac_name'], $_GET['vac_comment'])){

        mysql_select_db("attendance_db",$con);
        $query = "insert into vacationtype values(LAST_INSERT_ID(),vac_name,vac_comment)";
        $result=mysql_query($query,$con);
        //$row=mysql_num_rows($result);
    //  if($row>0){
        if($result==true){  
            echo "Successfully saved your message <br> We shall be in contact with you shortly";
        }else{
            echo mysql_error();
            echo "Sorry the message was not saved <br> Please try again. Thank You";
        }   }
        mysql_close($con);
    }else{
        echo "Cannot connect to the database";
    }
?>

像这样使用您的查询

insert into vacationtype values('LAST_INSERT_ID()','vac_name','vac_comment')

You mean $query="insert into vacationtype values(LAST_INSERT_ID(),'". $_GET['vac_name'] ."','". $_GET['vac_comment'] ."')" ? You have to insert the contents of $_GET.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM