Jump to content

Inserting records based on two recordsets and array


garian_of_brunlea

Recommended Posts

Hiya

 

I have 3 tables

 

Campaign

campID

camp

 

Questions

questID

campID

quest

 

Answers

ansID

questID

answer

 

Once someone select the campaign it displays all the questions and asnwers as multiple choice radio box. It then displays a form that has

 

Question

All the answers with radio box to select

 

I post these to a process page where i need to add campID, questID and ansID to a results table.  I have it so the results pull out everything but the questID.

 

The process page is below the form i use to display the questions.  Also, I can get the results to print but when i try to insert them, i get an error.

 

======================================================================

 

<form id="form1" name="form1" method="post" action="process.php">
<?php if ($totalRows_rsQuest > 0) { // Show if recordset not empty ?><table width="700" border="0" cellspacing="0" cellpadding="0">
      <?php do { 
  $quest=$row_rsQuest['questID'];
 		mysql_select_db($database_camp, $camp);
		$query_rsAns = "SELECT * FROM answer WHERE questID = $quest";
		$rsAns = mysql_query($query_rsAns, $camp) or die(mysql_error());
		$row_rsAns = mysql_fetch_assoc($rsAns);
		$totalRows_rsAns = mysql_num_rows($rsAns);
  ?>
        <tr>
            <td bgcolor="#EFEFEF"><strong><?php echo $row_rsQuest['questID']; ?></strong>  <?php echo $row_rsQuest['question']; ?> <br>
            <br>
		<table width="700" border="0" cellspacing="0" cellpadding="0">
				 <?php do { ?>
   				 			<tr>
      			 			<td bgcolor="#FFFFFF"><label>
      			 			<input name="box1[]" type="checkbox" id="box1[]" value="<?php echo $row_rsAns['ansID']; ?>" />
      			 			</label>
      			 			  <?php echo $row_rsAns['answer']; ?>
      			 			  <input name="questID" type="hidden" id="questID" value="<?php echo $row_rsAns['questID']; ?>" />
      			 			  <input name="campID" type="hidden" id="campID" value="<?php echo $row_rsQuest['campID']; ?>" />
      			 			  <input name="ansID" type="hidden" id="ansID" value="<?php echo $row_rsAns['ansID']; ?>" /></td>
   				 			</tr>
    			<?php } while ($row_rsAns = mysql_fetch_assoc($rsAns)); ?>
		</table></td>
        </tr>
        <tr>
          <td><hr size="1" noshade="noshade" /></td>
        </tr>
        <?php } while ($row_rsQuest = mysql_fetch_assoc($rsQuest)); ?>
      <tr>
        <td></td>
      </tr>
    </table><?php } // Show if recordset not empty ?><input name="Submit" type="submit" value="Submit" />
</form>

 

Process page

 

<?php require_once('Connections/camp.php');
//
$camp=$_POST['campID'];
$box=$_POST['box1']; 
$ans=$_POST['ansID'];
$questID=$_POST['questID'];
foreach ($box as $answer){
//testing results
echo "campaign ".$camp."<br>";
echo "Question ".$questID."<br>";
	echo "answerID ".$answer."<br>";
	echo "<br><br>";
	//mysql_select_db($database_camp, $camp);
	//$query_RersRes = "INSERT into results (campID, questID, ansID) VALUES ('$questID', '$camp', '$answer')";
	//$RrsRes = mysql_query($query_RrsRes, $camp) or die(mysql_error());
}     
//header("Location: success.php");
?>

 

you probably get the errors because of this part here

 

$camp=$_POST['campID']; //i take it this is not a connections script???

mysql_select_db($database_camp, $camp);
$query_RersRes = "INSERT into results (campID, questID, ansID) VALUES ('$questID', '$camp', '$answer')";
$RrsRes = mysql_query($query_RrsRes, $camp) or die(mysql_error());

 

i think you have got the connections script

require_once('Connections/camp.php');

mixed up with the variable you have set above

 

mysql_select() and mysql_query() do not need another argument that is a variable and not a connection to work with try this

 

mysql_select_db($database_camp);
$query_RersRes = "INSERT into results (campID, questID, ansID) VALUES ('$questID', '$camp', '$answer')";
mysql_query($query_RrsRes) or die(mysql_error());

I tried that but get an error "query is empty"

$camp=$_POST['campID'];  was not a connection string, ity just grabs the campID from the form.

 

 

Here is the new code.

<?php require_once('Connections/camp.php');
//
$camp=$_POST['campID'];
$box=$_POST['box1']; 
$ans=$_POST['ansID'];
foreach ($box as $answer){
$split = explode(",", $answer);
$answ = $split[0]; 
$quest = $split[1]; 
echo "campaign ".$camp."<br>";
echo "Question ".$quest."<br>";
	echo "answerID ".$answ."<br>";
	echo "<br><br>";
mysql_select_db($database_camp);
$query_RersRes = "INSERT into results (campID, questID, ansID) VALUES ('$camp', '$quest', '$answ')";
mysql_query($query_RrsRes) or die(mysql_error());

}     
header("Location: success.php");
?>

 

What I suggest now is for you to echo out the query and see if it makes sense

instead of

$query_RersRes = "INSERT into results (campID, questID, ansID) VALUES ('$camp', '$quest', '$answ')";

 

put

echo "INSERT into results (campID, questID, ansID) VALUES ('$camp', '$quest', '$answ')";

 

you will see what the query is that the system is trying to run, strange to get an "empty Query" from an insert statement.

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.