2012-09-18 49 views
2

下面是代碼: contact_us2.php什麼都不會插入到我的數據庫

<form id="form1" method="post" action="enquires.php"> 
<fieldset> 
<legend>Form to database example</legend> 
    <label for="text"> 
     <span>Comments:</span> 
    <textarea id="text" name="comments" rows="4" cols="80"></textarea> 
     </label> 
     <label for="name"> 
    <span>Name:</span> 
     <input id="name" type='text' name='name' size='50'/> 
     </label> 
     <label for="email"> 
    <span>Email:</span> 
    <input id="email" type='text' name='email' size='50'/> 
     </label> 

    <label for="submit1" id="submit"><span>&nbsp;</span> 
     <input id="submit1" class="submit" type="submit" name="submit" value="Submit"/> 
     </label> 
    </fieldset> 
    </form> 


       enquires.php 

       <?php 
     session_start(); 
     error_reporting(E_ALL & ~E_NOTICE); 
     require('authenticate.php'); 
     include_once"../scripts/connect_to_mysql.php"; 
     $name = $_post['name']; 
     $email = $_post['email']; 
     $comments = $_post['comments']; 
     echo "$email"; 
     // Query the body section for the proper page 
     mysql_select_db("hardware_cms")or die (mysql_error()); 
     $sqlCommand = MYSQL_QUERY("INSERT INTO enquires (id, name, email,  comments)". "VALUES ('NULL', '$name', '$email', '$comments')") or die (mysql_error()); 
     ?> 

沒有錯誤。插入數據庫的唯一東西是id。我認爲這個問題在contact_us2.php中。如果這是一個愚蠢的問題,我是新來的HTML和PHP抱歉。

+8

請不要在新代碼中使用'mysql_ *'函數。他們不再被維護,社區已經開始[棄用流程](http://news.php.net/php.internals/53799)。看到[**紅框**](http://php.net/mysql-connect)?相反,您應該瞭解[準備好的陳述](http://en.wikipedia.org/wiki/Prepared_statement)並使用[PDO](http://php.net/pdo)或[MySQLi](http:// php.net/mysqli)。如果你不能決定,試試[這篇文章](http://php.net/mysqlinfo.api.choosing)。如果你關心學習,[這裏是很好的PDO教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 –

+0

謝謝你,爲什麼我的代碼不起作用? – user1680558

+0

@ user1680558這不是它無法正常工作的原因,但是足夠重要的是可以立即停止正在執行的操作,並閱讀Mihai建議的內容並使用其中一種方法執行數據庫查詢。 – NightHawk

回答

2

的錯誤是在PHP代碼,因爲你必須使用$ _ POST和不$ _ POST

$name = $_POST['name']; 
$email = $_POST['email']; 
$comments = $_POST['comments']; 
+0

謝謝你的工作:) – user1680558

+0

歡迎接受答案,如果你已經解決了問題 –

1

更換$_post$_POST看看會發生什麼。此外,請清理您的SQL輸入(通過使用mysql_real_escape_string,或更好 - 通過使用PDO準備語句)。

+0

是的。如果他使用$ _post,我相信提問者會看到一個沒有db值的新行。大聲笑 – jondinham

+0

是的,這是一個好主意,謝謝你:)和$ _POST確實工作。 – user1680558

相關問題