Jump to content

Multiple inserts problem


razorlegacy

Recommended Posts

I want to insert these variables into multiple tables

I have these numbered to 50

I want all 50 to be on one page and questions will go into questions table. I might have made a mistake with a proper structure for the tables.

 

Questions Table

CREATE TABLE `questions` (
  `qid` int(11) NOT NULL auto_increment,
  `test_id` int( NOT NULL default '0',
  `question` text NOT NULL,
  `img` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`qid`),
  KEY `test_id` (`test_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Answers Table

CREATE TABLE `answers` (
  `aid` int(11) NOT NULL auto_increment,
  `test_id` int(11) NOT NULL default '0',
  `qid` int(11) NOT NULL default '0',
  `answers` text NOT NULL,
  PRIMARY KEY  (`aid`),
  KEY `test_id` (`test_id`,`qid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Correct Answers Table

CREATE TABLE `correct_answer` (
  `caid` int(11) NOT NULL auto_increment,
  `test_id` int(11) NOT NULL default '0',
  `qid` int(11) NOT NULL default '0',
  `c_a` int(11) NOT NULL default '0',
  `explained` text NOT NULL,
  `ca_img` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`caid`),
  KEY `test_id` (`test_id`,`qid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Form Structure

tst.png

<h3>Add Questions</h3>
<form id="insert" name="insert" method="post" action="http://www.mikeonlinetests.com/admin/questions.php">
Select a Test <select name="test_id">
<?
$sql = mysql_query("SELECT * FROM tests ORDER BY version ASC");
$tests = array();
while ($test = mysql_fetch_assoc($sql)) {
$tests[] = sprintf("<option value=\"%s\">%s->%s-%s</option>\n", 
$test['test_id'], $test['version'], $test['date_m'], $test['date_y']); } echo '' . join('' , $tests);
?>
</select>
<table width="100%" border="0" cellspacing="0" cellpadding="4">
<? $y = 1; while($y < 51) { ?>
<tr>
<td width="10" align="right" valign="top"><b><?php echo $y; ?>)</b></td>
<td>Questions<br/>
<textarea name="question" cols="60" rows="2"></textarea><br/>
Image<br/>
<input name="img" type="text" size="40" maxlength="50"/><br/><br/>
Answers<br/>
<? $a = 1; while($a < 6) { ?>
<b><?php echo $a; ?>)</b> <input name="answer" type="text" size="30" maxlength="40"/> <input name="c_a" type="radio" value="<?php echo $a; ?>"/><br/>
<? $a++; } ?><br/>
</td></tr>
<? $y++; } ?>
</table>
</form>

Not sure how to do all of the inserts at once. I was thinking of a foreach statement but not sure how to start it and/or it should be looped.

 

Thank you all in advance.

Link to comment
Share on other sites

The first thing to look at is how to distinguish all your inputs.  After that you can look at how to insert it into the database.  Have you decided how to distinguish the inputs yet?

 

PHP will create arrays for you if you give variables names like "var[]" in the html.  Two alternatives are to parse the form input yourself, or to give each variable from each question a distinct variable name (including the question number).

Link to comment
Share on other sites

specifically how can I do it??

 

how would I code it?

 

I know it can be done but how can I get the question numbers in everything and correct answers with question number.

 

OR

 

Should I just make one table with question, answers, and correct answer and then have that loop but will it slow down the  server and is it efficient?

Link to comment
Share on other sites

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.