我正在尝试使用下面的mysql insert语句将信息添加到数据库表中。但是,当我执行脚本时,insert语句会运行两次。为什么这个MySQL INSERT INTO运行两次?
这里的URL提前mysite.com/save.php?Body=p220,c180
感谢。
<?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();
}
?>
你们大多数人一样,我想这是因为我$ i开始= 0,所以我增加了$ 1 + $法官在插入语句,所以我可以看到物品是如何被插入到表。下面是运行该脚本返回:
我敢肯定有一个在我的逻辑错误。我似乎无法弄清楚它是什么。
我跑的LiveHTTPHeaders如下建议,这是什么止跌回升......
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
----------------------------------------------------------
原来,当我在Chrome加载save.php错误只发生。
循环开始之前'$ j'的值是什么? – FrustratedWithFormsDesigner 2010-04-19 20:24:38
在循环之前它被设置为$消息的计数。它就在那里;) – naugtur 2010-04-19 20:53:52
如果你看看你的数据的顺序,循环正在执行两次。 – 37Stars 2010-04-20 14:16:56