Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2010, 08:03 AM   PM User | #1
Jenny Dithe
Regular Coder

 
Join Date: Sep 2010
Posts: 457
Thanks: 212
Thanked 1 Time in 1 Post
Jenny Dithe is on a distinguished road
INSERT INTO multiple times should be once

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 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?
Jenny Dithe is offline   Reply With Quote
Old 12-07-2010, 08:09 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
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?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 12-07-2010, 09:36 AM   PM User | #3
Jenny Dithe
Regular Coder

 
Join Date: Sep 2010
Posts: 457
Thanks: 212
Thanked 1 Time in 1 Post
Jenny Dithe is on a distinguished road
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());
Jenny Dithe is offline   Reply With Quote
Old 12-07-2010, 10:34 AM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
function check_input($value){
Are you calling your function inside a loop or something like?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 12-07-2010, 11:48 AM   PM User | #5
Jenny Dithe
Regular Coder

 
Join Date: Sep 2010
Posts: 457
Thanks: 212
Thanked 1 Time in 1 Post
Jenny Dithe is on a distinguished road
No loop, the only thing on that page that I have omitted is the session information.

My code in my main page is:
Code:
<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.
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()); 
Jenny Dithe is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:45 PM.


Advertisement
Log in to turn off these ads.