2013-01-17 64 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

非常感谢您的帮助 –