PHP MySQL Insert survey answers into table - php

If i have this HTML and PHP Code:
<?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>
So the code selects the survey from the survey table and then gets all the questions from survey_questions for the selected survey.
Each question has up to 8 answers (all checkboxes with value of Y)
The user goes though and checks the box for the answer(s) they choose so when the form is submitted, how can i make it insert into surveys_completed table each answer.
So for each question, it adds a row in the surveys_completed table.
The structure of the surveys_completed table is:
sequence
survey_sequence
question_seq
answer1
answer2
answer3
answer4
answer5
answer6
answer7
answer8

create a page called getval.php then put the below code and you have to make sure you
connect to the database using pdo thats when this code is gonna work for you .
H i hope it will help you good 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 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
));
?>

Related

Getting the value of checkbox

Good day. I'm a newbie in php.
What I'm trying to do is i want to get the id of the specific data row from my database. And update some column on my table.
What I did is this..
<td style="width:50px;"><input type="checkbox" id="chkPending" name="chkPending[]" value="<?php echo $row['RequestNumber']; ?>"/></td>
<td><span class="label label-success">View Details</span></td>
<td style="width:100px;"><?php echo $row['RequestNumber']; ?></td>
<td style="width:100px;"><?php echo $row['Requestor']; ?></td>
<td style="width:100px;"><?php echo $row['Department']; ?></td>
<td style="width:50px;"><?php echo $row['category']; ?></td>
<td style="width:100px;"><?php echo $row['AssignedTo']; ?></td>
<td style="width:100px;"><?php echo $row['status']; ?></td>
<td style="width:100px;"><?php echo $row['DateRequested']; ?></td>
<td style="width:100px;"><?php echo $row['TimeRequested']; ?></td>
PHP CODE
<?php
$RequestNumber = $POST_['chkPending'];
$sql = "UPDATE tblrequest SET Assignedto = 'personnel', status = 'Assigned' WHERE RequestNumber = '$RequestNumber'";
if (isset($_POST['submit'])) {
$success = mysql_query($sql) or die(mysql_error());
mysql_close();
}
if ($success == TRUE) {
?>
<script>
alert('You have successfully update account.');
</script>
<?php
}
?>
I would appreciate any help. Thank you in advance.
This is my whole code:**
Change your input name by this:
<input type="checkbox" id="chkPending" name="chkPending[<?= $row['RequestNumber']; ?>]" />
In your PHP code do this:
<?php
if (isset($_POST['submit'])) {
foreach($_POST['chkPending'] as $RequestNumber=>$value)
{
$sql = "UPDATE tblrequest SET Assignedto = 'personnel', status = 'Assigned' WHERE RequestNumber = '".$RequestNumber."'";
$success = mysql_query($sql) or die(mysql_error());
}
mysql_close();
}
if ($success == TRUE) {
?>
<?php
<script>
alert('You have successfully update account.');
</script>
<?php
}
?>
You can use this code to compare it. This code is working and will print 2,4.
<form action="#" method="POST">
<input name="test[1]" type="checkbox" />
<input name="test[2]" checked type="checkbox" />
<input name="test[3]" type="checkbox" />
<input name="test[4]" checked type="checkbox" />
<input type="submit" />
</form>
<?php
if($_POST)
{
foreach($_POST['test'] as $id => $value)
{
echo $id.'<br />';
}
}
?>

Updating database with for loop and mysql

