我是 php 的新手,正在开发一个网络服务来增加 iphone 端的 itemQuantity。当用户多次点击 + 按钮来增加一个项目的数量时,有时它并没有准确地增加数量。 iphone 每次都给我发送更多的数量。我的意思是 26 然后 27 然后 28。
但是当我尝试调试它时,我发现有一段时间它首先使用 28 为数据库提供服务,然后将其更新为 27 。有人可以帮忙吗?我正在使用的功能如下....
function update_query($tblName,$queryArr,$condition)
{
if(!empty($this->_database)){
if(!mysql_select_db($this->_database, $this->_link) )
{
$this->_errorstr = "Couldn't change database: ".$this->_database."
My-SQL Error ".mysql_error($this->_link);
}
}
$fields=@array_keys($queryArr);
$values=@array_values($queryArr);
$update="";
for($i=0;$i<count($values);$i++)
{
$update.="$fields[$i]='".addslashes($values[$i])."',";
}
$update=substr($update,0,-1);
$query="update $tblName set $update $condition";
// echo $query
//echo $query."<br>";
//die;
$this->_errorstr = "";
$this->_query = $query;
$this->_result = mysql_query( $this->_query, $this->_link );
if ( ! $this->_result )
{
$this->_errorstr = "Error : ".mysql_error( $this->_link );
$this->_success = false;
}
else
{
$this->_success = true;
}
return $this->_success;
}
最佳答案
如果两个请求同时到达您的应用程序并且都从数据库中读取相同的值会怎样?
在您的 mysql 查询中使用 SET quantity = quantity + 1
。
这并不能解决你的iphone应用程序中的显示问题,但至少,用户命令已经存储在数据中了。
要解决显示问题,您需要更改应用程序的设计。例如,在客户端增加,稍后存储。
关于php - 先到先得 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7713582/