使用PHP / MySQL更新和插入问题:某些数据无法存储

时间:2015-06-16 03:10:36

标签: php mysql ajax

我遇到PHP代码问题,用户通过AJAX网页同时向MySQL数据库提交投票到两个单独的表中。

表:投票

场地:投票

表:民意调查

字段:多个

投票正确/完全插入投票表文件,但只有部分数据或部分投票正在更新到投票表>>>投票领域。

当我进行100票的测试时,他们会被存储在民意调查表中但是大约。 70%存储在投票表中?

插入和更新似乎有问题,因为当大量投票进入时,它们没有正确存储在投票表中。

我尝试增加MySQl的资源,但它没有修复它。我认为这个代码的更新增量部分存在问题吗?

(A)最差的数据量是多少? (B)当php获取帖子的那一刻你是否记录如果是这样的话。 (C)php可以暂时收集它而不立即处理它或者必须是用户交互显示结果。 (D)你使用的是mysql_ *吗? (E)你可以显示实际的mysql执行代码cuz我看到的是数组和字符串准备在一个高级别 - AsConfused 6月16日在5:00

A)经测试100,250&一分钟1000票。更高的容量会产生更大的错误,但通常较低的测试也会失败B)不知道C)必须立即收集并准备好为用户提供结果视图D)是 - 使用MySQL 5.6.22 E)是上面添加了 - electrolinks Jun 16 at 8: 58

/**
 * Vote
 * @since 1.1
 **/
protected function vote(){
    //Kill the Bots and validate request
    if(Main::bot()) die("Access Denied: We do not support bots, unfortunately.");
    if(!isset($_POST["poll_id"]) || !isset($_SERVER["HTTP_X_REQUESTED_WITH"]) || $_SERVER["HTTP_X_REQUESTED_WITH"]!=="XMLHttpRequest") die("Access Denied: Invalid request.");
    if(!isset($_POST["token"])) die("Access Denied: Token mismatch.");

$this->db->object=TRUE;
    $poll=$this->db->get("poll","BINARY `uniqueid`=?",array("limit"=>1),array($_POST["poll_id"]));
    $options=json_decode($poll->options,TRUE);
    $max=count($options);
    if(is_array($_POST["answer"])){
        $vote=implode(",",Main::clean($_POST["answer"]));
        foreach ($_POST["answer"] as $key => $value) {
            $options[$key]["count"]=$options[$key]["count"]+1;
        }
    }else{
        if(!is_numeric($_POST["answer"]) || $_POST["answer"]>$max) return FALSE;
        $options[$_POST["answer"]]["count"]=$options[$_POST["answer"]]["count"]+1;
        $vote=$_POST["answer"];
    }
    $options=json_encode($options);
    $update=array(
            ":votes"=>($poll->votes + 1),
            ":options"=>$options
        );
    if(isset($_POST["referrer"]) && !empty($_POST["referrer"])){
        $source=Main::clean($_POST["referrer"],3,TRUE);
    }else{
        $source="";
    }
    $insert=array(
            ":pollid" => $poll->id,
            ":polluserid" => $poll->userid,
            ":vote" => $vote,
            /*":ip" => Main::ip(),*/
            ":country" => $this->country(),
            ":source" => $source
        );
        if($this->db->insert("vote",$insert) && $this->db->update("poll","",array("id"=>$poll->id),$update)){
            if($poll->count=="month"){
                Main::cookie($poll->uniqueid,time(),60*60*24*30);
            }elseif ($poll->count=="day") {
                Main::cookie($poll->uniqueid,time(),60*60*24);
            }else{
                Main::cookie($poll->uniqueid,time(),60*60*24*365);
            }
            if(isset($_POST["embed"])){
                $this->results($poll->id,"",TRUE);
            }else{
                $this->results($poll->id);
            }
            return;
        }
    return;
}   
/**
* Update Query
* @since 1.0
**/
public function update($table,$field,$where,$param=array()){
    if(empty($field)){
        $field=$this->ph($param);
    }
    //Build Query
    $query="UPDATE {$this->dbinfo["prefix"]}$table SET ";
    if(is_array($field)){
        $count=count($field);
        $i=0;           
        foreach ($field as $key => $value) {
            if($value=="NOW()"){
                $query.="`$key`=$value";
            }else{
                $query.="`$key`=".$this->quote($value,$param);
            }                   
            if(++$i != $count) {
                $query.=",";
            }               
        }
    }else{
        $query.=$field;
    }
    if(is_array($where)){
        $count=count($where);
        $i=0;       
        $query.=" WHERE ";
        foreach ($where as $key => $value) {
            $query.="`$key`=".$this->quote($value,$param);
            if(++$i != $count) {
                $query.=" AND ";
            }               
        }
    }else{
        $query.=" WHERE $where";
    }       

    $result = $this->db->prepare($query);
    $result->execute($param);
    if($this->error_message($result->errorInfo())) {
        $this->query=strtr($query,$param);
        $this->db_error=$this->error_message($result->errorInfo());
        exit;
    }
    ++$this->num_queries;
return TRUE;    
}

