2013-01-17 65 views
1

我已經創建了一個簡單的Web表單,您可以在其中輸入數據,然後將其提交到數據庫中,但提交時提交的信息卻是正確的,但是當我在表中查看數據時,數據是不可見的。未插入數據

查詢

CREATE TABLE `recipe` (
    `id` int(4) NOT NULL auto_increment, 
    `recipename` varchar(65) NOT NULL default '', 
    `ingredients` varchar(65) NOT NULL default '', 
    `instructions` varchar(65) NOT NULL default '', 
PRIMARY KEY (`id`) 
) TYPE=MyISAM AUTO_INCREMENT=0 ; 

PHP代碼

<?php 

$host="localhost"; // Host name 
$username="my username"; // Left empty due to privacy 
$password="mypassword"; // Left empty due to privacy 
$db_name="mydatabase"; // Left empty due to privacy 
$tbl_name="recipe"; // Table name 


mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 


$recipe=$_POST['recipename']; 
$ingredients=$_POST['ingredients']; 
$instructions=$_POST['instructions']; 


$sql="INSERT INTO $tbl_name(recipename, ingredients, instructions)VALUES('$recipe', '$ingredients', '$instructions')"; 
$result=mysql_query($sql); 


if($result){ 
    echo "Successful"; 
    echo "<BR>"; 
    echo "<a href='recipe.php'>Back to main page</a>"; 
} else { 
    echo "ERROR"; 
} 
?> 

<?php 
// close connection 
mysql_close(); 
?> 

Web窗體

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1"> 
    <tr> 
    <td> 
     <form name="form1" method="post" action="insert_ac.php"> 
     <table width="100%" border="0" cellspacing="1" cellpadding="3"> 
      <tr> 
      <td colspan="3"><strong>Insert Data Into mySQL Database </strong></td> 
      </tr> 
      <tr> 
      <td width="71">Recipe name</td> 
      <td width="6">:</td> 
      <td width="301"><input name="name" type="text" id="recipe"></td> 
      </tr> 
      <tr> 
      <td>Ingredients</td> 
      <td>:</td> 
      <td><input name="lastname" type="text" id="ingredients"></td> 
      </tr> 
      <tr> 
      <td>Instructions</td> 
      <td>:</td> 
      <td><input name="email" type="text" id="instructions"></td> 
      </tr> 
      <tr> 
      <td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td> 
      </tr> 
     </table> 
     </form> 
    </td> 
    </tr> 
</table> 

我很想知道,如果有人知道的數據是不可見的原因。表中沒有數據

+0

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。 – Kermit

+0

除了明顯的SQL注入漏洞,你需要添加一些錯誤捕獲('或死亡(mysql_error());') – Kermit

+0

嘗試添加: 到 '$結果= mysql_query($的SQL);' 這 'echo mysql_error();' 它吐出錯誤到你的屏幕..數據是不可見的,但出現了錯誤。 – snitch182

回答

4
$recipe=$_POST['recipename']; 
$ingredients=$_POST['ingredients']; 
$instructions=$_POST['instructions']; 

需求enter image description here

圖片爲:

$recipe=$_POST['name']; 
$ingredients=$_POST['lastname']; 
$instructions=$_POST['email']; 

你有表單字段名稱錯誤的,所以它是沒有得到一個值,從而插入空數據。如果您將error_reporting設置爲E_ALL,則會發生錯誤。確保你在該設置下使用它進行開發。

另外在表單字段的name屬性是索引的名字會是什麼$_POST['INDEX_NAME'],你是mistakingly使用的id屬性。

+0

所以我應該設置輸入字段而不是id我沒有意識到,愚蠢的錯誤。我還是PHP的新手 –

+0

@勞倫斯史密斯愚蠢的錯誤只是一個學習的過程。我已經超過了我的記憶= o)如果這解決了您的問題,請將其標記爲已回答,並且如果您有任何問題,請發表評論。祝你愉快。 –

+0

非常感謝您的幫助 –