Jump to content

variable amount of variables


billgod

Recommended Posts

If I knew what to call it I could have searched it.  So I will just explain.

 

I have a form that is pulling from a database to build the following.

echo <input name='$row[id]' type='text' id='$row[id]' size=2>;

 

This will build a page that has this

<input name='22' type='text' id='22' size=2>

<input name='54' type='text' id='54' size=2>

 

This part works great.  The problem is when I try to add it to the database.  How can I insert variables that I don't know the names of?  They will be something like $_POST[22]  $_POST[54] and so on.  I hope someone can understand my jibberish.

 

I am sure there is a WAY better way to do this.

Link to comment
https://forums.phpfreaks.com/topic/224794-variable-amount-of-variables/
Share on other sites

There are a few ways to do this.

 

1) You can store the values and pass them as a hidden field to the processing page.

2) (This is a bit unsafe) You can dynamically go through your post values:

foreach($_POST as $key =>$value)
{
   echo "post: $key => $value";
}

Here is my query I currently have

		$sql2="insert into tbl_orders (col_orderid, col_prodid, col_qty) values ( '$orderid', '$row[id]', $_POST[number])";
	$result2=mysql_query($sql2) or die ('Error: '.mysql_error ());

 

I need to be able to insert $_POST[22], $_POST[54] and so on.  I am not sure how your suggestion is going to accomplish this.  I need $_POST[number] to change with every loop.

 

Archived

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

×
×
  • Create New...

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.