I need some help please, to submit multiple rows from a form on a
website using PHP to insert data into a MySQL database table... I am
creating a basic PHP application which will enable a customer to
create "competitions" on their website.
Each competition has a different number of questions (typically 2-5)
which equates to a different number of input boxes (input type=text or
textareas, depending on the selection) - and for the competition entry
form, I need to have a dynamic number of fields depending how many
questions there are.
My problem is mirrored on the page which the admin will specify what
each question will be. It is this code I have pasted below, because
the page is less cluttered with other visual design elements.
The first page takes the name for the competition, and how many
questions it will have. These go into the comp_name table, which has
four columns - comp_id, comp_name, no_questions, and open_closed.
comp_id is an autoincrement field, and open_closed defaults to 2
('closed' - ie people won't be able to enter the competition). This
page is then Dreamweaver's "go to" page which is displayed after the
first page is submitted. It sorts the comp_name table DESC and LIMIT
1, so the only entry showing is the competition that was just created
on the previous page:
########################### code
<?php require_once('Connections/competitions.php'); ?>
<?php
mysql_select_db($database_competitions, $competitions);
$query_show_newest_competition = "SELECT * FROM comp_name ORDER BY
comp_id DESC LIMIT 1";
$show_newest_competition = mysql_query($query_show_newest_competition,
$competitions) or die(mysql_error());
$row_show_newest_competition = mysql_fetch_assoc($show_newest_competition);
$totalRows_show_newest_competition = mysql_num_rows($show_newest_competition);
?>
<html>
<head>
<title>Specifying questions for <?php echo
$row_show_newest_competition['comp_name']; ?></title>
<body>
<b>Specifying the <?php echo
$row_show_newest_competition['no_questions']; ?> question(s) for <?php
echo $row_show_newest_competition['comp_name']; ?></b>
<br>
This competition has <?php echo
$row_show_newest_competition['no_questions']; ?> questions
<br><br>
<form action="" method="post" name="form1">
<?php
$first_question = 1;
$last_question = $row_show_newest_competition['no_questions']+1;
$numbers = range($first_question,$last_question);
$size = count($numbers);
for ( $i=1; $i < $size; $i++ ) {
?>
<input name="comp_id<?php echo $i;?>" type="hidden" value="<?php echo
$row_show_newest_competition['comp_id']; ?>">
<input name="question_number<?php echo $i;?>" type="hidden"
value="<?php echo $i;?>">
<input name="question<?php echo $i;?>" type="text" value="Enter
Question <?php echo $i;?> here">
<select name="question_type<?php echo $i;?>">
<option value="1">text input box</option>
<option value="2">textarea multiline entry</option>
</select><br>
<?php } ?>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
<?php
mysql_free_result($show_newest_competition);
?>
###########################code
I'd like the fields of the form to be submitted to the table
'comp_questions'. comp_questions has five fields:
- comp_question_id (autoincrement)
- comp_id (from the form - the unique ID for the competition set up earlier)
- question (the actual question - eg "What is Michael Jackson's home town?")
- question_type (either 1 or 2... 1 signifies that <input type=text>
will be used on the entry form, 2 will make it a <textarea>
- question_number (the number of the question, ie Question 1, Question
2 etc... coming from the loop above)
I hope that is enough information
Thanks
Daniel
PS: I have also asked this question at
http://www.experts-exchange.com/Web/WebDevSoftware/DreamWeaver/Q_21017246.html
and while you don't have to take anything there into account, it may
provide some extra useful information when giving an answer.
http://www.experts-exchange.com/Web/WebDevSoftware/DreamWeaver/Q_21017246.html |