我试图在我的网站上建立一个简单的评论部分。我用Name和Comment创建了两个字段(或框)。现在,如果我填写名称和注释字段并点击提交,则显示“找不到网页”错误(请参阅attachment)。此外,没有任何内容正在更新到我的数据库中。
[注意:在我的所有本地服务器都正确安装后,其他一切正常。之前我使用过phpMyAdmin,那时它能够从数据库中获取数据。 WAMP服务器也在运行。我正在关注此tutorial和代码。]
main.php:
<!--php code for comment section starts-->
<?php
require ('connect.inc.php');
$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];
if($submit){
if($name&&$comment){
$insert=mysql_query("INSERT INTO comment(name, comment) VALUES ('$name','$comment')");
}else{
echo "Please fill all the fields";
}
}
?>
<!--php code for comment section ends-->
<!--building a comment section starts-->
<form action="main.php method="POST">
<table>
<tr><td>Name: </td><td><input type="text" name="name"/> </td> </tr>
<tr><td colspan="2">Comment: </td></tr>
<tr><td colspan="2"><textarea name="comment"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"/> </td></tr>
</table>
</form>
<!--building a comment section ends-->
connect.inc.php:
<?php
mysql_connect("localhost", "root","");
mysql_select_db("comment_section");
?>
答案 0 :(得分:2)
您忘记了action="main.php
&lt; =
将其更改为action="main.php"
这就是为什么你找不到网页的错误。
Plus ,您当前的代码向SQL injection开放。使用mysqli_*
with prepared statements或PDO with prepared statements。
在您打开<?php
标记后立即将error reporting添加到文件顶部。
error_reporting(E_ALL);
ini_set('display_errors', 1);
将有助于发出代码中发现的错误信号。
和or die(mysql_error())
到mysql_query()
旁注:
现在,如果仍然会引发file not found
错误,请记住main.php
与某些服务器上的Main.php
不同,所以make确保文件名实际上以小写字母命名main.php
;这与self
相同。
您也可以使用<form action="" method="POST">
,因为您的整个代码都位于同一页面内。这会将操作设置为您执行代码的同一页面。