2012-07-22 71 views
0

我使用下面的代碼將表單中的值插入到MySQL數據庫中,但是在代碼執行MySQL表後顯示NULL,NULL而不是實際值。當變量存儲實際值時,PHP將NULL插入到MySQL數據庫中

我檢查,看看是否值已被正確地從GET函數獲得,並且在沒有任何問題,或者

<?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"; 
    } 
?> 
+0

這不是['INSERT'](http://dev.mysql.com/doc/refman/5.5/en/insert.html)的工作原理 - 你實際上並沒有提供列值。此外,請停止使用古代mysql_ *函數編寫新代碼。他們不再被維護,社區已經開始[棄用流程](http://news.php.net/php.internals/53799)。相反,您應該瞭解準備好的語句並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/mysqli)。如果你關心學習,[這裏是一個很好的PDO相關教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 – DCoder 2012-07-22 07:02:15

回答

1

用你這樣的查詢

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

謝謝,它已經解決了這個問題 – Yoosuf 2012-07-22 07:09:26

0

你的意思是$query="insert into vacationtype values(LAST_INSERT_ID(),'". $_GET['vac_name'] ."','". $_GET['vac_comment'] ."')"?你必須插入$ _GET的內容。