Inserting radio button input in the database - php

I'm creating a quiz-like structure. For that i'm trying to get the answer of each question and insert it into my database with the type of question that was answered. However i'm having trouble putting these data in variables and i'm getting:
Notice: Undefined index: question-0-answer
in the commented part of the code. Any help..
$options = '';
$filter=mysql_query("select afnumber from employees WHERE Status='Employed '");
while($row = mysql_fetch_array($filter)) {
$options .="<option >" . $row['afnumber'] . "</option>";
}
$menu="<form id='filter' name='filter' method='post' action=''>
AFNumber : <select name='SelectAF' id='filter' style='color:grey;'>" . $options . "</select>
Evaluation Test Type : <select name='Type' id='type' style='color:grey;'><option selected='selected'></option><option value='loyalty'>Loyalty</option><option value='performance'>Performance</option></select>
<input type='submit' name='submit1' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
</form>
<br>
";
echo $menu;
if(isset($_POST['submit1']))
{
$type = $_POST['Type'];
$mysqli = new mysqli("localhost", "root", "Js", "jr");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if ( $result = $mysqli->query( "SELECT questiontext FROM questioninfo WHERE type='$type'" ) ) {
$html=array();
$html[]="
<form action='' method='post' id='quiz'>
<ol>";
$counter=1;
while( $row = $result->fetch_array() ) {
$question=$row['questiontext'];
$answerA=1;
$answerB=2;
$answerC=3;
$answerD=4;
$answerE=5;
$html[]="
<br/>
<h3>Question {$counter}: {$question}</h3>
<li>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersA' value='A' />
<label for='question-{$counter}-answers-A'> {$answerA} </label>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersB' value='B' />
<label for='question-{$counter}-answers-B'> {$answerB} </label>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersC' value='c' />
<label for='question-{$counter}-answers-C'> {$answerC} </label>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersD' value='D' />
<label for='question-{$counter}-answers-D'> {$answerD} </label>
<br/>
<input type='radio' name='question-{$counter}-answers' id='question-{$counter}-answersE' value='E' />
<label for='question-{$counter}-answers-E'> {$answerE} </label>
</li>";
$counter++;
}
$html[]="
</ol>
<input type='submit' name='submit' value='Submit' style='width:80px; height:30px; text-align:center; padding:0px;'>
<input type='hidden' name='type' value='{$type}' />
</form>";
echo implode( PHP_EOL, $html );
$result->close();
}
}
/*
if( isset( $_POST['submit'] ) ){
$numQuestions=10;
for( $counter=0; $counter < $numQuestions; $counter++ ){
$answer=$_POST['question-'.$counter.'-answers'];
$sql="insert into `question` (`type`,`value`) values ('".$type."','".$answer."')";
$db->query( $sql );
}
}
*/

