做问答系统是对题目修改的bug

没有有BUG的代码

else if('add' == $key){
            //获取问卷名字
            $id = $this->get('wjid');
            $wjname = $this->getWants('Spectab','id',$id,'specname');
            $this->assign('wjname',$wjname);
        }else if('edit' == $key){
            $id = $this->get('wjid');
            $wjname = $this->getWants('Spectab','id',$id,'specname');
            $id = $this->get('id');
            //查出题目信息
            $res_que = $model_que->getOne('id = ?',$id);
            //查出题目答案信息
            $res_ans = $model_ans->getAll('qid = ?',$id);
            $arr_index = array('wjname' => $wjname, 'res_que' => $res_que ,'res_ans' => $res_ans);
            $this->assign($arr_index);
        }else if('editdata' == $key){
            //修改
            $topicname = $this->post('topicname');
            $id = $this->post('id');
            $orderid = $this->post('orderid');
            $intype = $this->post('intype');
            $inclass = $this->post('inclass');
            //先更新allquestion 表
            $updateArray_que = array(
                'topicname' =>$topicname,'orderid' => $orderid,'intype' => $intype,
                'inclass' => $inclass
            );
            $model_que->update($updateArray_que,'id = ?',$id);
            //判断是否答案项数量改变
            $count = $this->post('count');
            //查询出该题目下有几个答案
            $s_counts =  $model_ans->getAll('qid = ?',$id,'id');
            $s_count = count($s_counts);
            //获取record
            $record = $this->post('record');//拆分次字段来拼装post中的KEY 来取值 组装insert语句
            $record = substr($record,0,-1);
            //拆分
            $record_arr = explode('|',$record);
            /*print_r($s_counts);
            print_r($record_arr);*/
            //处理counts 变成一维数组 当前选项
            $macth_arr = null;

            foreach($s_counts as $key => $val){
                $macth_arr[] = $val['id'];
            }
            $column_que_totle = count($column_que);
            //应该拿俩数组对比
            $diff = array_merge(array_diff($record_arr,$macth_arr),array_diff($macth_arr,$record_arr));
            //如果$diff为空 则只进行update 如果不为空 判断是单个的键值属于哪个数组 如果是match则要做删除  如果是record 则要insert 最后得出 增加和减少的数组 执行
            if(!empty($diff)){
                $arr_plus = null;
                $arr_reduce = null;
                foreach($diff as $key => $val){
                    //判断属于哪个数组
                    if(in_array($val,$macth_arr)){
                         //如果是属于这个数组,则放在arr_reduce 反之则相反
                        $arr_reduce[] = $val;
                    }else{
                        $arr_plus[] = $val;
                    }
                }
                if(!empty($arr_reduce)){
                    $delete_sql_str = implode(',',$arr_reduce);
                    $sql = "delete from allscore where id in ($delete_sql_str)";
                    $model_ans->execute($sql);
                }
                if(!empty($arr_plus)){
                    $sql = 'insert into allscore (`qid`,`xuanxiang`,`xvalue`,`ztai`,`fengxian`,`jianyi`) values ';
                    //得到需要新添加的俩项
                    //拼装sql
                    $arr_reduce_totle = count($arr_plus);
                    foreach($arr_plus as $key => $value){
                        //到了最后一项的时候
                        if($key+1 == $arr_reduce_totle){
                            $temp = "('".$id."',";
                            foreach($column_que as $key => $val){
                                if($column_que_totle - 1 == $key){
                                    $temp .= "'".$_POST[$val.$value]."'";
                                }else{
                                    $temp .= "'".$_POST[$val.$value]."',";
                                }
                            }

                            $sql .= $temp.")";
                        }else{
                            $temp = "('".$id."',";
                            foreach($column_que as $key => $val){
                                if($column_que_totle - 1 == $key){
                                    $temp .= "'".$_POST[$val.$value]."'";
                                }else{
                                    $temp .= "'".$_POST[$val.$value]."',";
                                }
                            }
                            $sql .= $temp."),";
                        }
                    }
                    //bingo
                    $model_ans->execute($sql);
                }
            }
                foreach($macth_arr as $key => $val){
                    $updateSql = 'UPDATE allscore set';
                    $tempStr = null;
                    foreach($column_que as $key_1 => $val_1){
                        if($column_que_totle == $key_1+1){
                            $tempStr .= "$arr_update_ans[$key_1] ="."'".$_POST[$val_1.$val]."'";
                        }else{
                            $tempStr .= " $arr_update_ans[$key_1] ="."'".$_POST[$val_1.$val]."',";
                        }
                    }
                    $updateSql .= $tempStr." where `id` = $val ";
                    $model_ans->execute($updateSql);

                 }
            echo 'yes';
            exit;
        }

