I know I have asked about multiple updates before but this seems to be due to a different issue.
My INSERT INTO is inserting three times in to my db, and I am not sure why?
My Code is:
PHP Code:
<?php
include("dbconnect.php");
function check_input($value){
if (get_magic_quotes_gpc()){
$value=stripslashes($value);
}
if (!is_numeric($value)){
$value = mysql_real_escape_string($value);
}
return $value;
}
foreach ($_POST as $key=>$value){
$_POST[$key]= check_input($value);
}
$sql="UPDATE store SET country ='{$_POST['country']}'
WHERE PId={$_SESSION['Id']}";
echo $sql . '<br />';
mysql_query($sql,$con) or die('Error: ' . mysql_error());
$sql2="INSERT INTO purchases (Id, fullname, pphoto, category, subcategory, body, date)
VALUES
('{$_SESSION['Id']}','{$_SESSION['fullname']}','{$_SESSION['pphoto']}','country','{$_POST['country']}',' ordered from ',NOW())";
echo $sql2 . '<br />';
mysql_query($sql2,$con) or die('Error: ' . mysql_error());
if($_SESSION['country']=='online purchase'){
$sql3="UPDATE countryepurchases SET {$_POST['ccountry']}=='{$_POST['ccountry']}'
WHERE PId={$_SESSION['Id']}";
echo $sql3 . '<br />';
mysql_query($sql3,$con) or die('Error: ' . mysql_error());
}
?>
Now I worked out that there are three
mysql_query($sql3,$con) or die('Error: ' . mysql_error()); which is probably the connection, I have changed my $sql, so they are $sql, $sql2, $sql3 so the same naming can't be a problem.
I originall had
$result=mysql_query($sql) or die('Error: ' . mysql_error());
but I changed that to
mysql_query($sql/2/3,$con) or die('Error: ' . mysql_error()); in case the three $results were creating the same issue.
I know the problem is probably very simple, but I can't work it out?
And everything is echoing out correctly, e.g. only once?
As you can see this has it's own separate form from any other information.
I do have other parts of the page refreshing but they are contained.
The only possible exception is that in my main page I have information in the session refreshing, but that doesn't include any of the information I use to submit. By refreshing I mean I use setInterval.
The other thing that might help, is that originally I made the mistake of having mysql_query($sql3,$con) or die('Error: ' . mysql_error()); out of the brackets, as below, and when I did that it was inserting the information four times.
PHP Code:
$sql="UPDATE store SET country ='{$_POST['country']}' WHERE PId={$_SESSION['Id']}";
echo $sql . '<br />';
mysql_query($sql,$con) or die('Error: ' . mysql_error());
$sql2="INSERT INTO purchases (Id, fullname, pphoto, category, subcategory, body, date) VALUES ('{$_SESSION['Id']}','{$_SESSION['fullname']}','{$_SESSION['pphoto']}','country','{$_POST['country']}',' ordered from ',NOW())"; echo $sql2 . '<br />'; mysql_query($sql2,$con) or die('Error: ' . mysql_error());
if($_SESSION['country']=='online purchase'){ $sql3="UPDATE countryepurchases SET {$_POST['ccountry']}=='{$_POST['ccountry']}' WHERE PId={$_SESSION['Id']}"; echo $sql3 . '<br />'; } mysql_query($sql3,$con) or die('Error: ' . mysql_error());