Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting radio button value and storing in php

Tags:

html

php

mysql

this is radio button code now what if radio button does not have a constant name how would i store the data in database because to store the data in database we will need a name of form attribute

$sql1="select * from questions where email='". $_SESSION['email']    ."'";
 $row=mysqli_query($conn,$sql1);

 while ($result = mysqli_fetch_array($row))
{
 ?>

<h2 id="question_<?php echo $result['qid'];?>"><?php echo   $result['qid'].".".$result['question'];?></h2>

 <input type="radio" value="<?php echo $result['answer1'];?>"  name="<?php echo $result['qid'];?>"><?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>"  name="<?php echo $result['qid'];?>"><?php echo $result['answer2'];?>

<input type="radio" value="<?php echo $result['answer3'];?>"  name="<?php echo $result['qid'];?>"><?php echo $result['answer3'];?>

  <input type="radio" value="<?php echo $result['answer4'];?>"  name="<?php echo $result['qid'];?>"><?php echo $result['answer4'];?>

if we know the name of radio button we can access it using

<input type="radio" value="<?php echo $result['answer4'];?>"  name="name"><?php echo $result['answer4'];?>
 $name=$_POST['name'];

but in the above code name of radio button is not fixed.questions is the table which consists of qid and questions with multiple options that is answer1,answer2 etc

i want to store the option select by user into database.for which i need to know the name of radio button how should i use post in this case

$name=$_POST['what should go in here'];
like image 885
raju Avatar asked Oct 06 '16 06:10

raju


1 Answers

You can get the radio button value along with question ID as:

Basic Example:

<?php
$array = array(1,2); // your Question ID array
?>

Your Form:

<form method="post" action="">
<?php
foreach ($array as $key => $qid) {
?>
<input type="radio" value="1" name="radio[<?=$qid?>]">
Answer 1

<input type="radio" value="2" name="radio[<?=$qid?>]">
Answer 2

<input type="radio" value="3" name="radio[<?=$qid?>]">
Answer 3

<input type="radio" value="4" name="radio[<?=$qid?>]">
Answer 4
<?php
echo "<br/>";
}
?>
<input type="submit" name="submit"> 
</form>

In PHP:

<?php 
if(isset($_POST['submit'])) {
  $query = array();
  foreach ($_POST['radio'] as $key => $value) {
     $query[] = "('$value','$key')";
  }
  $sql = "INSERT INTO table (answer,questionID) VALUES ";
  $sql .= implode(",", $query);
  echo $sql;
}
?>

In this example query look like:

INSERT INTO table (answer,questionID) VALUES ('2','1'),('3','2')

Few Suggestions:

- Your code is open for SQL Injection, you must need to prevent your code with SQL Attack and this reference will help you to understand: How can I prevent SQL injection in PHP?

- Make sure your column name and table name not having any conflict, currently, you are using same name for both.


Update with Your Code:

<?php
while ($result = mysqli_fetch_array($row))
{
?>
<h2 id="question_<?php echo $result['qid'];?>"><?php echo $result['qid'].".".$result['question'];?></h2>

<input type="radio" value="<?php echo $result['answer1'];?>"  name="radio[<?php  echo $result['qid'];?>]">
<?php echo $result['answer1'];?>
<input type="radio" value="<?php echo $result['answer2'];?>"  name="radio[<?php  echo $result['qid'];?>]">
<?php echo $result['answer2'];?>

<input type="radio" value="<?php echo $result['answer3'];?>"  name="radio[<?php  echo $result['qid'];?>]">
<?php echo $result['answer3'];?>

<input type="radio" value="<?php echo $result['answer4'];?>"  name="radio[<?php  echo $result['qid'];?>]">
<?php echo $result['answer4'];?>
<? 
}
?>

In PHP:

<?php 
if(isset($_POST['submit'])) {
  $query = array();
  foreach ($_POST['radio'] as $key => $value) {
     $query[] = "('$value','$key')";
  }
  $sql = "INSERT INTO table (answer,questionID) VALUES ";
  $sql .= implode(",", $query);
  echo $sql; // run this query in mysqli_query()
}
?>

Few More Instructions:

- Change the table name as per your table name

- Change the column name according to your column.

- Use INSERT query at once, no need to use it inside the loop.

like image 102
devpro Avatar answered Sep 28 '22 19:09

devpro