Jump to content

mysql php form double post


Recommended Posts

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

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)               
?>

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.