0

この HTML と PHP コードがある場合:

<?php
if(isset($_POST["submit"]))
{
    $num = $_POST['number'];
    for ($i = 0; $i < $num; $i++)
    {
        if($i==0)
        {
            echo '0';
            $answer1 = $_POST["answer1"];
            $answer2 = $_POST["answer2"];
            $answer3 = $_POST["answer3"];
            $answer4 = $_POST["answer4"];
            $answer5 = $_POST["answer5"];
            $answer6 = $_POST["answer6"];
            $answer7 = $_POST["answer7"];
            $answer8 = $_POST["answer8"];
        }
        else
        {
            echo 'no 0';
            $answer1 = $_POST["answer1$i"];
            $answer2 = $_POST["answer2$i"];
            $answer3 = $_POST["answer3$i"];
            $answer4 = $_POST["answer4$i"];
            $answer5 = $_POST["answer5$i"];
            $answer6 = $_POST["answer6$i"];
            $answer7 = $_POST["answer7$i"];
            $answer8 = $_POST["answer8$i"];
        }
        //then insert the line items into the lineitems table with the bill sequence from the adhocbills table
        $sql="INSERT into surveys_completed (survey_sequence, question_seq, answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8) values ('".$_POST["survey_sequence"]."', '".$_POST["question_seq"]."', '".$answer1."', '".$answer2."', '".$answer3."', '".$answer4."', '".$answer5."', '".$answer6."', '".$answer7."', '".$answer8."')";
        $rs=mysql_query($sql,$conn) or die(mysql_error());
        echo $sql.'<br><br>';
    }
}
?>

<?php
/*
if(mysql_real_escape_string($_GET["string"]) == '')
{
    echo 'No Survey was selected';
    exit();
} */
if(mysql_real_escape_string($_GET["company"]) != '')
{
    //sql for company
    $sql="SELECT * from surveys where company = '".mysql_real_escape_string($_GET["company"])."' and string = '".mysql_real_escape_string($_GET["string"])."' ";
}
elseif(mysql_real_escape_string($_GET["string"]) != '')
{
    //sql for string
    $sql="SELECT * from surveys where string = '".mysql_real_escape_string($_GET["string"])."' ";
}
$rs=mysql_query($sql,$conn) or die(mysql_error());
$survey=mysql_fetch_array($rs);

echo '<h3>'.$survey["title"].'</h3>';
?>
<form method="post" action="/home.php?id=surveys/complete_survey">
<table width="600" border="0" cellspacing="5" cellpadding="5">
<?php
$sql2="SELECT * from surveys_questions where survey_seq = '".$survey["sequence"]."' ";
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
$counter=0;
while($survey_questions=mysql_fetch_array($rs2))
{
    $counter++;
    ?>
      <tr>
        <td colspan="2"><strong><?php echo $counter; ?>. <?php echo $survey_questions["question"]; ?></strong>
        COUNTER<input type="text" name="number" id="number" value="<?php echo $counter; ?>" />
        SURVEY<input type="text" name="survey_sequence" id="survey_sequence" value="<?php echo $survey["sequence"]; ?>" />
        QUESTION<input type="text" name="question_seq" id="question_seq" value="<?php echo $survey_questions["sequence"]; ?>" /></td>
      </tr>
      <tr>
        <td><?php if($survey_questions["answer1"] != '') { echo '<input type="checkbox" name="answer1'.$counter.'" value="Y" /> '.$survey_questions["answer1"]; } ?></td>
        <td><?php if($survey_questions["answer2"] != '') { echo '<input type="checkbox" name="answer2'.$counter.'" value="Y" /> '.$survey_questions["answer2"]; } ?></td>
      </tr>
      <tr>
        <td><?php if($survey_questions["answer3"] != '') { echo '<input type="checkbox" name="answer3'.$counter.'" value="Y" /> '.$survey_questions["answer3"]; } ?></td>
        <td><?php if($survey_questions["answer4"] != '') { echo '<input type="checkbox" name="answer4'.$counter.'" value="Y" /> '.$survey_questions["answer4"]; } ?></td>
      </tr>
      <tr>
        <td><?php if($survey_questions["answer5"] != '') { echo '<input type="checkbox" name="answer5'.$counter.'" value="Y" /> '.$survey_questions["answer5"]; } ?></td>
        <td><?php if($survey_questions["answer6"] != '') { echo '<input type="checkbox" name="answer6'.$counter.'" value="Y" /> '.$survey_questions["answer6"]; } ?></td>
      </tr>
      <tr>
        <td><?php if($survey_questions["answer7"] != '') { echo '<input type="checkbox" name="answer7'.$counter.'" value="Y" /> '.$survey_questions["answer7"]; } ?></td>
        <td><?php if($survey_questions["answer8"] != '') { echo '<input type="checkbox" name="answer8'.$counter.'" value="Y" /> '.$survey_questions["answer8"]; } ?></td>
      </tr>
    <?php
}
?>
<tr>
    <td colspan="2"><input type="submit" name="submit" id="submit" value="Complete Survey" />
</tr>
</table>
</form>

そのため、コードは調査テーブルから調査を選択し、選択した調査の survey_questions からすべての質問を取得します。

各質問には最大 8 つの回答があります (すべてのチェックボックスの値が Y)

ユーザーは、選択した回答のボックスをチェックして、フォームが送信されたときに、各回答をsurveys_completedテーブルに挿入するにはどうすればよいですか。

したがって、質問ごとに、surveys_completed テーブルに行が追加されます。

surveys_completed テーブルの構造は次のとおりです。

シーケンス 調査_シーケンス 質問_シーケンス 回答1 回答2 回答3 回答4 回答5 回答6 回答7 回答8

4

1 に答える 1

0

getval.php というページを作成し、以下のコードを配置します。このコードが機能する場合は、pdo を使用してデータベースに接続する必要があります。H私はそれがあなたの幸運に役立つことを願っています

<?php 
$answer1=(isset($_POST['answer1']))? trim($_POST['answer1']): '';
$answer2=(isset($_POST['answer2']))? trim($_POST['answer2']): '';
$answer3=(isset($_POST['answer3']))? trim($_POST['answer3']): '';
$answer4=(isset($_POST['answer4']))? trim($_POST['answer4']): '';
$answer5=(isset($_POST['answer5']))? trim($_POST['answer5']): '';
$answer6=(isset($_POST['answer6']))? trim($_POST['answer6']): '';
$answer7=(isset($_POST['answer7']))? trim($_POST['answer7']): '';
$answer8=(isset($_POST['answer8']))? trim($_POST['answer8']): '';
$query="INSERT INTO surveys_completed(
answer1,answer2,answer3,answer4,answer5,answer5,answer6,answer7,answer8 )
VALUES(:answer1,:answer2,:answer3,:answer4,:answer5,:answer5,
:answer6,:answer7,:answer8)";
$insert = $con->prepare($query);
$insert->execute(array(
':answer1'=>$answer1,
':answer2'=>$answer2,
':answer3'=>$answer3,
':answer4'=>$answer4,
':answer5'=>$answer5,
':answer6'=>$answer6,
':answer7'=>$answer7,
':answer8'=>$answer8
));
?>
于 2013-07-21T20:43:54.997 に答える