tags:

views:

196

answers:

4

I'm attempting to use the mysql insert statement below to add information to a database table. When I execute the script, however, the insert statement is run twice.

Here's the URL mysite.com/save.php?Body=p220,c180

Thanks in advance.

<?php
//tipping fees application
require('base.inc.php');
require('functions.inc.php');

// connect to the database & save this message there

try {
    $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);


//$number = formatPhone($_REQUEST['From']);
//if($number != 'xxx-xxx-xxxx'){die('SMS from unknown number');} // kill this if from anyone but mike

$message = $_REQUEST['Body'];
//$Sid = $_REQUEST['SmsSid'];
$now = time();
echo $message;

$message = explode(",",$message);
echo '<pre>';
print_r($message);
echo 'message count = '.count($message);
echo '</pre>';

$i = 0;
$j = count($message);

while($i<$j){
    $quantity =$message[$i];
    $material = substr($quantity, 0, 1);
    $amount = substr($quantity, 1);

    switch ($material) {
        case 'p':
            $m = "paper";
            break;
        case 'c':
            $m = "containers";
            break;
        default:
            $m = "other";
        }

        $count = $dbh->exec("INSERT INTO tippingtotals(sid,time,material,weight) VALUES('$i+$j','$now','$m','$amount')");
        echo $count;
        echo '<br />';

    $i++;
    }



//close the database connection 
    $dbh = null;
    }
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>

Like most of you, I thought it was because I was starting $i=0 so I added the $i+$j in the insert statement so I could see how items were being inserted into the table. Here's what running the script returns.

http://cl.ly/63I

I'm certain there's an error in my logic. I just can't seem to figure out what it is.

I ran LiveHTTPHeaders as suggested below and this is what turned up...

http://localhost/mysite/save.php?Body=p180,c220

GET /mysite/save.php?Body=p180,c220 HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive

HTTP/1.1 200 OK
Date: Mon, 19 Apr 2010 22:17:31 GMT
Server: Apache/2.0.63 (Unix) PHP/5.2.11 DAV/2
X-Powered-By: PHP/5.2.11
Content-Length: 93
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html
----------------------------------------------------------

Turns out that the error is only happening when I load save.php in Chrome.

+4  A: 

Without a doubt your condition

while($i<$j){

is getting evalueated such that it executes twice.

That is - it gets evaluated as

 while(0<2)

Isn't it?

+4  A: 

Your $j variable, the result of count($message) is going to be 2, as explode(',',"p220,c180") will give you a two-element array.

Since you're starting with $i = 0, and incrementing it by one on each loop, your loop will execute twice before $i < $j is no longer a true statement.

zombat
I thought the same thing, that's why I put the $i+$j in the insert statement so I could see how items were being inserted into the table. Here's what running the script returns.http://cl.ly/63II'm certain there's an error in my logic. I just can't seem to figure out what it is.
Ryan
That's exactly the output I would expect from your code as it currently stands. On the first loop, `$i` will be 0 and `$j` will be 2, and on the second loop, `$i` will be 1 and `$j` will still be 2. It's unclear what you are expecting to happen though... if you only want to run the INSERT once, why is it inside a loop?
zombat
I need to increment to the second item in the message array. It's obvious I'm having a mental block. If you don't want to keep trying to push this through my dense head, feel free to abandon ship!Here's what I'm expecting...http://cl.ly/9oW
Ryan
I think I see what you're getting at. Judging by the output comparison though, it's not just the `INSERT` that's being run twice though, it's the whole script. Your first output link shows that `$i` gets reset to zero. Are you sure the whole thing isn't being run twice?
zombat
That's what I thought! I can't figure out why it would be running twice. I've removed all non-essential includes, reset mysql and apache with no luck.
Ryan
Maybe check that there are no redirects occurring? Try the Firefox extension LiveHTTPHeaders and capture the output when you request that page. I definitely think it's a problem with the script being run twice because of that reset of `$i` to `0`.
zombat
Ran the page in Firefox and it works like a charm. The error is only happening in Chrome. Strange.
Ryan
Good old Chrome, saddling us hard-working web developers with another browser to support. ;) Glad you found it... sorry that it's a browser specific issue! You'll have to spend some quality time debugging the request flow with Chrome.
zombat
+1  A: 

Here it is:

$i is 0 and $j is initialized with number of elements in the array $message. This array is created by using explode(',', $var) on a string. This string, contained in $_GET['Body'], is p220,c180. So the array has two elements, thus $j = 2.

The while loop will get executed twice before $i >= $j ($i >= 2).

Felix Kling
A: 

It looks save.php is being called twice. What does the code that calls this page look like?

37Stars
I'm calling the page directly for testing.
Ryan