alexguz79 Posted June 7, 2010 Share Posted June 7, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/204120-problem-with-comment-system/ Share on other sites More sharing options...
steveangelis Posted June 7, 2010 Share Posted June 7, 2010 $sql="INSERT INTO images_comments (id, image_id, name, comment) VALUES ('','$id','$_POST[name]','$_POST[comment]')"; That there is inserting the blank line into your database. Your form needs to be submitted BEFORE you insert this line. Quote Link to comment https://forums.phpfreaks.com/topic/204120-problem-with-comment-system/#findComment-1069087 Share on other sites More sharing options...
sspoke Posted June 7, 2010 Share Posted June 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/204120-problem-with-comment-system/#findComment-1069089 Share on other sites More sharing options...
alexguz79 Posted June 7, 2010 Author Share Posted June 7, 2010 steveangelis not sure if i understand what your are saying... what am idoing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/204120-problem-with-comment-system/#findComment-1069098 Share on other sites More sharing options...
steveangelis Posted June 7, 2010 Share Posted June 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204120-problem-with-comment-system/#findComment-1069100 Share on other sites More sharing options...
ignace Posted June 7, 2010 Share Posted June 7, 2010 $_POST[name] equal to nothing To be more precise as the index 'name' has not yet been defined (form has not yet been submitted) this returns NULL converted to a string equals '' Quote Link to comment https://forums.phpfreaks.com/topic/204120-problem-with-comment-system/#findComment-1069139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.