PDA

Click to See Complete Forum and Search --> : Inserting multiple rows of data*RESOLVED*


Muk108
Sep 27th, 2003, 02:53 PM
I am trying to insert multiple rows of data at the same time by having the admin enter in all the information. its all going to the same table. i was wondering if there is a way to do it...? if you need to see how i am doing it now, ill show you if you ask.

kows
Sep 27th, 2003, 05:12 PM
show us some code..

Muk108
Sep 27th, 2003, 09:40 PM
here is how i am diong it now...

<?
if (isset($_POST['a_sbmt'])) {
$a_raceid = $_POST['a_meet_id'];
$a_rfname = $_POST['a_rfname'];
$a_rlname = $_POST['a_rlname'];
$a_time = $_POST['a_time'];
$a_grade = $_POST['a_grade'];
$a_cap = $_POST['a_cap'];
$a_var = $_POST['a_var'];
?>
<html><head><title>Data Inserted</title><META HTTP-EQUIV="Refresh" CONTENT="0; admin_times.php"></head><body><br />Thank You For Adding your information.<a href=admin_team.php>Click Here</a>to return.</body></html>
<?
$m_date = date("D M j G:i:s Y");//
$insert_meet = "INSERT INTO times (`times_id`,`times_raceid`,`times_rfname`,`times_rlname`,`times_time`,`times_grade`,`times_cap`,`tim es_var`) VALUES ('','$a_raceid','$a_rfname','$a_rlname','$a_time','$a_grade','$a_cap','$a_var')";
mysql_query($insert_meet) or die(mysql_error());

} else {

then it does the form stuff...

any i want it to first ask for how many entrys there are going to be then i want it to display my form that many times so i dont have to keep reloading the page. can ya help me with that?

Phenix
Sep 28th, 2003, 10:38 AM
"INSERT INTO times (`times_id`,`times_raceid`, ...)
VALUES ('','$a_raceid1' , ...) , ('','$a_raceid2' , ...) , ..."

In your case, you could ask for how many records will be added, like you said, then enumerate the form field names:

record1:
...name=a_meet_id1
...

record2:
...name=a_meet_id2
...

record3:
...name=a_meet_id3

Then use...
$a_raceid1 = $_POST['a_meet_id1'];
$a_raceid2 = $_POST['a_meet_id2'];
$a_raceid3 = $_POST['a_meet_id3'];

along with your new multiple INSERT INTO statement (also dynamically created).

kows
Sep 28th, 2003, 12:25 PM
This is probably written pretty poorly, but it works for what I think you want to do..

If you aren't sure what it does, check this out:
http://blast.complex-sys.com/php/multiform/multiform.php
Feel free to try it out, although I limited the $_GET['num'] value to 25 because I didn't want my server to get any 1000+ form requests..


<?
//set how many forms to show
if(isset($_GET['num']) && is_numeric($_GET['num']) && $_GET['num'] > 0){
$forms = $_GET['num'];
}else{
$forms = 1;
}
?>
<form method='get'>
Showing <input type='text' name='num' value='<?=$forms;?>' size='2'> forms &nbsp;&nbsp;&nbsp; <input type='submit' value='update'><br>
</form>
<hr width='25%' align='left'>
<?
$m_date = date("D M j G:i:s Y");
if(isset($_POST['a_sbmt'])){
//do nothing
}else{
echo "<form method='post'>\n";
}
for($i = 1; $i <= $forms; $i++){
if(isset($_POST['a_sbmt'])){
//define form variables
$a_raceid = $_POST['frm' . $i . '_a_meet_id'];
$a_rfname = $_POST['frm' . $i . '_a_rfname'];
$a_rlname = $_POST['frm' . $i . '_a_rlname'];
$a_time = $_POST['frm' . $i . '_a_time'];
$a_grade = $_POST['frm' . $i . '_a_grade'];
$a_cap = $_POST['frm' . $i . '_a_cap'];
$a_var = $_POST['frm' . $i . '_a_var'];
//construct MySQL query
$insert_meet = "INSERT INTO times ";
$insert_meet .= "(`times_id`,`times_raceid`,`times_rfname`,`times_rlname`,`times_time`,`times_grade`,`times_cap`,`tim es_var`) ";
$insert_meet .= "VALUES ('','$a_raceid','$a_rfname','$a_rlname','$a_time','$a_grade','$a_cap','$a_var')";
echo "form $i = submitted<br><br>";
// mysql_query($insert_meet) or die(mysql_error());
}else{
echo " <b>form $i</b><br><br>\n";
echo " <blockquote>\n";
echo " a_meed_id: <input type='text' name='frm" . $i . "_a_meet_id' size='4' maxlength='2'><br>\n";
echo " a_rfname: <input type='text' name='frm" . $i . "_a_rfname' size='15' maxlength='20'><br>\n";
echo " a_rlname: <input type='text' name='frm" . $i . "_a_rlname' size='15' maxlength='20'><br>\n";
echo " a_time: <input type='text' name='frm" . $i . "_a_time' size='10' maxlength='20'><br>\n";
echo " a_grade: <input type='text' name='frm" . $i . "_a_grade' size='5' maxlength='10'><br>\n";
echo " a_cap: <input type='text' name='frm" . $i . "_a_cap' size='10' maxlength='10'><br>\n";
echo " a_val: <input type='text' name='frm" . $i . "_a_var' size='10' maxlength='10'><br>\n";
echo " </blockquote>\n";
echo " <hr width='25%' align='left'>\n\n";
}
}
if(isset($_POST['a_sbmt'])){
//do nothing
}else{
echo " <br><br>\n";
echo " <input type='submit' name='a_sbmt' value='submit'>\n";
echo "</form>\n";
}
?>

Muk108
Sep 28th, 2003, 09:23 PM
the maltiple forms parts works but it only inputs the first set, i acnt get it to input anymroe....

kows
Sep 28th, 2003, 10:14 PM
oh, hmm, maybe you should uncomment the query. I commented it out because I didn't actually set up a database and I would get an error if I didn't take it out, so that just might be your problem.

Muk108
Sep 29th, 2003, 05:47 AM
i did uncomment it but it only inputs the first form.

kows
Sep 29th, 2003, 11:59 AM
you could just not query it in the first loop, make the query into an array, and then query it later on.. sort of like this:


//construct MySQL query
$insert_meet[$i] = "INSERT INTO times ";
$insert_meet[$i] .= " (`times_id`,`times_raceid`,`times_rfname`,`times_rlname`,`times_time`,`times_grade`,`times_cap`,`tim
es_var`) ";
$insert_meet[$i] .= "VALUES ('','$a_raceid','$a_rfname','$a_rlname','$a_time','$a_grade','$a_cap','$a_var')";


and later on, after the FOR() loop and maybe even after the whole form is made, you can query the whole array


for($i = 0; $i < count($insert_meet); $i++){
mysql_query($insert_meet[$i]) or die(mysql_error());
}


I would test it myself but I'm at school right now and don't have access to an FTP client to upload scripts anywhere..

Muk108
Sep 29th, 2003, 03:06 PM
ok i fixed it the the inserts needed the [$i] but the mysql_query() didnt need the for statment.