doupai6875 2013-12-06 08:17
浏览 56
已采纳

数据库未定义索引[重复]

This question already has an answer here:

it's me again. I tried to make comments to my website and apparently it is working :/, like in the database it shows inserted values, but every time I refresh the page or go to the page, I get this error:

Notice: Undefined index: user_comment_name in /home/kall0467/public_html/svale_rejser/comments.php on line 38 Notice: Undefined index: user_comment_message in /home/kall0467/public_html/svale_rejser/comments.php on line 39 1 record added

I've been trying to figure it out for 2 days. Here's the code:

<?php


$con = mysql_connect("URL","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("database", $con);

$result = mysql_query("SELECT * FROM comments");

echo "<table border='1'>
<tr>
<th>Username</th>
<th>Comment</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['comment'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);

$con1=mysqli_connect("url","username","password","db-name");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
   $user_comment_name = $_POST['user_comment_name'];
   $user_comment_message = $_POST['user_comment_message'];
$sql1="INSERT INTO user_comment (user_comment_name, user_comment_message)
VALUES
('$user_comment_name','$user_comment_message')";

if (!mysqli_query($con1,$sql1))
  {
  die('Error: ' . mysqli_error($con1));
  }
echo "1 record added";

mysqli_close($con1);

?>



<form action="" method="post">
Name <input type="text" name="user_comment_name">
Message <input type="text" name="user_comment_message">
<input type="submit">
</form>

The weird thing is that when I put the second the mysqli connection into another file called insert.php (for example) and put into the then it works, but it directs me to a white page which I dont want. All help appreciated guys! Cheers :)

</div>
  • 写回答

2条回答 默认 最新

  • douyong1285 2013-12-06 08:19
    关注

    The notice error is originating from the fact that you are redirecting/viewing the page:

    $user_comment_name = $_POST['user_comment_name'];

    This line expects your page to be loaded when submitting values through POST.

    In order to validate whether this information is actually posted you should check:

    if( isset($_POST)) for instance

    Additionally (adding to @h2ooooooo's comment) be aware that you are inserting data from user submitted POST values directly into the database without sanitation. This said, anyone can hack your database with your current setup. Also try to move to prepared statements, available in mysqli_ functions and PDO, refrain from writing new code based on mysql_* function. They are considered bad practice and will be dropped from PHP all together in the near future.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