gpt4 book ai didi

php - 未定义索引但它在数据库中命名?

转载 作者:行者123 更新时间:2023-11-29 00:07:47 28 4
gpt4 key购买 nike

大家好,我遇到了一个不知道如何解决的问题,它告诉我我有一个“ undefined index ”用于以下内容:

Notice: Undefined index: name in D:\xampp\htdocs\pages\service.php on line 113

Notice: Undefined index: comment in D:\xampp\htdocs\pages\service.php on line 114

Notice: Undefined index: submit in D:\xampp\htdocs\pages\serv

但我的数据库中清楚地列出了它: http://puu.sh/cAlHz/56afbd7e80.png

评论功能可以正常工作,但它向我显示此错误。有什么想法吗?

<?php
mysql_connect("localhost","root", "");
mysql_select_db("wd6__0100348514");
$name=$_POST['name'];
$comment=$_POST['comment'];
$submit=$_POST['submit'];

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

if($submit)
{
if($name&&$comment)
{
$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
echo "<meta HTTP-EQUIV='REFRESH' content='0; url=service.php'>";
}
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="service.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 comment 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"/>'
;}
?>

最佳答案

此代码块:(另请参阅我关于未定义索引警告脚注)

if($submit)
{
if($name&&$comment)
{

可以而且应该替换为isset()empty()同时用大括号括起代码执行。

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

if(isset($_POST['submit'])
&& !empty($_POST['name'])
&& !empty($_POST['comment']) ){

$name=$_POST['name'];
$comment=$_POST['comment'];

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

$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
echo "<meta HTTP-EQUIV='REFRESH' content='0; url=service.php'>";
}
else
{
echo "please fill out all fields";
}

但是您使用的是已弃用的库。

此外,您的代码对 SQL injection 开放.
使用 mysqli_* with prepared statements , 或 PDOprepared statements .


或:

if(isset($_POST['submit'])){

if(!empty($_POST['name']) && !empty($_POST['comment']) ){

$name=$_POST['name'];
$comment=$_POST['comment'];

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

$insert=mysql_query("INSERT INTO comment (name,comment) VALUES ('$name','$comment') ");
echo "<meta HTTP-EQUIV='REFRESH' content='0; url=service.php'>";
}
else
{
echo "please fill out all fields";
}

}

脚注:

在我看来,您好像是在同一个页面中运行所有代码,这就是为什么您会在页面加载后立即收到 undefined index 警告。

此外,只是一个建议:您正在使用 <form action="service.php" method="POST">可以改为<form action="" method="POST">

关于php - 未定义索引但它在数据库中命名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26701327/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com