有bug的代码

if('editdata' == $key){
            //修改
            $topicname = $this->post('topicname');
            $id = $this->post('id');
            $orderid = $this->post('orderid');
            $intype = $this->post('intype');
            $inclass = $this->post('inclass');
            //先更新allquestion 表
            $updateArray_que = array(
                'topicname' =>$topicname,'orderid' => $orderid,'intype' => $intype,
                'inclass' => $inclass
            );
            $model_que->update($updateArray_que,'id = ?',$id);
            //判断是否答案项数量改变
            $count = $this->post('count');
            //查询出该题目下有几个答案
            $s_counts =  $model_ans->getAll('qid = ?',$id,'id');
            $s_count = count($s_counts);
            //获取record
            $record = $this->post('record');//拆分次字段来拼装post中的KEY 来取值 组装insert语句
            $record = substr($record,0,-1);
            //拆分
            $record_arr = explode('|',$record);
            /*print_r($s_counts);
            print_r($record_arr);*/
            //处理counts 变成一维数组 当前选项
            $macth_arr = null;

            foreach($s_counts as $key => $val){
                $macth_arr[] = $val['id'];
            }

$arr_update_arr = null; $column_que_totle = count($column_que); //不应该是从数量上单纯判断 if($count != $s_count){ $arr_plus = null; if($count - $s_count > 0 ){ $sql = 'insert into allscore (`qid`,`xuanxiang`,`xvalue`,`ztai`,`fengxian`,`jianyi`) values '; //说明增加了选项 要进行insert操作 foreach($record_arr as $key => $val){ if(!in_array($val,$macth_arr)){ //当不在这个数据库的时候 说明需要新增 记录下ID $arr_plus[] = $val; }else{ $arr_update_arr[] = $val; } } //得到需要新添加的俩项 //拼装sql $arr_reduce_totle = count($arr_plus); foreach($arr_plus as $key => $value){ //到了最后一项的时候 if($key+1 == $arr_reduce_totle){ $temp = "('".$id."',"; foreach($column_que as $key => $val){ if($column_que_totle - 1 == $key){ $temp .= "'".$_POST[$val.$value]."'"; }else{ $temp .= "'".$_POST[$val.$value]."',"; } } $sql .= $temp.")"; }else{ $temp = "('".$id."',"; foreach($column_que as $key => $val){ if($column_que_totle - 1 == $key){ $temp .= "'".$_POST[$val.$value]."'"; }else{ $temp .= "'".$_POST[$val.$value]."',"; } } $sql .= $temp."),"; } } //bingo $model_ans->execute($sql); }else{ //删除了选项 要进行delete操作 $arr_reduce = null; foreach($macth_arr as $key => $val){ if(!in_array($val,$record_arr)){ //当不在这个数据库的时候 说明需要新增 记录下ID $arr_reduce[] = $val; }else{ $arr_update_arr[] = $val; } } $delete_sql_str = implode(',',$arr_reduce); $sql = "delete from allscore where id in ($delete_sql_str)"; $model_ans->execute($sql); } } //最后进行update操作 并且总是要进行这一步 // 有可能没有变化 选项的个数 if(empty($arr_update_arr)){ $arr_update_arr = $macth_arr; } foreach($arr_update_arr as $key => $val){ $updateSql = 'UPDATE allscore set'; $tempStr = null; foreach($column_que as $key_1 => $val_1){ if($column_que_totle == $key_1+1){ $tempStr .= "$arr_update_ans[$key_1] ="."'".$_POST[$val_1.$val]."'"; }else{ $tempStr .= " $arr_update_ans[$key_1] ="."'".$_POST[$val_1.$val]."',"; } } $updateSql .= $tempStr." where `id` = $val "; $model_ans->execute($updateSql); } echo 'yes'; exit; }
积累知识,分享知识,学习知识。
原文地址:https://www.cnblogs.com/bin-pureLife/p/3906825.html