sherwoodbear79 Posted December 13, 2010 Share Posted December 13, 2010 Hi All, Hope you can help. I am doing a very basic guestbook. Having problems with the posting method. The code is below all I am doing is insert a record into a mysql table but when I post getting a blank page with no obvious errors! code is below. if ($_POST['submit']) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $date = $_POST("Y-m-d"); $time = $_POST("H:i:s"); if($name&&$email&&$message) { $querypost = mysql_query("INSERT INTO guestbook (id, name, email, message, date, time) VALUES ('','$name','$email','$message','$date','$time')"); echo "Please wait.... <meta http-equiv='refresh' content='2'>"; } else echo "Please fill out all fields."; } Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/ Share on other sites More sharing options...
MMDE Posted December 13, 2010 Share Posted December 13, 2010 Now, I don't know your html form, but I kinda fixed up a bit on your php... I'm not too happy about some of the input names either, try to just use letters. if(isset($_POST['submit'])){ $formfilled=0; $formsforgot=array(); if(!empty($_POST['name'])){ $name = $_POST['name']; $formfilled++; }else{ $formsforgot[]='name'; } if(!empty($_POST['email'])){ $email = $_POST['email']; $formfilled++; }else{ $formsforgot[]='email'; } if(!empty($_POST['message'])){ $message = $_POST['message']; $formfilled++; }else{ $formsforgot[]='message'; } if(!empty($_POST['Y-m-d'])){ $date = $_POST['Y-m-d']; $formfilled++; }else{ $formsforgot[]='Y-m-d'; } if(!empty($_POST['H:i:s'])){ $time = $_POST['H:i:s']; $formfilled++; }else{ $formsforgot[]='H:i:s'; } if($formfilled==5){ mysql_query("INSERT INTO guestbook (id, name, email, message, date, time) VALUES ('','$name','$email','$message','$date','$time')") or die(mysql_error()); echo "Please wait.... <meta http-equiv='refresh' content='2'>"; }else{ echo "Please fill out all fields."; foreach($formsforgot AS $forgot){ echo '<br />You forgot to fill out '.$forgot; } } } Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/#findComment-1146621 Share on other sites More sharing options...
MMDE Posted December 13, 2010 Share Posted December 13, 2010 if(isset($_POST['submit'])){ if(!empty($_POST['name'])&&!empty($_POST['email'])&&!empty($_POST['message'])&&!empty($_POST['Y-m-d'])&&!empty($_POST['H:i:s'])){ // mysql_real_escape_string to at least spare you from some trouble... $query="INSERT INTO guestbook (id, name, email, message, date, time) VALUES ('','".mysql_real_escape_string($_POST['name'])."','".mysql_real_escape_string($_POST['email'])."','".mysql_real_escape_string($_POST['message'])."','".mysql_real_escape_string($_POST['Y-m-d'])."','".mysql_real_escape_string($_POST['H:i:s'])."')"; echo $query; // just for testing purposes... mysql_query($query) or die(mysql_error()); echo "Please wait.... <meta http-equiv='refresh' content='2'>"; }else{ echo "Please fill out all fields."; } } I still want to see the html code, and also, you need to connect to the mysql server and select database before the mysql query... also, the meta tag should be in the header of the html code... Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/#findComment-1146627 Share on other sites More sharing options...
sherwoodbear79 Posted December 13, 2010 Author Share Posted December 13, 2010 Full post just trying your solutions! much appreciated! <? echo "<h1> Make a Post</h1>"; echo "<hr>"; if ($_POST['submit']) { $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $date = $_POST("Y-m-d"); $time = $_POST("H:i:s"); if($name&&$email&&$message) { $querypost = mysql_query("INSERT INTO guestbook (id, name, email, message, date, time) VALUES ('','$name','$email','$message','$date','$time')"); echo "Please wait.... <meta http-equiv='refresh' content='2'>"; } else echo "Please fill out all fields."; } //connect require("connectegamerdb.php");//connecting to sql //get all data entries $queryget = mysql_query("SELECT * FROM guestbook") or die ("error"); while($row = mysql_fetch_assoc($queryget)) { //get and store var $id = $row['id']; $name = $row['name']; $email = $row['email']; $message = $row['message']; $date = $row['date']; $time = $row['time']; //echo out echo " <table> <tr> <td> <b>Posted by $name ($email) on $date at $time.</b> </td> </tr> <tr> <td> ".nl2br(strip_tags($message))." </td> </tr> </table> "; } echo "<hr>"; echo " <form action='guestbook.php' method='POST'> <table width='100'> <tr> <td width = '20%'> Your Name: </td> <td> <input type='text' name='name' maxlength='25'> </td> </tr> <tr> <td> Your email: </td> <td> <input type='text' name='email' maxlength='35'> </td> </tr> <tr> <td valign = 'top'> Your message: </td> <td> <textarea cols='20' rows='2' name='message' maxlength='250'></textarea> <p><input type ='submit' name='submit' value='Post'> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/#findComment-1146631 Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 If id is a PRIMARY KEY and AUTO INCREMENTED You don't need to put this in your insert query. MySQL can handle this field, this is probably why your getting a white page. $query="INSERT INTO guestbook (name, email, message, date, time) VALUES (".mysql_real_escape_string($_POST['name'])."','".mysql_real_escape_string($_POST['email'])."','".mysql_real_escape_string($_POST['message'])."','".mysql_real_escape_string($_POST['Y-m-d'])."','".mysql_real_escape_string($_POST['H:i:s'])."')"; echo $query; // just for testing purposes... mysql_query($query) or die(mysql_error()); and as MMDE shows, always sanitize user input before inserting into your db. Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/#findComment-1146633 Share on other sites More sharing options...
sherwoodbear79 Posted December 13, 2010 Author Share Posted December 13, 2010 still getting blank page on refresh. not sure whats happening thanks for all the solutions offered! any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/#findComment-1146636 Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 where do u do a mysql_connect? Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/#findComment-1146649 Share on other sites More sharing options...
sherwoodbear79 Posted December 13, 2010 Author Share Posted December 13, 2010 just after the insert should it be above it? Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/#findComment-1146653 Share on other sites More sharing options...
MMDE Posted December 13, 2010 Share Posted December 13, 2010 Also, you should see the query and what is wrong with the mysql query insert if you used what I wrote... and it would be safer... mysql_connect($mysqlhost,$mysqlusername,$mysqlpassword); mysql_select_db($mysqldatabase) or die($mysqlerror); Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/#findComment-1146655 Share on other sites More sharing options...
sherwoodbear79 Posted December 13, 2010 Author Share Posted December 13, 2010 Hi all, must have been the way I structured it. Just looked over a tutorial, and did it in 10 mins and is now working????!?!?! guess thats why I am called a learner. Many thanks for all replies!! cheers Quote Link to comment https://forums.phpfreaks.com/topic/221503-guestbook-problem-please-help/#findComment-1146675 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.