Reputation: 65
Am trying to save the result of the the selected answer with the question id. don't know how to pass the id to the DB. here's 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>
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>';
?>
This works but am trying to save the question id. don't know how to pass that to the database. thanks in advance
Upvotes: 3
Views: 944
Reputation: 106
I assume $row['id']
is a unique number pulled from the questions
table, so that would be the question id.
So in your save file from this line.
$var2=$answer[$key];
$key
is question number, which can be used to save it.
Upvotes: 0
Reputation: 727
Your problem is that you're assigning a different <input>
name for each row, meaning each input is essentially a different question. Because of this, each answer will be being sent to the server, for example $_POST['answer1'];
, $_POST['answer2'];
, etc.
Remove [".$row['id']."]
from your inputs and $answer = $_POST['answer'];
will result in only the selected input value being stored, and thus entered into the database.
Remember, you're passing the input value
attribute to the server and referencing it with the name
attribute.
Upvotes: 1
Reputation: 75
pass id in hidden field if there is only one button then assign your id to value of button
Upvotes: 1
Reputation: 986
Pass Id in hidden field
Try this
<?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 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>';
?>
Upvotes: 3