Save id with selected answer in php

I am trying to save the result of the selected answer with the question id. I don’t know how to pass the identifier to the database. here is my code

<?php
session_start();
include('config/manpower_db.php');

$sql = mysql_query("SELECT * FROM question") or die(mysql_error());
$i = '1';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="save_feedback.php" method="post">
<H2> How Will You Rate This Service</H2>

<?php
while($row = mysql_fetch_array($sql))
{

echo  $row['question'];
  
 // $comp = $row['id'][$i];
  
 // $_SESSION['comp']= $comp;

?>
<br />
<?php


echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='1' >1";
echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='2' >2";
echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='3' >3";
echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='4' >4";
echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='5' >5";


echo'<br/>';
$i++;
}
?>
<br/>

<strong> NOTE:</strong> 1 - Very Poor, 2 - poor, 3 - Okay, 4 - Fair, 5 - Good
<p>

<input name="submit" type="submit" value="submit" />
</form>
</body>
</html>
Run codeHide result

Save to DB

<?php
session_start();

$answer  = $_POST['answer'];

include('config/manpower_db.php');


			
			foreach($hobb as $key=>$val)
{
    $var1=$hobb[$key];
    $var2=$answer[$key];
			
         
            //include ('connect.php');
			//include ('mysql_connect.php');
            $table = "INSERT INTO answer (answer) ".
                     "VALUES ('$var2')";
            mysql_query($table) or die(mysql_error());
            $inserted_fid = mysql_insert_id();
            mysql_close();  
        }

		

echo'<script>alert("Inserted Successfully")</script>'; 

   
?>
Run codeHide result

This works, but I am trying to keep the question id. I don’t know how to transfer this to the database. thank you in advance

+4
source share
4 answers

Hidden Pass Pass ID

try it

<?php
session_start();
include('config/manpower_db.php');

$sql = mysql_query("SELECT * FROM question") or die(mysql_error());
$i = '1';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="save_feedback.php" method="post">
<H2> How Will You Rate This Service</H2>

<?php
while($row = mysql_fetch_array($sql))
{

echo  $row['question'];

  $comp = $row['id'][$i];

 // $_SESSION['comp']= $comp;

?>
<br />
<?php


echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='1' >1";
echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='2' >2";
echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='3' >3";
echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='4' >4";
echo "<td> <input type='radio' name='answer[".$row['id']."]'  value='5' >5";

echo "<input type='hidden' name='question_id'  value=$comp >";


echo'<br/>';
$i++;
}
?>
<br/>

<strong> NOTE:</strong> 1 - Very Poor, 2 - poor, 3 - Okay, 4 - Fair, 5 - Good
<p>

<input name="submit" type="submit" value="submit" />
</form>
</body>
</html>

And save the data file

<?php
session_start();

$answer  = $_POST['answer'];
$question_id = $_POST['question_id'];

include('config/manpower_db.php');



            foreach($hobb as $key=>$val)
{
    $var1=$hobb[$key];
    $var2=$answer[$key];


            //include ('connect.php');
            //include ('mysql_connect.php');
            $table = "INSERT INTO answer (answer) ".
                     "VALUES ('$var2')";
            mysql_query($table) or die(mysql_error());
            $inserted_fid = mysql_insert_id();
            mysql_close();  
        }



echo'<script>alert("Inserted Successfully")</script>'; 


?>
+3
source

pass id in the hidden field if there is only one button, then assign your id to the button value

+1
source

, <input> , , . - , $_POST['answer1'];, $_POST['answer2']; ..

[".$row['id']."] , $answer = $_POST['answer']; , , .

Remember that you pass the input attribute valueto the server and refer to it with the attribute name.

+1
source

I assume that $row['id']this is a unique number pulled from the table questions, so this will be the identifier of the question.

So, in your save file from this line.

$var2=$answer[$key];

$key - The number of the question that can be used to save it.

0
source

Source: https://habr.com/ru/post/1606580/


All Articles