PHP MySQL Insert survey answers into table -
if have html , php code:
<?php if(isset($_post["submit"])) { $num = $_post['number']; ($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 line items lineitems table bill sequence adhocbills table $sql="insert 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 selected'; exit(); } */ if(mysql_real_escape_string($_get["company"]) != '') { //sql company $sql="select * surveys company = '".mysql_real_escape_string($_get["company"])."' , string = '".mysql_real_escape_string($_get["string"])."' "; } elseif(mysql_real_escape_string($_get["string"]) != '') { //sql string $sql="select * surveys 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 * surveys_questions 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>
so code selects survey survey table , gets questions survey_questions selected survey.
each question has 8 answers (all checkboxes value of y)
the user goes though , checks box answer(s) choose when form submitted, how can make insert surveys_completed table each answer.
so each question, adds row in surveys_completed table.
the structure of surveys_completed table is:
sequence survey_sequence question_seq answer1 answer2 answer3 answer4 answer5 answer6 answer7 answer8
create page called getval.php put below code , have make sure connect database using pdo thats when code gonna work . h hope luck
<?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 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 )); ?>
Comments
Post a Comment