我已经创建了一个简单的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>
我很想知道,如果有人知道的数据是不可见的原因。表中没有数据
[**请不要在新代码中使用'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
除了明显的SQL注入漏洞,你需要添加一些错误捕获('或死亡(mysql_error());') – Kermit
尝试添加: 到 '$结果= mysql_query($的SQL);' 这 'echo mysql_error();' 它吐出错误到你的屏幕..数据是不可见的,但出现了错误。 – snitch182