Jump to content

switch and insert into table problem


gerry_kef

Recommended Posts

Hi everyone. I want to build a questionnaire with php. My problem is that the second page where i insert the results into the database doesnt work. Any help will be much appreciated. Thanx in advance.

 

 

 

<?php

$title = "Questionnaire";
include("../inc.header.php");


$fct = isset($_GET['fct']) ? $_GET['fct'] : null;

/********************************************************/
if(!isset($_SESSION['id_project'])){ $_SESSION['id_project'] = "1"; }
/********************************************************/

switch($fct){
case "post":
$proj_id = 	$_SESSION['id_project'];
$post = $_POST['q'];
foreach ($post as $result => $value)
{
foreach ($result as $result2 => $value2)
	{
$sql="INSERT INTO answers (id,q_id,result, proj_id) VALUES ('',$result2,$result,$proj_id)";
$result = mysql_query($sql);
	}
}


break;
default:
	?>
	<form name="form" method="post" action="index.php?fct=post">

		<table border="0" class="table" width="100%">
		<?php
		$sql = mysql_query("SELECT * FROM questions");
		$num = mysql_num_rows($sql);
		if($num > 0){
			$i = 1;
			while($row = mysql_fetch_array($sql)){
				$id = $row['id'];
				echo "<tr><td>".$i."</td><td>".$row['question']."</td>";
				echo "<tr><td><input id= ".$i." type=\"radio\" name=q[".$i."][".$id."] value=\"1\" /> Agree </td>";
				echo "<tr><td><input id= ".$i." type=\"radio\" name=q[".$i."][".$id."] value=\"2\" /> Strongly Agree  </td>";
				echo "<tr><td><input id= ".$i." type=\"radio\" name=q[".$i."][".$id."] value=\"3\" /> Disagree </td>";
				echo "<tr><td><input id= ".$i." type=\"radio\" name=q[".$i."][".$id."] value=\"4\" /> Strongly Disagree  </td>";
				echo "<tr><td><input id= ".$i." type=\"radio\" name=q[".$i."][".$id."] value=\"5\" /> Do not have an opinion  </td>";
				$i++;
			}
		}
	echo "<tr><td><input type=\"submit\">";
	echo "</form>";
	echo "</table>";
}
?>
<br /><br />
</div>
<?php
include("../inc.footer.php");
?>



Link to comment
https://forums.phpfreaks.com/topic/49054-switch-and-insert-into-table-problem/
Share on other sites

it looks like you are trying to insert the array $result as the third value, try serializing it before insertion and then unserialize it when you need to retrieve it  :)

 

$str = serialize($result);
$sql="INSERT INTO answers (id,q_id,result, proj_id) VALUES ('',$result2,$str,$proj_id)";

Actually what i am trying to do( so successfully.... :-\) is to insert into the table with the answers

1.the corresponding value depending on the answer given, for example 1 for Agree, and

2. the id of the question.

 

In the radio button form i use q[][] in order to be able to print multiple questions and check more than one radio button......

 

I tried serializing but i didnt work. Thanks for helping!!!!!!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.