博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
做问答系统是对题目修改的bug
阅读量:5156 次
发布时间:2019-06-13

本文共 9863 字,大约阅读时间需要 32 分钟。

没有有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

你可能感兴趣的文章
函数积累
查看>>
Swift 入门之简单语法(六)
查看>>
〖Python〗-- IO多路复用
查看>>
栈(括号匹配)
查看>>
Java学习 · 初识 面向对象深入一
查看>>
源代码如何管理
查看>>
vue怎么将一个组件引入另一个组件?
查看>>
bzoj1040: [ZJOI2008]骑士
查看>>
LeetCode 74. Search a 2D Matrix(搜索二维矩阵)
查看>>
利用SignalR来同步更新Winfrom
查看>>
反射机制
查看>>
CocoaPod
查看>>
BZOJ 1251: 序列终结者 [splay]
查看>>
5G边缘网络虚拟化的利器:vCPE和SD-WAN
查看>>
MATLAB基础入门笔记
查看>>
【UVA】434-Matty's Blocks
查看>>
Android开发技术周报 Issue#80
查看>>
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
django知识点总结
查看>>
C++ STL stack、queue和vector的使用
查看>>