6

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'];
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
raju
  • 209
  • 7
  • 19

2 Answers2

1

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.

Community
  • 1
  • 1
devpro
  • 16,184
  • 3
  • 27
  • 38
  • i will try it thaks – raju Oct 06 '16 at 08:00
  • no i did'nt i found @kerv method much easier and thanks fro help – raju Oct 06 '16 at 09:24
  • @sumanthreddy: no, because he is using mysqli_query inside the loop, it means, your query will run multiple times, u can use only one single query. – devpro Oct 06 '16 at 09:25
  • ohh in that case can u explain ur code by using my code so it would be easy for me – raju Oct 06 '16 at 09:27
  • @sumanthreddy: first of all tell me, u just want to save answer, or answer along with quersion id? in Database? – devpro Oct 06 '16 at 09:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125080/discussion-between-devpro-and-sumanth-reddy). – devpro Oct 06 '16 at 09:32
  • what if table also conatins email and email is session variable hoe to add it the database – raju Oct 07 '16 at 10:12
  • @sumanthreddy: u can add email field also. and add column in query to – devpro Oct 07 '16 at 10:14
0

Create an array at the end of while loop like this :

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'];?>
<? $names[] = $result['qid'];

}
if(isset($_POST['submit'])) { 

for ($i=0; $i<count($names); $i++) {
if(isset($_POST[$names[$i]]) {
$rate = $_POST[$names[$i]];

$sql ="INSERT INTO answer (answer, qid) VALUES (".$rate.", ".$names[$i] .")";

mysqli_query($con, $sql);



}
}
}
?>
kerv
  • 315
  • 1
  • 2
  • 13
  • am new to php can u explain how to use the $names[ ] array in query to insert the value into database – raju Oct 06 '16 at 07:42
  • i would probably use a for loop for your query like this for($i=0; $i – kerv Oct 06 '16 at 07:44
  • you say that i need to write query to insert values to data base inside the for loop?? – raju Oct 06 '16 at 07:56
  • Yea, if i understand correctly. you want to insert all the different values – kerv Oct 06 '16 at 08:01
  • there are questions which have mutliple options then the value selected by the user for every question should be stored in the database to a column named answer – raju Oct 06 '16 at 08:11
  • can you please help what to do after the names array is created explain with the my above code please – raju Oct 06 '16 at 08:12
  • oh ok sorry i didnt understand your question. I am gonna update my answer. – kerv Oct 06 '16 at 08:13
  • bro, u need to modify this line `mysqli_query($sql);` mysqli_query needs two params. – devpro Oct 06 '16 at 09:26
  • what if i want to also add qid to database ...something like insert into answer (qid,answer)values('{rate}','{what sholud be here}'); that i s want to the question id to which user answered – raju Oct 06 '16 at 09:35
  • @sumanthreddy: yes, i asked u, do u want to insert questionid and answer together – devpro Oct 06 '16 at 09:36
  • what change did you made and where – raju Oct 06 '16 at 09:50
  • Adding $names[$i] to the query and making sure that $_POST[$names[$i]] is set – kerv Oct 06 '16 at 09:52