The code for updating my questions is working but when updating the answers it is not. How can i fix this? help. thanks.:)
This is on my form:
<?php do { ?><tr><th width="170" scope="col"><input type="checkbox" name="selected[]" value="<?php echo $row_Recordset1['question_id']; ?>" />
Description:</th>
<td colspan="2" scope="col">old:
<?php echo $row_Recordset1['question_description']; ?>
new:<input name="questiondesc[]" type="text" size="50" />/td>
<td width="549" colspan="2" scope="col"><div align="left"></td>
</tr>
<input type="hidden" name="ansid[]" value="<?php echo $row_Recordset2['answer_id']; ?>" />
<input name="answerdesc[]" type="text" size="20" value="<?php echo $row_Recordset2['answer_description']; ?>" /><?php
if($row_Recordset2['answer_iscorrect'] == 1){
echo "Correct";
}
?>
<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
<?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>
/////This is my update code
if(isset($_POST['selected'])){
$selected = $_POST['selected'];
$question = $_POST['questiondesc'];
$answer = $_POST['answerdesc'];
$answerid = $_POST['ansid'];
for ($i = 0; $i < sizeof($selected); $i++)
{
$sql = sprintf("UPDATE exam_questions SET question_description = '%s' WHERE question_id = '%s'",
mysql_real_escape_string($question[$i]),
mysql_real_escape_string($selected[$i]));
mysql_query($sql)or die(mysql_error());
for($x = 0; $x < sizeof($answerid); $x++){
$gomugomu = sprintf("UPDATE exam_answers SET answer_description = '%s' WHERE answer_question_set_id = '%s'",
mysql_real_escape_string($answer[$x]),
mysql_real_escape_string($answerid[$x]));
mysql_query($gomugomu)or die(mysql_error());
}
}
}
?>
Your ansid and answerdesc are not arrays
<input type="hidden" name="ansid" .....
<input name="answerdesc" ....
to
<input type="hidden" name="ansid[]" ...
<input name="answerdesc[]" ...

Want to update multiple records using checkboxes and save button

I am searching some records using two text boxes and then updating the selected records in database. i am able to see the value row id of the selected checkbox but when i want to get the value for updation in database it gives 0, i.e showing no record in array
Here is my code
if($_POST["search"])
{
$nitnumber = $_POST["nitnumber"];
$workno = $_POST["workno"];
$query = "select * from print where nit = $nitnumber and work = $work";
$result = mysql_query($query) or die ("<font color =red>NIT Number and/or Work Number is Missing</font>");
$count = mysql_num_rows($result);
if($count == 0)
echo "<font color=red>Record not found</font>";
else
{
while($record = mysql_fetch_assoc($result))
{
?>
<tr class="odd">
<td><div align="center"><?php echo $record['a']; ?></div></td>
<td><div align="center"><?php echo $record['b']; ?></div></td>
<td><div align="left"><?php echo $record['c']; ?></div></td>
<td> <div align="left">
<?php
enter code hereecho $record["d"];
?>
</td>
<td>
<input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>">
<?php echo $record["id"];?>
</td>
<?php } } }?>
<tr>
<td colspan="5" align="right"> <input type="submit" value="Save" name="save"> </td>
</tr>
<?php
if ($_POST['save'])
{
$num_chkboxes=count($_POST['checkbox']);
for($i=0; $i<$num_chkboxes; $i++){
$complete = intval($checkbox[$i]);
echo $complete;
var_dump($complete);
echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
//$Result1 = mysql_query($updateSQL, $FamilyOrganizer) or die(mysql_error());
}
}
?>
<?php
if ($_POST['save'])
{
$checkbox1 = $_POST['chk1'];
$selected_checkbox = "";
foreach ($checkbox1 as $checkbox1)
{
$selected_checkbox .= $checkbox1 . ", ";
}
$selected_checkbox = substr($selected_checkbox, 0, -2);
$updateSQL = "" // your update query here
}
?>
<input type="checkbox" name="chk1[]" value=""> // take checkboxes like this
First, the problem started when you fill the values of the checkboxes:
<input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>" />
you must gave them the value like
value="<?php echo $record['id'] ?>"
Second, you don't get the values of the checkboxes as you should:
for($i = 0; $i < count($_POST['checkbox']); $i++){
$complete = intval($_POST['checkbox'][$i]);
//echo $complete;
//var_dump($complete);
echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
}
Another thing you should take care about is the assigning of the elements ids:
id="checkbox[]"
you must give unique ids for each element:
id="checkbox-1"
id="checkbox-2"

How to Keep Radio Button Value after Next/Prev page?

## Keep Radio Button Value after Next/Prev page .. ##
This is for Student Online Exam
if(!isset($_GET['start'])){
$_GET['start'] = "undefined";
}
$start=$_GET['start']; /*To take care global variable if OFF*/
if(!($start > 0)) { /*This variable is set to zero for the first page*/
$start = 0;
}
$startpos = ($start -0);
$limit = 1;
// No of records to be shown per page.
$currentpos = $startpos + $limit;
$back = $startpos - $limit;
$next = $startpos + $limit;
$countquery=" SELECT * FROM quiz ";
$countresult=mysql_query($countquery);
echo mysql_error();
$totalrecords=mysql_num_rows($countresult);
//$quiz_table = mysql_query("select * from quiz Limit 1");
?>
<form method="POST" action="" onclick="">
<table>
<?php
$quiz_table=" SELECT * FROM quiz ORDER BY qid limit $startpos, $limit ";
$result=mysql_query($quiz_table);
echo mysql_error();
while ($row = mysql_fetch_array($result))
{
?>
<tr>
<td><?php echo "$row[qid]"; ?></td>
<td><?php echo "$row[Question]"; ?></td>
</tr>
<tr>
<td><input type="radio" name="a" id="a" value="a"/> <?php echo "$row[opt1]"; ?></td>
<td><input type="radio" name="a" id="a" value="b"/> <?php echo "$row[opt2]"; ?></td>
<td><input type="radio" name="a" id="a" value="c"/> <?php echo "$row[opt3]"; ?></td>
<td><input type="radio" name="a" id="a" value="d"/> <?php echo "$row[opt4]"; ?></td>
</tr>
<tr>
<td><!--<input type="submit" name="smt" value="check"/>-->
<?php
if($back >=0) {
print "<a href='onlineexam.php?start=$back'><font face='Verdana' size='2'><input type='button' name='prev' value='PREV'></font></a>";
}
if($currentpos < $totalrecords) {
## Keep Radio Button Value after Next/Prev page .. ##
print "<a href='onlineexam.php?start=$next'><font face='Verdana' size='2'><input type='button' name='nxt' value='NEXT'></font></a>";
}
?></td>
<td><input type="submit" name="smt" value="Finish"/></td>
</tr>
<?php
}
$quiz_table="SELECT * from quiz";
$result=mysql_query($quiz_table);
while ($row = mysql_fetch_array($result))
{
$id=$row['qid'];
$ques=$row['Question'];
$op1=$row['opt1'];
$op2=$row['opt2'];
$op3=$row['opt3'];
$op4=$row['opt4'];
$crt=$row['woptcode'];
if(isset($_POST['smt'])){
$ans= $_POST['a'];
if(isset($_POST['a'])){
if($crt == $ans ){
$mark=0;
$mark=$mark+1;
mysql_query("INSERT INTO stuans(qid,Question,opt1,opt2,opt3,opt4,woptcode,stuanswer,mark)VALUES('$id','$ques','$op1','$op2','$op3', '$op4','$crt','$ans','$mark')");
} else{
$mark=0;
mysql_query("INSERT INTO stuans(qid,Question,opt1,opt2,opt3,opt4,woptcode,stuanswer,mark)VALUES('$id','$ques','$op1','$op2','$op3', '$op4','$crt','$ans','$mark')");
}
}
}
}
..THANKS..
You can use the checked attribute.
<input type="radio" checked>
So if you had some variables (booleans), $opt1, $opt2, $opt3, $opt4, then you could do something similar to this:
<input type="radio" name="opt1" <?php if ($opt1) echo "checked"; ?> />
<input type="radio" name="opt2" <?php if ($opt2) echo "checked"; ?> />
<input type="radio" name="opt3" <?php if ($opt3) echo "checked"; ?> />
<input type="radio" name="opt4" <?php if ($opt4) echo "checked"; ?> />

How to disable individual checkbox?

I want to display array values followed by a checkbox inside a html table using a for loop :
<?php
for ($j = 0; $j < $data['user']['droit']['cnt']; $j++)
{
?>
<table>
<tr>
<td><?php echo $data['user']['droit'][$j]['menu_titre']; ?></td>
<?php
$checked = "";
if (is_array($data['user_droit']) && count($data['user_droit']) > 0 && in_array($data['user']['droit'][$j]['menu_code'], $data['user_droit']))
$checked = " checked ";
?>
<td><input type="checkbox" name="<?php echo $data['user']['droit'][$j]['menu_code']; ?>"
value="<?php echo $data['user']['droit'][$j]['menu_code']; ?>" <?php echo $checked ?> />
</td>
</tr>
</table>
<?php
}
?>
I don't know how to make the individual checkbox to be disabled if it is checked from the loop. How to achieve that ?
or set $checked = " checked disabled ";
<?php
for ($j = 0; $j < $data['user']['droit']['cnt']; $j++)
{
?>
<table>
<tr>
<td><?php echo $data['user']['droit'][$j]['menu_titre']; ?></td>
<?php
$checked = "";
if (is_array($data['user_droit']) && count($data['user_droit']) > 0 && in_array($data['user']['droit'][$j]['menu_code'], $data['user_droit']))
{
$checked = " checked disabled ";
}
?>
<td><input type="checkbox" name="<?php echo $data['user']['droit'][$j]['menu_code']; ?>"
value="<?php echo $data['user']['droit'][$j]['menu_code']; ?>" <?php echo $checked ?> />
</td>
</tr>
</table>
<?php
}
?>

Categories