Jump to content

Archived

This topic is now archived and is closed to further replies.

elie

problem with looping textfields

Recommended Posts

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\" /]

Share this post


Link to post
Share on other sites
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: [code]<?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'/>";
}
?>[/code] 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: [code]<?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());
}
?>[/code] That should be enough of an idea to get you on your way.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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
[code]
<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>
[/code]

going to inventory1.php...
[code]
<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>
[/code]

and lastly, this starts my problem.. add_inventory.php
[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)";
}
...
?>
[/code]

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

Share this post


Link to post
Share on other sites
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')";

Share this post


Link to post
Share on other sites
[!--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) [snapback]374537[/snapback][/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?

Share this post


Link to post
Share on other sites
so post the actual error message. also, echo $sql and post what it outputs onto the screen

Share this post


Link to post
Share on other sites
okay... so here's my entire code...
[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!!!";
}
?>
[/code]

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

Share this post


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

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=374541:date=May 16 2006, 10:07 PM:name=elie)--][div class=\'quotetop\']QUOTE(elie @ May 16 2006, 10:07 PM) [snapback]374541[/snapback][/div][div class=\'quotemain\'][!--quotec--]
okay... so here's my entire code...
[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!!!";
}
?>
[/code]

the message i got was "Couldn't execute query."
doesn't it have something to do with [b]$num = $_POST['num'];[/b] ???
[/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?

Share this post


Link to post
Share on other sites
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\" /]

Share this post


Link to post
Share on other sites
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 :
[code]
.
.
.
$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());
}
.
.
.
[/code]

Share this post


Link to post
Share on other sites
thanks for the help guys... i kinda got it already, but only field_a values are stored in the database...

here's the code..
[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());
}

?>
[/code]

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

×

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.