0

我的第 4、5、6 行有问题,一切正常,数据正在发送到数据库以及输出结果,但我在加载页面时收到此消息。

Undefined index: name in C:\xampp\htdocs\xampp\site\comment.php on line 4

Notice: Undefined index: comment in C:\xampp\htdocs\xampp\site\comment.php on line 5

Notice: Undefined index: submit in C:\xampp\htdocs\xampp\site\comment.php on line 6

再次感谢,任何帮助将不胜感激:)这是代码,非常感谢任何可以提前帮助我的人

<?php
mysql_connect("localhost","root","");
mysql_select_db("commentbox");

$name= $_POST["name"];
$comment= $_POST["comment"];
$submit= $_POST["submit"];

$dbLink = mysql_connect("localhost", "root", "Broadband74");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);

if($submit)
{
    if($name&&$comment)
    {
        mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment') ");
    }
    else
    {
        echo "please fill out all fields";
    }
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comment box</title>
</head>

<body>
<center>
<form action="comment.php" method="POST">
<table>
<tr><td>Name: <br><input type="text" name="name"/></td></tr>
<tr><td colspan="2">Comment: </td></tr>
<tr><td colspan="5"><textarea name="comment" rows="10" cols="50"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
</table>
</form>
<?php
$dbLink = mysql_connect("localhost", "root", "");
    mysql_query("SET character_set_results=utf8", $dbLink);
    mb_language('uni');
    mb_internal_encoding('UTF-8');

$getquery=mysql_query("SELECT * FROM commenttable ORDER BY id DESC");
while($rows=mysql_fetch_assoc($getquery))
{
    $id=$rows["id"];
    $name=$rows["name"];
    $comment=$rows["comment"];
    echo $name . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<hr size="1"/>'
;}
?>

</body>
</html>
4

5 回答 5

0
<?php
mysql_connect("localhost","root","");
mysql_select_db("commentbox");

if(isset($_POST["name"]){
$name= $_POST["name"];
}
if(isset($_POST["comment"]){
$comment= $_POST["comment"];
}
if(isset($_POST["submit"]){
$submit= $_POST["submit"];
}

$dbLink = mysql_connect("localhost", "root", "Broadband74");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);

if($submit)
{
if($name&&$comment)
{
mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment') ");
}
else
{
echo "please fill out all fields";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comment box</title>
</head>

<body>
<center>
<form action="comment.php" method="POST">
<table>
<tr><td>Name: <br><input type="text" name="name"/></td></tr>
<tr><td colspan="2">Comment: </td></tr>
<tr><td colspan="5"><textarea name="comment" rows="10" cols="50"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"></td></tr>
</table>
</form>
<?php
$dbLink = mysql_connect("localhost", "root", "");
    mysql_query("SET character_set_results=utf8", $dbLink);
    mb_language('uni');
    mb_internal_encoding('UTF-8');

$getquery=mysql_query("SELECT * FROM commenttable ORDER BY id DESC");
while($rows=mysql_fetch_assoc($getquery))
{
$id=$rows["id"];
$name=$rows["name"];
$comment=$rows["comment"];
echo $name . '<br/>' . '<br/>' . $comment . '<br/>' . '<br/>' . '<hr size="1"/>'
;}
?>

</body>
</html>
于 2013-07-24T15:03:42.940 回答
0

当您第一次加载页面时,还没有绑定到请求的 POST 数据,因此在提交表单之前它会失败。您可以检查是否使用 isset() 设置了值,也可以显式处理错误:

http://php.net/manual/en/language.exceptions.php

或者,您可以将表单和处理它的代码放在不同的页面上。

于 2013-07-24T15:04:55.040 回答
0

您需要将参数与或者mysqli_PDO函数一起使用,但是:

<?php
mysql_connect("localhost","root","");
mysql_select_db("commentbox");

$dbLink = mysql_connect("localhost", "root", "Broadband74");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);

if($_POST["submit"])
{
    if( (isset($_POST["name"]) && !empty($_POST["name"])) &&
        (isset($_POST["comment"]) && !empty($_POST["name"])) )
    {
        $name= $_POST["name"];
        $comment= $_POST["comment"];
        mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment') ");
    }
    else
    {
        echo "please fill out all fields";
    }
}
于 2013-07-24T15:05:51.597 回答
0

您必须检查您的表单是否之前提交过:

if(isset($_POST['submit']))
{    
    mysql_connect("localhost","root","");
    mysql_select_db("commentbox");

    $name= $_POST["name"];
    $comment= $_POST["comment"];
    $submit= $_POST["submit"];

    $dbLink = mysql_connect("localhost", "root", "Broadband74");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);

    if($name&&$comment)
    {
        mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment') ");
    }
    else
    {
        echo "please fill out all fields";
    }
}
于 2013-07-24T15:06:17.900 回答
0

每次加载页面时都会执行您的 PHP 代码。在第一次加载时,没有给出表单输入,并且这些值没有设置。但是您的脚本尝试使用$_POST['field'],并且由于 PHP 不知道这些字段,它只会引发Undefined Index错误。

这个问题的解决方法其实很简单。使用isset()

<?php

if(isset($_POST['submit']) { //checking if form was submitted

mysql_connect("localhost","root","");
mysql_select_db("commentbox");

$name = isset($_POST['name']) ? $_POST['name'] : null; 
$comment= isset($_POST['comment']) ? $_POST['comment'] : null; 
$submit= isset($_POST["submit"]) ? $_POST["submit"] : null;

$dbLink = mysql_connect("localhost", "root", "Broadband74");
    mysql_query("SET character_set_client=utf8", $dbLink);
    mysql_query("SET character_set_connection=utf8", $dbLink);

if($submit)
{
    if($name&&$comment)
    {
        mysql_query("INSERT INTO commenttable (name,comment) VALUES ('$name','$comment') ");
    }
    else
    {
        echo "please fill out all fields";
    }
}
?>
于 2013-07-24T15:07:48.270 回答