danielandlisa3144 Posted June 2, 2009 Share Posted June 2, 2009 Hi i i got a html form that posts to a database, it works but i get double posts. My blog im posting to displays one empty message with date together with the message posted. I wonder if there is any easy way to prevent this? I guess header location would work but i get the headers allready sent error. Im posting my code below for the page that handles the html form / thanks Lisa <?php include.mysqlconnect.php $name = kontrollera($_POST['name'], "you havent filled in your name, <a href=post.php>go back </a> "); $subject = kontrollera($_POST['subject'], "you havent filled in subject , <a href=post.php>go back</a> "); $message = check($_POST['message'], "you havent filled in message, <a href=post.php>go back</a> "); function check($data, $error='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); { die($error); } return $data; } $_SERVER['REMOTE_ADDR']; $ip=$_SERVER['REMOTE_ADDR'];; $strSQL = "INSERT INTO messages( ip) VALUES ('$ip')"; $test=mysql_query ($strSQL); $ip_address = $_SERVER['REMOTE_ADDR']; if (mysql_num_rows($result) < 1) { $sql = "INSERT INTO messages(name subject, message, ip) VALUES ('$_POST[name]','$_POST[subject]', '$_POST[message]', '$ip')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } $message = "Thankyou your message is sent" ; }else { $message = "go back.<br />"; /} echo $message; mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/160687-mysql-php-form-double-post/ Share on other sites More sharing options...
gevans Posted June 2, 2009 Share Posted June 2, 2009 A lot of that code is useless, and you're running 2 mysql queries that's what's causing the problem. I've played with the code bellow, have a read; <?php include.mysqlconnect.php $name = kontrollera($_POST['name'], "you havent filled in your name, <a href=post.php>go back </a> "); $subject = kontrollera($_POST['subject'], "you havent filled in subject , <a href=post.php>go back</a> "); $message = check($_POST['message'], "you havent filled in message, <a href=post.php>go back</a> "); function check($data, $error='') { //this whole function attempts to clean a string, not check the data $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); /*{ die($error); //this is doing nothing }*/ return $data; } //line not needed $ip=$_SERVER['REMOTE_ADDR']; //extra semi colon removed /*$strSQL = "INSERT INTO messages( ip) VALUES ('$ip')"; $test=mysql_query ($strSQL);*///not needed //line removed //if statmenet removed $sql = "INSERT INTO messages(name subject, message, ip) VALUES ('$_POST[name]','$_POST[subject]', '$_POST[message]', '$ip')"; if (!mysql_query($sql)) { die('Error: ' . mysql_error()); } else { $message = "Thankyou your message is sent" ; } echo $message; mysql_close($con) ?> Link to comment https://forums.phpfreaks.com/topic/160687-mysql-php-form-double-post/#findComment-848041 Share on other sites More sharing options...
danielandlisa3144 Posted June 2, 2009 Author Share Posted June 2, 2009 Thanks alot that worked. Link to comment https://forums.phpfreaks.com/topic/160687-mysql-php-form-double-post/#findComment-848062 Share on other sites More sharing options...
gevans Posted June 2, 2009 Share Posted June 2, 2009 It may have worked, but you still have a lot of work to do. Lines 4 - 18 are currently pointless in our implementation Link to comment https://forums.phpfreaks.com/topic/160687-mysql-php-form-double-post/#findComment-848085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.