Jump to content


Photo

problem with looping textfields


  • Please log in to reply
14 replies to this topic

#1 elie

elie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 16 May 2006 - 06:44 AM

hello everyone... i am quite new to php and i have a problem regarding php loops and arrays...
i want to have a form which will ask a user to input a number of 'set of fields' .. when the form is submitted, it will create a table with its rows(set of fields) being looped accdg. to the user input...

in my case i have 6 textfields; (field1, field2, field3, field4, field5, field6) = 1 set of fields... say if a user submits 4, it will create 4 rows(or sets of fields)

how do i implement this using arrays? another problem is how will i insert all the textfield values in mysql?

im so clueless... please help me! [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 16 May 2006 - 08:31 AM

I don't understand why, or how, you want to use arrays to do this, but to create the rows of fields you can just use a simple for() like so:
<?php
$num = 4;
for($i=0; $i<$num; $i++) {
    echo "<input type='text' name='field_a_$i'/>
    echo "<input type='text' name='field_b_$i'/>
    echo "<input type='text' name='field_c_$i'/>
    echo "<input type='text' name='field_d_$i'/>
    echo "<input type='text' name='field_e_$i'/>
    echo "<input type='text' name='field_f_$i'/>";
}
?>
At this point, you could pass $i as a hidden field to the next script so that you know how many rows you need to loop through. Then its just a case of looping through them:
<?php
$num = $_POST['num'];
for($i=0; $i<$num; $i++) {
    mysql_query("INSERT INTO `table` (`field_a`,`field_b`....) VALUES ('$_POST[field_a_$i]','$_POST[field_b_$i]'....)") or die(mysql_error());
}
?>
That should be enough of an idea to get you on your way.

#3 elie

elie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 May 2006 - 02:03 AM

hmmm... i tried it but it wouldn't work... it creates an error msg couldn't execute query...
why is that?? thanks for taking time to help me with this..

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 17 May 2006 - 02:08 AM

post your code so we can see what it looks like.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 elie

elie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 May 2006 - 02:41 AM

actually im using php 4.1.1, so i guess it has slight difference in their syntax... does it??

this is my first page... inventory.php
<form method="POST" action="inventory1.php">
<p>Number of equipments:<br><input type="text" name="num" size=5></p>

<input type="submit" value="Go">
</form>

going to inventory1.php...
<form method="POST" action="add_inventory.php">
<table>
<?php

for($i=0; $i<$num; $i++) {
    echo "<tr><td><input type='text' name='field_a_$i'/></td>";
    echo "<td><input type='text' name='field_b_$i'/></td>";
    echo "<td><input type='text' name='field_c_$i'/></td>";
    echo "<td><input type='text' name='field_d_$i'/></td>";
    echo "<td><input type='text' name='field_e_$i'/></td>";
    echo "<td><input type='text' name='field_f_$i'/></td></tr>";
}
?>

<tr>
<td><input type="submit" value="Next"></td>
</tr>
</table>
</form>

and lastly, this starts my problem.. add_inventory.php
<?php
$num = $_POST['num'];
for($i=0; $i<$num; $i++)
{
$sql="INSERT INTO inventory (field_a, field_b, field_c, field_d, field_e, field_f) VALUES ($field_a_$i, $field_b_$i, $field_c_$i, $field_d_$i, $field_e_$i, $field_f_$i)";
}
...
?>

the first two pages worked alright, but when i start inserting the values, it wouldnt execute... =(

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 17 May 2006 - 02:56 AM

first, try putting some ' ' around the variable names:

$sql="INSERT INTO inventory (field_a, field_b, field_c, field_d, field_e, field_f) VALUES ('$field_a_$i',' $field_b_$i', '$field_c_$i', '$field_d_$i', '$field_e_$i', '$field_f_$i')";
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 elie

elie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 May 2006 - 02:59 AM

[!--quoteo(post=374537:date=May 16 2006, 09:56 PM:name=Crayon Violent)--][div class=\'quotetop\']QUOTE(Crayon Violent @ May 16 2006, 09:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
first, try putting some ' ' around the variable names:

$sql="INSERT INTO inventory (field_a, field_b, field_c, field_d, field_e, field_f) VALUES ('$field_a_$i',' $field_b_$i', '$field_c_$i', '$field_d_$i', '$field_e_$i', '$field_f_$i')";
[/quote]

yep i already tried it.. still the same error i got.. hmm.. what could've been wrong?


#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 17 May 2006 - 03:01 AM

so post the actual error message. also, echo $sql and post what it outputs onto the screen
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 elie

elie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 May 2006 - 03:07 AM

okay... so here's my entire code...
<?php
$num = $_POST['num'];
for($i=0; $i<$num; $i++)
{
$sql="INSERT INTO inventory (field_a, field_b, field_c, field_d, field_e, field_f) VALUES ('$field_a_$i', '$field_b_$i', '$field_c_$i', '$field_d_$i', '$field_e_$i', '$field_f_$i')";
}

$connection = mysql_connect("localhost","root","") or die ("Couldn't connect to server.");
$db = mysql_select_db("investigatory", $connection) or die ("Couldn't select database.");
$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");

if (!sql_result)
{
echo "<p>Couldn't insert into table";
} 
else
{
echo "<p>created!!!";
}
?>

the message i got was "Couldn't execute query."
doesn't it have something to do with $num = $_POST['num']; ???

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 May 2006 - 03:31 AM

Change this line:
<?php $sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query."); ?>
to
<?php $sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query: $sql<br>" . mysql_error()); ?>
This will echo your generated query and the real mysql error message. Post that.

Ken

#11 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 17 May 2006 - 03:45 AM

[!--quoteo(post=374541:date=May 16 2006, 10:07 PM:name=elie)--][div class=\'quotetop\']QUOTE(elie @ May 16 2006, 10:07 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
okay... so here's my entire code...
<?php
$num = $_POST['num'];
for($i=0; $i<$num; $i++)
{
$sql="INSERT INTO inventory (field_a, field_b, field_c, field_d, field_e, field_f) VALUES (
}

$connection = mysql_connect("localhost","root","") or die ("Couldn't connect to server.");
$db = mysql_select_db("investigatory", $connection) or die ("Couldn't select database.");
$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");

if (!sql_result)
{
echo "<p>Couldn't insert into table";
} 
else
{
echo "<p>created!!!";
}
?>

the message i got was "Couldn't execute query."
doesn't it have something to do with $num = $_POST['num']; ???
[/quote]
your for loop is not doing anything in it except set $sql equal to that same query with the same values over and over 0 to num times. there's absolutely no point in that for loop, unless you meant for it to actually run the sql query that many times. in which case, you need to throw in a mysql_query ($sql); into that loop. but when you do, it will do this:

let's say

$num = 3
$field_a_$i = 1;
$field_b_$i = 2;
$field_c_$i = 3;
$field_d_$i = 4;
$field_e_$i = 5;
$field_f_$i = 6;

you will insert

1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6

into your database. is that what you ware wanting it to do?
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#12 elie

elie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 May 2006 - 04:15 AM

what i wanted to happen is that...

say $num=3, it will output...

field_a0, field_b0, field_c0, field_d0, field_e0, field_f0
field_a1, field_b1, field_c1, field_d1, field_e1, field_f1
field_a2, field_b2, field_c2, field_d2, field_e2, field_f2

a user enters anything into every field, which means each field has different values, say...

field_a0 = hello
field_b0 = world
.
.
.
field_f2 = goodbye

i want these values to be inserted into the database... is it possible to do such thing?? [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

#13 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 17 May 2006 - 04:54 AM

okay i don't know what your form script looks like, so you're gonna have to do this:

for instance, if field_a were a text input field

change this:

<input type='text' name='field_b_$i'/>

to this:

<input type = 'text' name = 'field_a[]'>

that is, add brackets [] after each name = '...[]' in your input tags (and take off the _$i on the end, if you have it).

then do :
.
.
.
$field_a = $_POST['field_a'];
$field_b = $_POST['field_b'];
$field_c = $_POST['field_c'];
$field_d = $_POST['field_d'];
$field_e = $_POST['field_e'];
$field_f = $_POST['field_f'];
$num = $_POST['num'];
foreach($field_a as $key => $val) {
{
$sql="INSERT INTO inventory (field_a, field_b, field_c, field_d, field_e, field_f) VALUES ('".$val."','".$field_b[$key]."','".$field_c[$key]."','".$field_d[$key]."','".$field_e[$key]."','".$field_f[$key]."')";
mysql_query($sql) or die(mysql_error());
}
.
.
.

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#14 elie

elie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 May 2006 - 05:34 AM

thanks for the help guys... i kinda got it already, but only field_a values are stored in the database...

here's the code..
<?php

$field_a = $_POST['field_a'];
$field_b = $_POST['field_b'];
$field_c = $_POST['field_c'];
$field_d = $_POST['field_d'];
$field_e = $_POST['field_e'];
$field_f = $_POST['field_f'];
$num = $_POST['num'];
foreach($field_a as $key => $val)
{
$sql="INSERT INTO inventory (field_a, field_b, field_c, field_d, field_e, field_f) VALUES 

('".$val."','".$field_b[$key]."','".$field_c[$key]."','".$field_d[$key]."','".$field_e[$key]

."','".$field_f[$key]."')";

$connection = mysql_connect("localhost","root","") or die ("Couldn't connect to server.");
$db = mysql_select_db("investigatory", $connection) or die ("Couldn't select database.");
mysql_query($sql) or die(mysql_error());
}

?>


#15 elie

elie
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 17 May 2006 - 03:34 PM

oh well... i got it solved! [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] jst made slight errors with the underscore thing...
thanks for the help guys! ur all geniuses!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users