The $counter for outputting the radiobuttons is initialized with the value 1 but when reading the values of radiobuttons at the end of your script, you are starting with $counter = 0;.
Please change
for( $counter=0; $counter < $numQuestions; $counter++ ){
to
for( $counter=1; $counter <= $numQuestions; $counter++ ){

You are running loop statically 10 times in comment and what if database have less than 10 records? You need to run loop a many times as db having records or you can put condition before insert by isset($_POST['question-'.$counter.'-answers'])

Related

How to insert multi text box values into sql using php?

<?php
$conn=mysqli_connect("localhost","root","","fesdb");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
if (isset($_POST['Insert']))
{
$txt = $_POST['text1'];
}
{
$query = "insert into table (Mytext) VALUES ('$txt')";
$result = mysqli_query($conn,$query);
if($result)
{
echo '<script>alert("record inserted")</script>';
}
else
{
echo ' Please Check Your Query ';
}
}
?>
<html>
<body>
<form action="" id="myForm" >
<input type='text' name="text1"><br>
<input type='text' name="text2"><br>
<input type='text' name="text3"><br>
<input type='text' name="text4"><br>
<input type='text' name="text5"><br>
<input type='text' name="text6"><br>
<input type='text' name="text7"><br>
<input type='text' name="text8"><br>
<input type='text' name="text9"><br>
<input type='text' name="text10"><br>
<input type='submit' name="Insert">
</form>
</body>
</html>
I am a beginner in PHP .. I have sql database with one Table , this table have one column .... I have 10 textboxes in my page with one button ... I want to insert each textbox into the table as separated record ... I know I should use loop (for each ...) to solve this problem ... but I dont know how do that ...
if possible help me with a sample

How to submit multiple radio button in php

I am trying to submit this radio buttons but it shows this
Notice: Array to string conversion in C:...
i don't really know what to do to make it work.
I've to try some other ways but its not working
<?php
public function userQuestion($type)
{
$option = $this->con->prepare("SELECT * FROM stem_questions WHERE category = :type");
$option->bindParam(':type', $type);
$option->execute();
$id = array();
while ($row = $option->fetch()) {
$id[] = $row["id"];
$que[] = $row["question"];
$o1[] = $row["opt1"];
$o2[] = $row["opt2"];
$o3[] = $row["opt3"];
$o4[] = $row["opt4"];
$cat[] = $row["category"];
$isa[] = $row["is_anwer"];
}
$count = count($id);
if ($count < 1) {
echo "<span class='alert alert-info fa fa-warning col-lg-12' style='color:orange; font-size:14px; color: white'> You have not Set any Question Yet.</span>";
} else {
$k = 0;
echo "";
for ($i = 1, $j = 0; $i <= $count; $i++, $j++) {
$k++;
echo "<form method='POST'> <p><strong> Question $i : $que[$j]</strong></p> <div class='form-group'> <input type='hidden' name ='qust' value='$que[$j]'> <input type='hidden' name ='cat' value='$cat[$j]'><input type='hidden' name ='ans' value='$isa[$j]'> <input type='radio' name ='opt[$k]' value='$o1[$j]'> <label class='form-check-label'>&nbsp $o1[$j]</label><br> <input type='radio' name='opt[$k]' value='$o2[$j]'><label class='form-check-label'>&nbsp $o2[$j]</label><br> <input type='radio' name='opt[$k]' value='$o3[$j]'><label class='form-check-label'>&nbsp $o3[$j]</label><br> <input type='radio' name ='opt[$k]' value='$o4[$j]'> <label class='form-check-label'>&nbsp $o4[$j]</label><br> </div> </form> ";} } }
public function submitQuiz()
{
//FOR THE QUIZ SYSTEM
if (isset($_POST["submit"])) {
for ($i = 1; $i <= 5; $i++) {
$userid = $_SESSION['id'];
$qid = $_POST['qust'];
$uans = $_POST['opt'] . $i;
$ans = $_POST['ans'];
$category = $_POST['cat'];
$insert = $this->con->prepare("INSERT INTO user_question_answer (user_id, question_id, user_ans, is_right, type)VALUES (:userid, :qid, :uans, :ans, :category)");
$insert->bindParam(':userid', $userid);
$insert->bindParam(':qid', $qid);
$insert->bindParam(':uans', $uans);
$insert->bindParam(':ans', $ans);
$insert->bindParam(':cat', $cat);
if (!$insert->execute()) {
$atry = $insert->errorInfo();
echo $atry[2];
exit();
} else {
// this just my person function #return $this->just_notify("<strong>Successfully uploaded </strong>", 1);} } } }
it was suppose to submit but its just bringing up errors
Error is with your name tag radio option in form.
Replace your echo "" part --
with
echo "<form method='POST'>
<p><strong> Question $i : $que[$j]</strong></p>
<div class='form-group'>
<input type='hidden' name ='qust' value='$que[$j]'>
<input type='hidden' name ='cat' value='$cat[$j]'>
<input type='hidden' name ='ans' value='$isa[$j]'>
<input type='radio' name ='opt1['.$k.']' value='$o1[$j]'> <label class='form-check-label'>&nbsp $o1[$j]</label><b`enter code here`r>
<input type='radio' name='opt2['.$k.']' value='$o2[$j]'><label class='form-check-label'>&nbsp $o2[$j]</label><br>
<input type='radio' name='opt3['.$k.']' value='$o3[$j]'><label class='form-check-label'>&nbsp $o3[$j]</label><br>
<input type='radio' name ='opt4['.$k.']' value='$o4[$j]'> <label class='form-check-label'>&nbsp $o4[$j]</label><br>
</div>
</form>"
and
$insert->bindParam(':uans', $uans); can bind only single value not the array data.
> <input type="radio" name="radio_name[]"
> value="id_of_row_from_database_or_identity_of_this_radio_button" />
>
> ex:-
>
> <input type="radio" name="age[]" value="G`enter code here`G101" />12 <input
> type="radio" name="age[]" value="GG102" />23

mysql_data_seek not working

my code is,
$query3 = "SELECT * FROM db_exam_skip WHERE user='$session'";
$result3 = mysql_query($query3) or die(mysql_error());
$length3 = mysql_num_rows($result3);
while($rows3 = mysql_fetch_array($result3))
{
$query1 = "SELECT * FROM db_exam_questions WHERE id='$rows3[ques_id]'";
$result1 = mysql_query($query1) or die(mysql_error());
$length1 = mysql_num_rows($result1);
}
if(isset($_POST['next']))
{
if(isset($_SESSION['list']))
{
mysql_data_seek($result1,$_SESSION['list']);
}
else
{
$list = $_POST['list'];
mysql_data_seek($result1,$list);
}
}
<?php
while($rows1 = mysql_fetch_row($result1))
{
$start = $rows1[0];
$_SESSION['start'] = $start;
?>
<form action="" method="post">
<p style="font-size:20px;font-weight:bold"><?php echo $rows1[5]; ?></p>
<ul style="list-style-type:none">
<input type='hidden' name='number' value='<?php echo $_SESSION['order']++; ?>' />
<input type='hidden' name='list' value='<?php echo $_SESSION['list']++; ?>' />
<input type='hidden' name='ques_id' value='<?php echo $rows1[0]; ?>' />
<input type='hidden' name='correct' value='<?php echo $rows1[10]; ?>' />
<li><input type="radio" name="answer" value="1" /> <?php echo $rows1[6]; ?> <br><br>
<input type="radio" name="answer" value="2" /> <?php echo $rows1[7]; ?> <br><br>
<input type="radio" name="answer" value="3" /> <?php echo $rows1[8]; ?> <br><br>
<input type="radio" name="answer" value="4" /> <?php echo $rows1[9]; ?> <br></li>
</ul>
<input type="submit" class="button4" value="Next" name="next" />
</form>
<?php
break;
}
?>
my question is, after the first question is loaded, when i press next button to load the second question I am getting the below error.
Warning: mysql_data_seek(): Offset 2 is invalid for MySQL result index 8 (or the query data is unbuffered) in C:\wamp\www\Albert\ICAMS\start_skip_question.php on line 15
I have tried a lot to solve this error. But till now no success. Is there any method to solve. Any help will be appreciated.
Thank you.
I guess the result set is empty.I think query is returning empty set.
You will get this error if result set is empty
check the PHP DOCS
First check if you are getting ant rows from the result
if (mysqli_num_rows($sql) > 0)
{
}

How to get chekbox values on Mysql?

Hello i'm at linking my form data to my mysql database server so far so good i have a little problems here
my prcoes.php. code :
$db_selected = mysql_select_db (DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' / mysql_error());
}
$value = $_POST['name'];
$value2 = $_POST['surname'];
$value3 = $_POST['email'];
$value4 = $_POST['phone'];
$value4 = $_POST['activity'];
$value5 = $_POST['ltype'];
With checkboxes $_POST['ltype'];i get only Array on Mysql as result ?
I get the right values with this code by sending form data to my email :
'LType : ' . implode(',', $_POST['ltype']). "\n" .
Any help will be welcomed , thanks in advance
Update here my chekbox code :
<div class="thumb1" >
<label for="word" ><img class="img" src="images/my1.jpg" /></label>
<input type="checkbox" class="chk" name="ltype[]" id="word" value="word" /><hr> <p><strong>Word Mark Logo</strong></p>
</div>
<div class="thumb1" >
<label for="letter"><img class="img" src="images/my2.jpg" /></label>
<input type="checkbox" class="chk" name="ltype[]" id="letter" value="letter" /><hr> <p><strong>Letter Mark Logo</strong></p>
</div>
<div class="thumb1">
<label for="emblerm"><img class="img" src="images/my3.jpg" /></label>
<input type="checkbox" class="chk" name="ltype[]" id="emblerm" value="emblerm" /><hr> <p><strong>Emblerm Logo</strong></p>
</div>
Why don't you:
$value5 = implode(',', $_POST['ltype']);
if you want the array back just explode the database value.
Depends on how you define the checkbox if
<form action='XXX.php' method='POST'>
Football: <input type="checkbox" name="sports[]" value="football" />
Baseball: <input type="checkbox" name="sports[]" value="baseball" />
</form>
And on the XXX.php it return as array
if ( $_POST['sports'] ) {
$arySports = $_POST['sports'];
foreach( $arySports AS $value ) {
echo $value ."<br>";
}
}
If you did
Subscribe Now <input type="checkbox" name="subscribe" />
And on the XXX.php it return as on
If you did
Subscribe Now <input type="checkbox" name="subscribe" value="now" />
Then it return string "now". So try with this and see what you want for your checkbox
<form action="XXX.php" method="POST">
<input type='checkbox' name='A'/>
<input type='checkbox' name='B' value='this is array'/>
<input type='checkbox' name='B' value='the second one'/>
<input type='checkbox' name='C[]' value='A'/>
<input type='checkbox' name='C[]' value='B'/>
<input type='checkbox' name='C[]' value='C'/>
<input type='checkbox' name='D' value='string'/>
</form>
on the XXX.php do
print_r( $_POST['A'] );
echo "<br>";
print_r( $_POST['B'] );
echo "<br>";
print_r( $_POST['C'] );
echo "<br>";
print_r( $_POST['D'] );
echo "<br>";

Update multiple rows with radio button check

I am getting the value for the single tsid for each record, however the checked radio button value is not returned in the array, I just get 0? Any help is appreciated.
PHP:
// Set the timesheets to set status approved/rejected
// find out how many records there are to update
$size = count($_POST['tsid']);
// start a loop in order to update each record
$i = 0;
while ($i < $size) {
// define each variable
$tsid = intval($_POST['tsid'][$i]);
$personnelid = intval($_POST['personnel'][$i]);
print "TSID: " . $tsid . "<br>";
print "TSuser: " . $personnelid . "<br>";
if ($tsid > 0 && $personnelid > 0) {
// do the update and print out some info just to provide some visual feedback
$query = "Update timesheets set status='1' where id= '$tsid' LIMIT 1";
mysql_query($query) or die ("Error in query: $query");
}
++$i;
}
mysql_close();
HTML:
<input type="hidden" name="tsid[]" value="<?PHP echo $row['id']; ?>">
<li data-role="fieldcontain">
<a href="tsapprove.php?id=<?PHP echo $row['id']; ?>"><p><?PHP echo $row['personnel']; ?></p>
<p><?PHP echo $row['name']; ?></p>
<p class="ui-li-aside"><strong><?PHP echo $row['totalhrs']; ?> H</strong></p>
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="radio" name="personnel[]" id="1" value="<?PHP echo $row['personnel']; ?>" />
<label for="1">Approve</label>
<input type="radio" name="personnel[]" id="2" value="<?PHP echo $row['personnel']; ?>" />
<label for="2">Reject</label>
</fieldset>
</a>
View Details
</li>
Hopefully you can piece this together:
<?
$size = count($_POST['tsid']);
echo "<pre>";print_r($_POST);echo "</pre>";
$i = 0;
while ($i < $size) {
$tsid = $_POST['tsid'][$i];
$pid = $_POST['personnel_'.$i];
print "TSID: " . $tsid . "<br />";
print "TSuser: " . $pid . "<br />";
$i++;
}
?>
<form method="post">
<!--tsid[0] - $i = 0-->
<input type="hidden" name="tsid[]" value="1" />
<input type="radio" name="personnel_0" value="111" />
<input type="radio" name="personnel_0" value="222" />
<!--tsid[1] - $i = 1-->
<input type="hidden" name="tsid[]" value="2" />
<input type="radio" name="personnel_1" value="333" />
<input type="radio" name="personnel_1" value="444" />
<input type="submit" />
</form>
Basically, the way you are returning personnel doesnt really work. So I simplified this a bit, and basically set the radios for personnel differently. Instead of an array, its setting the actual name with the TSID value.
I hard coded 1 and 2, but you would replace with your $row['id'], as well has the number after "personnel_" would be 0..1..2 etc
Hopefully this helps
edit: few issues with code

Categories