开发者

Why is this MySQL INSERT INTO running twice?

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']);
//i开发者_运维问答f($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:

Why is this MySQL INSERT INTO running twice?

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.


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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