Retrieving looped input text box data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PMacca_1987
Forum Newbie
Posts: 5
Joined: Sun Apr 04, 2010 10:27 am

Retrieving looped input text box data

Post by PMacca_1987 »

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:

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 } ?>
and the code to accompany this after the add.png image is clicked is as follows:

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();
?>
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
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: Retrieving looped input text box data

Post by roders »

You can try this instead. I think this is what you're trying to achieve.

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[<?php echo $c;?>]" type="text"><br>
          <?php $i++?>
             <?php } ?>

         <input name="Save" type="image"  src="images/add.png" align="right">
         <?php } 
?>

Code: Select all

$ProductValue = $_GET['ProductValue'];

              <?php foreach($ProductValue as $name=>$value){
                  $query = "INSERT INTO <variable table name> (".$name.") VALUES ('".$value."')";
              }
                $result = @mysql_query($query, $connection)
                or die ("Unable to perform query<br>$query");
      header("Location: PRODUCTS.php");
        exit();
PMacca_1987
Forum Newbie
Posts: 5
Joined: Sun Apr 04, 2010 10:27 am

Re: Retrieving looped input text box data

Post by PMacca_1987 »

Hi Thanks for the reply.

I ran the code as stated and got the following 2 errors

Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\SALESDOCK\AddProductForm.php on line 21

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\SALESDOCK\AddProductForm.php on line 34

Is there anything I need to change in the code other than the variable table name to make it work?


Thanks,

Paul
Post Reply