drpfu51608120170 2012-04-03 15:40
浏览 33
已采纳

它只在数据库中正确显示选项

I have a php code where it uses a a switch statement to find any of the values from the textbox and output the correct 'optionId' which matches the $selected_option which matches the value from the textbox.

The problem I have though is that lets say there are 4 questions, 3 questions with different options and 1 which has the same optionId as another question. When I insert them in the database it is suppose to look like below:

   Question               OptionId

    What is my gender     O17   //case '19'
    What is my name        O3    //case '5'
    What is my address     O17   //case '19'
    What is my age         O7    //case '9'

But instead it is showing only the latest OptionId for both questions like below which is incorrect.

Question               OptionId

What is my gender      O7
What is my name        O7
What is my address     O7
What is my age         O7

So what I want to know is why is it displaying only the latest OptionId for all questions? Do I need to loop the switch statement or is there something wrong with the way I loop the $insertQuestion[] as that goes through each Question?

    $insertquestion = array();


        $options = $_POST['gridValues'];

        switch ($options){

            case "3": 
            $selected_option = "A-C";
            break;

            case "4": 
            $selected_option = "A-D";
            break;

            case "5": 
            $selected_option = "A-E";
            break;

            default:
            $selected_option = "";
            break;

        }      


  $optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '". mysql_real_escape_string($selected_option)."')";

$optionrs = mysql_query($optionquery);
    while($optionrecord = mysql_fetch_array($optionrs)){
        $optionid[] = $optionrecord['OptionId'];
    }



foreach($_POST['questionText'] as $question)
{
    $insertquestion[] = "'". mysql_real_escape_string( $_SESSION['id'] ) . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". mysql_real_escape_string( $_POST['num_questions'] ) ."','".  mysql_real_escape_string( $question ) ."','".  mysql_real_escape_string( $optionid[] ) ."'";
}

  $questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId) 
  VALUES (" . implode('), (', $insertquestion) . ")";



echo($questionsql)

UPDATE:

Below is the form code. How it works is the user types in a question in the textarea ('name='questionText') and types in an option (name='gridValues') and then they append them two in a table row (table in the form which id='qandatbl'). This is the question 1. Then they do the same again for second question, then third and etc. Please look at this carefully, it is easy to follow :)

<script>

    function insertQuestion(form) {   

    var context = $('#optionAndAnswer');

    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
    var $question = $("<td class='question'></td>");
    var $options = $("<td class='option'></td>");
    var $questionType = '';

    $('#questionTextArea').each( function() {

    var $this = $(this);
    var $questionText = $("<textarea class='textAreaQuestion'></textarea>").attr('name',$this.attr('name')+"[]")
                   .attr('value',$this.val())

    $question.append($questionText);

    });

    $('.gridTxt', context).each( function() {

     var $this = $(this);
     var $optionsText = $("<input type='text' class='gridTxtRow maxRow' />").attr('name',$this.attr('name'))
                     .attr('value',$this.val())

    $options.append($optionsText);
    $questionType = $this.val();

    });

    $tr.append($question);
    $tr.append($options);    
    $tbody.append($tr); 

    }

</script>


<form id="QandA" action="insertQuestion.php" method="post" >

<table>
<tr>
    <td rowspan="3">Question:</td> 
    <td rowspan="3">
        <textarea id="questionTextArea" rows="5" cols="40" name="questionText"></textarea>
    </td>
</tr>
</table>

<table id="optionAndAnswer" class="optionAndAnswer">
<tr class="option">
<td>Option Type:</td>
<td>
<div>
    <input type="text" name="gridValues" class="gridTxt maxRow" readonly="readonly" />
</div>
</td>
</tr>
</table>

<table id="qandatbl" align="center">
<thead>
<tr>
    <th class="question">Question</th>
    <th class="option">Option Type</th>
</tr>
</thead>
<tbody>
</tbody>
</table>

</form>
  • 写回答

3条回答 默认 最新

  • douda5227 2012-04-03 16:43
    关注

    as discussed - you have asked to show you how to apply loop:

    $optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '" 
             . mysql_real_escape_string($selected_option)."')";
    
        $optionrs = mysql_query($optionquery);
        while($optionrecord = mysql_fetch_array($optionrs)){
            $optionid[] = $optionrecord['OptionId'];
        }
    

    your $optionid is then becomes array of all matched elements.

    if you think i understood your problem correctly, then let me know so we can work on UPDATE query


    UPDATE

    $options[] = $_POST['gridValues'];
    $i=0;
    
    foreach($_POST['questionText'] as $question){
    
            switch ($options[$i]){
                case "3": 
                $selected_option = "A-C";
                break;
                case "4": 
                $selected_option = "A-D";
                break;
                case "5": 
                $selected_option = "A-E";
                break;
                default:
                $selected_option = "";
                break;
            }      
        $optionquery = "SELECT OptionId FROM Option_Table WHERE (OptionType = '". mysql_real_escape_string($selected_option)."')";
    
        $optionrs = mysql_query($optionquery);
        $optionrecord = mysql_fetch_array($optionrs);
        $optionid = $optionrecord['OptionId'];
    
        $insertquestion[] = "'". mysql_real_escape_string( $_SESSION['id'] ) . 
            ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '') ."' ,'". 
            mysql_real_escape_string( $_POST['num_questions'] ) ."','".  
            mysql_real_escape_string( $question ) ."','". 
            mysql_real_escape_string( $optionid ) ."'";
    
        $i++;
    }
    
    $questionsql = "INSERT INTO Question (SessionId, QuestionId, QuestionContent, OptionId) 
        VALUES (" . implode('), (', $insertquestion) . ")";
    
        echo($questionsql);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