Retrieving looped input text box data
Posted: Tue Apr 13, 2010 10:13 am
Hi,
I'm currently trying to use a form to add data into a product table, the table is customisable which means I can rarely hard code the names of the attributes that I need the user to fill in.
I have a form as follows:
and the code to accompany this after the add.png image is clicked is as follows:
As you can probably tell by the code, as I am using a variable in a loop, only the last piece of data stored in the ProductValue variable is being saved to the database.
If someone could assist me in getting all of the data stored in the database I would be very grateful.
Paul
I'm currently trying to use a form to add data into a product table, the table is customisable which means I can rarely hard code the names of the attributes that I need the user to fill in.
I have a form as follows:
Code: Select all
<?php
$result = mysql_query("SHOW COLUMNS FROM <variable named table> WHERE Field NOT IN ('Column1','Column2')");
$count = 0;
while ($row=mysql_fetch_row($result)){
$cnt = 0;
foreach ($row as $item){
if ($cnt == 0){
$cnames[$count] = $item;
$cnt++;
$count++;
}
}
}
?>
<?php
$i = 0;
foreach($cnames as $c){
echo $c?> <input name="ProductValue" type="text"><br>
<?php $i++?>
<?php } ?>
<input name="Save" type="image" src="images/add.png" align="right">
<?php } ?>
Code: Select all
$ProductValue = $_GET['ProductValue'];
$result = mysql_query("SHOW COLUMNS FROM ".$_SESSION['Username']."");
$count = 0;
while ($row=mysql_fetch_row($result)){
$cnt = 0;
foreach ($row as $item){
if ($cnt == 0){
$cnames[$count] = $item;
$cnt++;
$count++;
}
}
}
?>
<?php foreach($cnames as $c){
$query = "INSERT INTO <variable table name> (".$c.") VALUES ('$ProductValue')";
}
$result = @mysql_query($query, $connection)
or die ("Unable to perform query<br>$query");
header("Location: PRODUCTS.php");
exit();
?>
If someone could assist me in getting all of the data stored in the database I would be very grateful.
Paul