1

Hi there guys I'm just having an issue which I don't know how to get around, it's telling me I have an 'Undefined Index" for the following:

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

But it's clearly listed in my DB: http://puu.sh/cAlHz/56afbd7e80.png

The comment features work and everything like that but it's showing me this error. Any idea?

<?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"/>'
;}
?>
Community
  • 1
  • 1
Sarah
  • 41
  • 5
  • What are lines 113 and 114 ? – Luc M Nov 02 '14 at 16:06
  • @LucM $name=$_POST['name']; $comment=$_POST['comment']; – Sarah Nov 02 '14 at 16:07
  • You may want to check if the $_POST variables are set before assigning them. `if(isset($_POST['name'])) $name=$_POST['name'];` – Tom Hoang Nov 02 '14 at 16:09
  • 2
    @Sarah that means that it's not a database error. It's saying that "name" and "comment" are undefined indices of the $_POST array. So you're not passing name/comment to the page through a POST payload. – TonyArra Nov 02 '14 at 16:11
  • Your conditional statements are incorrect. `if($submit) { if($name&&$comment)` and in the wrong place. – Funk Forty Niner Nov 02 '14 at 16:19

1 Answers1

1

This code block: (also consult my footnotes concerning the undefined index warnings)

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

Can and should be replaced by isset() and empty() while wrapping the braces around the code execution.

<?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";
}

However you are using a deprecated library.

Plus, your code is open to SQL injection.
Use mysqli_* with prepared statements, or PDO with prepared statements.


or:

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";
    }

}

Footnotes:

It looks to me as if you are running your entire code from within the same page, which is why you are getting the undefined index warnings as soon as the page is loaded.

Also, just a suggestion: You're using <form action="service.php" method="POST"> which can be changed to <form action="" method="POST">

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141