i am trying to create a very simple comment form underneath images. i have the form set up, and i have some code. but when i go on to the image i get an unsigned variable and it will automatically write to the database and the comments box will be blank. when i type in the comments box and click submit the text does actually get written to the db but i need it to stop automatically writing that first time when you arrive on the page. could someone please help.
here is my code i have:
<?php
$dbLink = new mysqli('localhost', 'root', '', 'gallery');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
/* $_GET['id'] passes our test, so perform security functions on it & assign it to the $id variable */
$id= $dbLink->real_escape_string($_GET['id']);
} else {
/* it doesn't exist or it isn't numeric so assign a default value so our query below always gets constructed properly & safely */
$id = 1;
}
/* construct the query */
$query = "SELECT name, size, viewed_path, description FROM images WHERE id = $id";
/* run the query */
$result = $dbLink->query($query, MYSQLI_STORE_RESULT);
while($row = $result->fetch_array()) {
echo "<div id=\"image\">\n";
echo "<img src=\"" . $row['viewed_path'] . "\" alt=\"" . $row['name'] . "\" />\n";
echo "</div>\n";
echo '<p>' . $row['description'] . '</p>';
}
?>
<FORM METHOD="POST" action="">
<INPUT TYPE="text" NAME="name" SIZE="30">
<INPUT TYPE="text" NAME="surname" SIZE="30">
<textarea NAME="comment" ROWS=6 COLS=40></textarea>
<input type="submit" name="Comment" value="Comment">
</FORM>
<?php
$comments = $dbLink->real_escape_string($_POST['comment']);
$query = "INSERT INTO `image_comments`(`image_id`, `comments`) VALUES ('{$id}', '{$comments}')";
$results = $dbLink->query($query, MYSQLI_STORE_RESULT);
$query1 = "SELECT comments FROM image_comments WHERE id = $id";
$result = $dbLink->query($query1, MYSQLI_STORE_RESULT);
?>
this is the error i get:
Notice: Undefined index: comment in C:\wamp\www\Blean_Photos\image.php on line 77
but if i click submit i dont get that error after.