Jump to content

Form Submission array handling


MrsTupid

Recommended Posts

I have a form that is a survey which has a variable number of questions in it. There are two types of question in it, rating questions (under which a variable number of people are rated) and text questions.

 

So for each rating question I need to insert into a table called FeedbackRating

 

FeedbackID

QuestionID passed through as RQuest (and then an incrementing number on the end)

LecturerID passed through as LecsID (" same as above, eg first will be LecsID1, LecsID2...)

RatingValue passed through as Rat (" same as above Rat1, Rat2, Rat3)

 

So what I have created is this:

 

......

<?php

$FeedbackID = mysql_insert_id();  # fetch the newly generated id



$RatingQuests = array();

for($i=1;$i<=40;$i++)  # create array with 1-40 elements
if(isset($_POST['Rat'.$i]) and ($_POST['Rat'.$i] > ""))
$RatingQuests[] = '('.$FeedbackID.',"'.($_POST['Rat'.$i]).'")';
$values = implode(',',$RatingQuests);

$sql2 = "INSERT INTO FeedbackRating (FeedbackID,RatingValue) VALUES $values";

?>

 

What this does is cycle through all the Rat posts and inserts them all fine. However I can't work how how to expand this to do the RQuest and LecsID.

 

I thought it might be something like this however it doesn't work

 

<?php
$FeedbackID = mysql_insert_id();  # fetch the newly generated id

$RatingQuests = array();

for($i=1;$i<=40;$i++)  # create array with 1-40 elements
if(isset($_POST['RQuest'.$i]) and ($_POST['RQuest'.$i] > "")) && isset($_POST['LecsID'.$i]) and ($_POST['LecsID'.$i] > "")) && isset($_POST['Rat'.$i]) and ($_POST['Rat'.$i] > "")))
$RatingQuests[] = '('.$FeedbackID.',"'.($_POST['RQuest'.$i]).'","'.($_POST['LecsID'.$i]).'","'.($_POST['Rat'.$i]).'")';
$values = implode(',',$RatingQuests);

$sql2 = "INSERT INTO FeedbackRating (FeedbackID,QuestionID,LecturerID,RatingValue) VALUES $values";

?>

 

Hope I have explained this well enough anyway. Any thoughts would be great

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/149649-form-submission-array-handling/
Share on other sites

Hi

 

You have an odd set of mismatching brackets.

 

Should be:-

 

if(isset($_POST['RQuest'.$i]) and $_POST['RQuest'.$i] > "" and isset($_POST['LecsID'.$i]) and $_POST['LecsID'.$i] > "" and isset($_POST['Rat'.$i]) and $_POST['Rat'.$i] > "")

 

All the best

 

Keith

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.