Jump to content

Recommended Posts

Hello all, Am trying to create a simple feedback form and also save the answers of each questions. i manage to get the system to save the answers but am not getting the id of my questions saved.. My code below

<?php

if($_POST)
{
$answer  = $_POST['answer'];
$hobb = $_POST['comp'];







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


          
			include('manpower_db.php');
			
            $table = "INSERT INTO answer (question_id,answer) ".
                     "VALUES ('$var1','$var2')";
            mysql_query($table) or die(mysql_error());
            $inserted_fid = mysql_insert_id();
            mysql_close();  
        }



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

}

?>

<?php
//session_start();
include('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="" 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='comp[".$row['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>
Link to comment
https://forums.phpfreaks.com/topic/300855-simple-feedback-form-issue/
Share on other sites

I have no idea what you mean by "not getting the id of my questions saved" since you don't show us any code involving "saving".

 

One thing that puzzles me is this:

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

 

The result of a fetch is a one-dimensional array. You are trying to use it with two. Try turning on php error checking (in my sign.) and see what error message(s) you get.

 

As for your question - please elaborate.

 

Upon Further Review:

 

You are placing your radio buttons in td elements but you have NOT built a proper html table. Is this the problem you are having - simply not SEEING the radio buttons with the id values you have assigned?

Edited by ginerjm

Upon EVER More Further Review:

 

I now see the insert. Don't know how I missed it. That leads to the question - you're showing us your process of some input, but not the actual html producing that input. Can we not see the incoming form for this script? The form you are sending out here I assumed was the one you are using for future input, but I see now that it is not.

Edited by ginerjm

am getting the questions from the DB and displaying each question with the radio button options.

<?php
//session_start();
include('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="" 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='comp[".$row['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>

the other part is to save the result .

<?php
session_start();
if($_POST)
{
$answer  = $_POST['answer'];
$hobb = $_POST['comp'];







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


          
			include('manpower_db.php');
			
            $table = "INSERT INTO answer (question_id,answer) ".
                     "VALUES ('$var1','$var2')";
            mysql_query($table) or die(mysql_error());
            $inserted_fid = mysql_insert_id();
            mysql_close();  
        }



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

}

?>

Now that I understand. So the value of $comp might be not what you want. As I see it the key value of your 'answer[]' array is the id you want. Yet you are using the comp input which I believe you have appeneded an extra value to.

 

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

 

is going to give you this

 

assume id='1' from your table and $i is 3. $comp will end up being invalid since $row is not a 2 dimensional array.

 

Skip the comp element and just use the $k value you already have.

The code makes no sense whatsoever. I recommend you start with the basics of PHP before you jump to more advanced topics like form processing.

 

 

 

assume id='1' from your table and $i is 3. $comp will end up being invalid since $row is not a 2 dimensional array.

 

$row['id'] is a string, so $row['id'][$i] is technically valid -- until you hit the string limit.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.