doueta6642 2010-04-19 20:23 采纳率: 0%
浏览 296

为什么MySQL INSERT INTO会运行两次?

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:

script output

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.

  • 写回答

3条回答 默认 最新

  • duanjiani6826 2010-04-19 20:27
    关注

    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?

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog