这是我在HTML中的表单。
<html>
<head>
</head>
<body>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form" name="form" method="POST" action="add_new_topic.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
<tr>
<td width="14%"><strong>Topic</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
<td valign="top"><strong>Detail</strong></td>
<td valign="top">:</td>
<td><textarea name="detail" cols="50" rows="3" id="detail"></textarea></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50" /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="email" type="text" id="email" size="50" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
这是add_new_topic.php代码。
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="myforum"; // Database name
$tbl_name="fquestions"; // Table name
// Connect to server and select database.
$con = mysqli_connect("$host", "$username", "$password" , "$db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"$db_name")or die("cannot select DB");
// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO '$tbl_name'('topic',' detail', 'name', 'email', 'datetime')VALUES('topic', 'detail', 'name', 'email', 'datetime')";
$result=mysqli_query($con,$sql);
if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysqli_close($con);
?>
显示错误:
注意:未定义的索引:主题在 第18行的C:\ xampp \ htdocs \ myforum \ add_new_topic.php
注意:未定义的索引:详细信息 第19行的C:\ xampp \ htdocs \ myforum \ add_new_topic.php
注意:未定义的索引:名称在 第20行的C:\ xampp \ htdocs \ myforum \ add_new_topic.php
注意:未定义的索引:电子邮件 第21行的C:\ xampp \ htdocs \ myforum \ add_new_topic.php错误
这里,在第18,19,20和21行,以下代码所在。
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
现在这个POST方法有什么错误?
答案 0 :(得分:1)
在获取post值之前如果post值不为空,则添加一个条件:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="myforum"; // Database name
$tbl_name="fquestions"; // Table name
if(isset($_POST) && !empty($_POST)){
// Connect to server and select database.
$con = mysqli_connect("$host", "$username", "$password" , "$db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"$db_name")or die("cannot select DB");
// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO '$tbl_name'('topic',' detail', 'name', 'email', 'datetime')VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysqli_close($con);
}
?>