...

View Full Version : INSERT INTO multiple times should be once



Jenny Dithe
12-07-2010, 09:03 AM
Hi,

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

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?

abduraooft
12-07-2010, 09:09 AM
My INSERT INTO is inserting three times in to my db, and I am not sure why? Did you mean to the same table purchases?

Jenny Dithe
12-07-2010, 10:36 AM
Yes.

This part inserts three times:

$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());

abduraooft
12-07-2010, 11:34 AM
function check_input($value){ Are you calling your function inside a loop or something like?

Jenny Dithe
12-07-2010, 12:48 PM
No loop, the only thing on that page that I have omitted is the session information.

My code in my main page is:


<script type="text/javascript">
function loadXMLDoc(File,ID,Msg){
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}

xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(ID).innerHTML=xmlhttp.responseText;
}
}

var params=Msg;
xmlhttp.open("POST",File,true);

xmlhttp.setRequestHeader("Pragma", "Cache-Control:no-cache");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}

</script>
</head>
<body>
<label class="two">Country:</label>
<form action="" method="post">
<select name="country" class="input" required="required">
<option value="please select"
selected"selected">Please Select</option>
<option value="AF|" >Afghanistan</option>
//country list
<option value="ZW|" >Zimbabwe</option>

</select>
&nbsp; &nbsp;
<input type="button" value="Update" onclick="loadXMLDoc('update.php','txtHint','country='+encodeURI(document.getElementById('country').value))"; />
</form>
&nbsp; &nbsp;
<span id="txtHint"></span>

(update.php is the code posted in my first post).

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.


$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());



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum