-4

I was trying to build a simple comment section in my website. I created two fields (or boxes) with Name and Comment. Now if I fill up the Name and Comment fields and hit Submit it shows "Webpage isn't found" error (See the attachment). Besides, nothing is updating into my database.

[NOTE: After all my local server is properly installed and everything else works fine. I worked with my phpMyAdmin before and that time it was able to derive data from the database. WAMP server is also running. I am following this tutorial and code.]

main.php:

 <!--php code for comment section starts--> 
 <?php
  require ('connect.inc.php');
  $name=$_POST['name'];
  $comment=$_POST['comment'];
  $submit=$_POST['submit'];

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

  $insert=mysql_query("INSERT INTO comment(name, comment) VALUES ('$name','$comment')"); 
  }else{
  echo "Please fill all the fields";
 }

  }

 ?>
<!--php code for comment section ends--> 


<!--building a comment section starts-->
<form action="main.php method="POST">

<table>
<tr><td>Name: </td><td><input type="text" name="name"/> </td> </tr>
<tr><td colspan="2">Comment: </td></tr>
<tr><td colspan="2"><textarea name="comment"></textarea></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Comment"/> </td></tr>

</table>
</form>
<!--building a comment section ends-->

connect.inc.php:

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

1 Answers1

2

You forgot a quote in action="main.php <=

change it to action="main.php"

that's why you're getting a Webpage isn't found error.


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


Add error reporting to the top of your file(s), immediately following your opening <?php tag.

error_reporting(E_ALL);
ini_set('display_errors', 1);

which will help signal errors found in code.

and or die(mysql_error()) to mysql_query()


Sidenote:

Now, if that still throws an error of file not found, keep in mind that main.php is not the same as Main.php on certain servers, so make sure the filename is in fact named main.php all in lower-case; this is the same as self.

You can also use <form action="" method="POST"> since your entire code is inside the same page. This will set the action as the same page you are executing the code from.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • hey your answer helps. it doesn't show any display error now. the only thing is that it can't add the data into database, which means updating comment and name doesn't work still. – Jabir AL Fatah Aug 25 '14 at 22:43
  • @JabirALFatah Did you add error reporting to the top of your PHP? – Funk Forty Niner Aug 25 '14 at 22:44
  • yes I did it. Now there is no "webpage not found error". but no data is updated in the database. – Jabir AL Fatah Aug 25 '14 at 22:49
  • @JabirALFatah Your table is in fact called `comment` and you have 2 columns named `name` and `comment`? – Funk Forty Niner Aug 25 '14 at 22:51
  • @JabirALFatah MySQL is installed and running? I've tested this just now with no problems. – Funk Forty Niner Aug 25 '14 at 22:56
  • This is how my database looks like: https://plus.google.com/u/0/photos/100402704740320621129/albums/6051641033634649121/6051641040684243970?pid=6051641040684243970&oid=100402704740320621129 – Jabir AL Fatah Aug 25 '14 at 22:58
  • yes mysql is running fine. – Jabir AL Fatah Aug 25 '14 at 22:59
  • by the way, is it the right way to add error report code into the file(s): I added the error report code both in my main.php and connect.inc.php file in the same way. – Jabir AL Fatah Aug 25 '14 at 23:01
  • @JabirALFatah Yes, that is how to do it. Try adding `if (!$insert) { die('Invalid query: ' . mysql_error()); }` after your query. Plus, try increasing your column VARCHAR, and setting your `comment` column as VARCHAR(255) also. Or change the name of it, it might be conflicting with the table name. – Funk Forty Niner Aug 25 '14 at 23:03
  • @JabirALFatah Also look at the example #1 on http://php.net/manual/en/function.mysql-select-db.php and use that syntax, see if it throws any errors. Complete with variables. `$link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Not connected : ' . mysql_error()); } // make foo the current db $db_selected = mysql_select_db('foo', $link); if (!$db_selected) { die ('Can\'t use foo : ' . mysql_error()); }` – Funk Forty Niner Aug 25 '14 at 23:05
  • thanks for all you have done for me. Its too late at night in my country. I will be back to you later. – Jabir AL Fatah Aug 25 '14 at 23:13
  • @JabirALFatah You're welcome. I setup a DB myself and everything worked fine, using your code exactly as shown, therefore am unable to know what the problem is on your end. – Funk Forty Niner Aug 25 '14 at 23:18