Jump to content

Problem with comment system


alexguz79

Recommended Posts

Hey everybody...

 

i have this problem:

 

i have my comment system working in terms of adding and displaying the data... but there's this thing that everytime the page loads insert a new blank field in the database... here's the code... thank in advance

 

<form action="<?=$PHP_SELF?>" method="post">
          Nombre:<br /> 
          <input type="text" name="name" id="name" />
          <br />
          Commentario:<br /> 
          <input type="text" name="comment" id="comment" />
          <br />
          <input type="submit" value="Enviar" /></form>
	  
	  <?php

	  mysql_connect("localhost", "root", "root") or die(mysql_error());
	  mysql_select_db("gallery") or die(mysql_error());
	  $id    = $_GET['id'];
	  

	  $result2 = mysql_query("SELECT * FROM images_comments WHERE image_id = '$id' ORDER BY ID DESC");
	  $sql="INSERT INTO images_comments (id, image_id, name, comment)
	  VALUES ('','$id','$_POST[name]','$_POST[comment]')";
	  
	  !mysql_query($sql);
	   
	  ?>
	   

Link to comment
https://forums.phpfreaks.com/topic/204120-problem-with-comment-system/
Share on other sites

you should never put primary key into query! mysql handles primary keys without php's help remove id from query completely

 


  $sql="INSERT INTO images_comments (id, image_id, name, comment)
	  VALUES ('','$id','$_POST[name]','$_POST[comment]')";

 

to


  $sql="INSERT INTO images_comments (image_id, name, comment)
	  VALUES ('$id','$_POST[name]','$_POST[comment]')";

why do people always do the blank thing in primary keys??

 

 

not to mention your

!mysql_query($sql); has a Exclamation mark

 

you also have to check for

if(isset($_POST['submit']))  //before you run it all code with mysql

Your code is currently inputting blank lines into your database because it has no variables to deal with.  You are calling up variables such as $_POST[name] before the form is submitted, which is making $_POST[name] equal to nothing, hence why the entries are being made blank. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.