架构是:

TABLE `vote` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pollid` mediumint(9) NOT NULL,
`polluserid` mediumint(9) NOT NULL DEFAULT '0',
`vote` varchar(255) NOT NULL,
`ip` varchar(255) NOT NULL,
`country` varchar(255) NOT NULL,
`source` text NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)

TABLE `poll` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` mediumint(9) NOT NULL DEFAULT '0',
`open` int(1) NOT NULL DEFAULT '1',
`question` text NOT NULL,
`options` text NOT NULL,
`results` int(1) NOT NULL DEFAULT '1',
`choice` int(1) NOT NULL DEFAULT '0',
`share` int(1) NOT NULL DEFAULT '1',
`pass` varchar(255) NOT NULL,
`theme` varchar(255) NOT NULL,
`custom` text NOT NULL,
`count` enum('day','month','off') DEFAULT 'off',
`created` datetime NOT NULL,
`expires` varchar(255) NOT NULL,
`uniqueid` varchar(8) NOT NULL,
`votes` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
 /** 
 * Insert to database
 * @since v1.0
 */
public function insert($table,$parameters=array()){
    $param="";
    $val="";
    $insert= $this->ph($parameters);
    //Build Query
    $query="INSERT INTO {$this->dbinfo["prefix"]}$table";                       
    if(is_array($insert)){
        $count=count($insert);
        $i=0;           
        foreach ($insert as $key => $value) {
            if($parameters[$value]=="NOW()"){
                $val.= "NOW()";
                unset($parameters[$value]);
            }else{
                $val.=$this->quote($value,$parameters);
            }                   
            $param.="`$key`";
            if(++$i != $count) {
                $param.=",";
                $val.=",";
            }               
        }
        $query.=" ($param) VALUES ($val)";
    }       
    $result = $this->db->prepare($query);
    $result->execute($parameters);
    if($this->error_message($result->errorInfo())) {
        $this->query=strtr($query,$parameters);
        $this->db_error=$this->error_message($result->errorInfo());
        exit;
    }
    ++$this->num_queries;
return TRUE;        
}
/**
 * Update Query
 * @since 1.0
 **/
public function update($table,$field,$where,$param=array()){
    if(empty($field)){
        $field=$this->ph($param);
    }
    //Build Query
    $query="UPDATE {$this->dbinfo["prefix"]}$table SET ";
    if(is_array($field)){
        $count=count($field);
        $i=0;           
        foreach ($field as $key => $value) {
            if($value=="NOW()"){
                $query.="`$key`=$value";
            }else{
                $query.="`$key`=".$this->quote($value,$param);
            }                   
            if(++$i != $count) {
                $query.=",";
            }               
        }
    }else{
        $query.=$field;
    }
    if(is_array($where)){
        $count=count($where);
        $i=0;       
        $query.=" WHERE ";
        foreach ($where as $key => $value) {
            $query.="`$key`=".$this->quote($value,$param);
            if(++$i != $count) {
                $query.=" AND ";
            }               
        }
    }else{
        $query.=" WHERE $where";
    }       

    $result = $this->db->prepare($query);
    $result->execute($param);
    if($this->error_message($result->errorInfo())) {
        $this->query=strtr($query,$param);
        $this->db_error=$this->error_message($result->errorInfo());
        exit;
    }
    ++$this->num_queries;
return TRUE;    
}

0 个答案:

没有答案