|
-
Oct 15th, 2007, 12:20 AM
#1
Thread Starter
Member
Arrayed form block, now want to insert using array
What i'm trying to do is take a form block that auto generate the number of rows I need and have my sql statements insert it into my db using an array.
Here's what I have :
(The page where date, number of people and type is shown):
PHP Code:
Training/Event Update<br><br>
<FORM METHOD="POST" ACTION="do_addrecord.php">
Date (mm/dd/yyyy):<br>
<INPUT TYPE="text" NAME="date" SIZE=30><br><br>
Number of Members Present:<br>
<INPUT TYPE="text" NAME="num_ppl" SIZE=30><br><br>
Training Type:<br>
<INPUT TYPE="text" NAME="trg_type" SIZE=30><br><br>
<INPUT TYPE="submit" NAME="submit" VALUE="Go to next Step">
</FORM>
(The form page):
PHP Code:
<?php
$form_block = "
<FORM METHOD=\"POST\" ACTION=\"do_addrecord2.php\">
<INPUT TYPE=\"hidden\" NAME=\"date\" VALUE=\"$_POST[date]\">
<INPUT TYPE=\"hidden\" NAME=\"type\" VALUE=\"$_POST[trg_type]\">
<TABLE CELLSPACING=5 CELPADDING=5>
<TR>
<TH>Member Name</TH><TH>Helmet Number</TH><TH>Training Type</TH>
</TR>";
for ($i = 0; $i <$_POST[num_ppl]; $i++) {
$form_block .= "
<TR>
<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"member_name[]\" SIZE=\"30\"></TD>
<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"helmet_num[]\" SIZE=\"5\"></TD>
<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"trg_type[]\" SIZE=\"30\" VALUE=\"$_POST[trg_type]\"></TD>
</TR>";
}
$form_block .= "
<TR>
<TD ALIGN=CENTER COLSPAN=3><INPUT TYPE=\"submit\" VALUE=\"Add Training Record\"></TD>
</TR>
</TABLE>
</FORM>";
?>
and here's the part where it's sopposed to use an array to feed the db but I'll be honest...I'm learning this off the web and out of books and not sure what's not ticking...
PHP Code:
<?php
$db_name = "db";
$table_name = "table";
$connection = @mysql_connect("location", "name", "password") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
for ($i = 0; $i < count($_POST[num_ppl]); $i++) {
$sql = "INSERT INTO $table_name (id, date, member_name, helmet_num, trg_type)
VALUES ('$_POST[date][$i]', '$_POST[member_name][$i]', '$_POST[helmet_num][$i]', '$_POST[trg_type][$i]')";}
$result = @mysql_query($sql, $connection) or die(mysql_error());
?>
What I'm trying to have happen is on the 2nd page once all the names n helmet numbers are entered, they are fed to the db. the id field is an auto-increment as well. Other than using tables instead of CSS, any ideas?
-
Oct 15th, 2007, 12:58 AM
#2
Junior Member
Re: Arrayed form block, now want to insert using array
A couple of problems I see are:
$_POST variables need to have single quotes around the variable name like this, $_POST['date'], $_POST['member_name'], etc.
You also probably don't need so many slashes where there're double quotes. It's a string, let it be.
Try those out and see if these work.
-
Oct 15th, 2007, 01:14 AM
#3
Thread Starter
Member
Re: Arrayed form block, now want to insert using array
:/
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in do_addrecord2.php on line 8
This is what I changed it to....
PHP Code:
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
for ($i = 0; $i < count($_POST[num_ppl]); $i++) {
$sql = "INSERT INTO $table_name (id, date, member_name, helmet_num, type)
VALUES ('$_POST['date']'[$i], '$_POST['member_name']'[$i], '$_POST['helmet_num']'[$i], '$_POST['type']'[$i])";}
$result = @mysql_query($sql, $connection) or die(mysql_error());
?>
-
Oct 15th, 2007, 01:17 AM
#4
Junior Member
Re: Arrayed form block, now want to insert using array
 Originally Posted by Albatross
:/
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in do_addrecord2.php on line 8
This is what I changed it to....
PHP Code:
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
for ($i = 0; $i < count($_POST[num_ppl]); $i++) {
$sql = "INSERT INTO $table_name (id, date, member_name, helmet_num, type)
VALUES ('$_POST['date']'[$i], '$_POST['member_name']'[$i], '$_POST['helmet_num']'[$i], '$_POST['type']'[$i])";}
$result = @mysql_query($sql, $connection) or die(mysql_error());
?>
You need to turn query into this:
$sql = "INSERT INTO $table_name (id, date, member_name, helmet_num, type)
VALUES ('$_POST['date'][$i]', '$_POST['member_name'][$i]', '$_POST['helmet_num'][$i]', '$_POST['type'][$i]')";
Edit: What I did was encase the $_POST variable in single quotes, then encased the $_POST name in single quotes as well. You need that array identifier inside the quotes as well.
So they need to be like this '$_POST['date'][$i]'.
-
Oct 15th, 2007, 11:29 AM
#5
Thread Starter
Member
Re: Arrayed form block, now want to insert using array
I copied what you gave me (sorry about the delay, fell asleep at the wheel) but still came up with the same error. Not 100% certain what's not clicking correctly here :/
-
Oct 15th, 2007, 01:21 PM
#6
Junior Member
Re: Arrayed form block, now want to insert using array
You also have a $_POST variable in your for loop that doesn't have quotes in it. i.e. $_POST['num_ppl']
Code:
for ($i = 0; $i <$_POST[num_ppl]; $i++) {
$form_block .= "
<TR>
<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"member_name[]\" SIZE=\"30\"></TD>
<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"helmet_num[]\" SIZE=\"5\"></TD>
<TD ALIGN=CENTER><INPUT TYPE=\"text\" NAME=\"trg_type[]\" SIZE=\"30\" VALUE=\"$_POST[trg_type]\"></TD>
</TR>";
}
-
Oct 15th, 2007, 01:38 PM
#7
Thread Starter
Member
Re: Arrayed form block, now want to insert using array
Still same error. Only thing I can think of at this point is that the error is in my for statement here:
PHP Code:
for ($i = 0; $i <('$_POST['num_ppl']'); $i++) {
$sql = "INSERT INTO $table_name (id, date, member_name, helmet_num, type)
VALUES ('$_POST['date'][$i]', '$_POST['member_name'][$i]', '$_POST['helmet_num'][$i]', '$_POST['type'][$i]')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
}
By changing the top for statement, my error did change so I have to guess the error is there....but only so many different combos to use and not usre what else to try.
So far i've done these:
PHP Code:
for ($i = 0; $i <('$_POST['num_ppl']'); $i++) {
for ($i = 0; $i <($_POST['num_ppl']); $i++) {
for ($i = 0; $i <('$_POST[num_ppl]'); $i++) {
for ($i = 0; $i <count('$_POST['num_ppl']'); $i++) {
for ($i = 0; $i <count($_POST['num_ppl']); $i++) {
for ($i = 0; $i <count('$_POST[num_ppl]'); $i++) {
and the new error :
Parse error: syntax error, unexpected T_STRING in do_addrecord2.php on line 6.
Line 6 is the for statement.
-
Oct 15th, 2007, 02:20 PM
#8
Junior Member
Re: Arrayed form block, now want to insert using array
 Originally Posted by Albatross
Still same error. Only thing I can think of at this point is that the error is in my for statement here:
PHP Code:
for ($i = 0; $i < $_POST['num_ppl']; $i++) {
$sql = "INSERT INTO $table_name (date, member_name, helmet_num, type)
VALUES ('$_POST['date'][$i]', '$_POST['member_name'][$i]', '$_POST['helmet_num'][$i]', '$_POST['type'][$i]')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
}
When using the $_POST variables outside of a string, they don't need to be encased in single quotes. All you need to use is $_POST['num_ppl'].
OH! And I removed the ID from your INSERT query. Since when it activates, it would put date into the ID field.
-
Oct 15th, 2007, 06:37 PM
#9
Thread Starter
Member
Re: Arrayed form block, now want to insert using array
Using a combo of that and some jigging around, I have it so the script will at least finish now....but not post to the db.
Here's the result so far....just a note though....the single quote around the braces would not allow the script to finish. error checking wise, it means my entire layout is incorrect, or else the for statement is. Problem is I dont know which....
PHP Code:
for ($i = 0; $i < ($_POST[num_ppl]); $i++) {
$sql = "INSERT INTO $table_name (date, member_name, helmet_num, trg_type) VALUES
('$_POST[date][$i]', '$_POST[member_name][$i]', '$_POST[helmet_num][$i]', '$_POST[trg_type][$i]')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
}
?>
<?php
include("header.php");
?>
Records added for <?php echo "$_POST[date]"; ?>.
<?php
include("footer.php");
?>
-
Oct 20th, 2007, 02:10 PM
#10
Thread Starter
Member
Re: Arrayed form block, now want to insert using array
I know this one shoud be easy but if anyone has a moment, could I get another opinion on what I'm doing wrong here. I've been experimenting here for a couple days now and it's not exactly moving forward.
Not sure what else to try....the code I'm using is in my initial posts (thought it would help).
Hopin...
-
Oct 20th, 2007, 09:50 PM
#11
Re: Arrayed form block, now want to insert using array
Your HTML is appalling. Element and attribute names should be in lowercase as a matter of good style, and never use echo() for anything other than debugging purposes. You should also never use die() in a production environment.
Fix those issues and then we can work on your original problem. You may even fix it in the process.
-
Oct 21st, 2007, 09:05 PM
#12
Thread Starter
Member
Re: Arrayed form block, now want to insert using array
Hmmm....perhaps I should have looked for abig ol rubber stamp that said this isn't my strong point.
Forget it....having insults thrown at attempts isn't what I came looking for.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|