我正在嘗試使用下面的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