Variable variables!

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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Variable variables!

Post by spacebiscuit »

This is driving me crazy:

while( isset($id{${z}}) ){
do something;
$z++;
}

So whilist $id1, $id2 etc. is set do something.

What am I doing wrong - thanks,

Rob.
Last edited by spacebiscuit on Fri Feb 19, 2010 9:24 am, edited 1 time in total.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Variable variables!

Post by spacebiscuit »

Ok I think I have figured it out:

while( isset(${"{id}_${z}"}) ){

However, if I want to check for the $_POST variable (I have highlighted the code chages from the above snippet:

while( isset($_POST[{"{id}_${z}"}]) ){

And guess what? It does not work.

I'm lost!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Variable variables!

Post by AbraCadaver »

There's probably a better way to do this using arrays in your form, but to answer your question:

Code: Select all

while(isset($_POST["{$id}_{$z}"])) {
    //
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
snipered
Forum Newbie
Posts: 2
Joined: Mon Apr 21, 2008 9:17 am

Re: Variable variables!

Post by snipered »

I agree, this is not a good way.

Pass an array to your function and skim through that.
Then you can do this.

Code: Select all

$passedValue = $_POST['arrayPassed'];
 
for ( $i = 0; i < size_of( $passedValue ); $i++ )
{
//do something
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Variable variables!

Post by AbraCadaver »

Not what I meant. Use arrays in the HTML form:

Code: Select all

<input type="text" name="something[]">
 
<!-- or -->
 
<input type="text" name="something[whatever][]">
Then you can loop through the $_POST array or the $something array. I would use foreach().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Variable variables!

Post by spacebiscuit »

Thanks guys, that has done the trick.

Now I stumped by this:

$query="UPDATE $_POST[table] SET title=$_POST[\"title\"] WHERE id=$_POST[id1]";

The quotatins surrounding 'title' are causing the page to not load which make n sense to me.

Rob.
Benwahballz
Forum Commoner
Posts: 25
Joined: Mon Sep 21, 2009 12:54 pm

Re: Variable variables!

Post by Benwahballz »

Not sure if it will help or not but try this

Code: Select all

 
$query="UPDATE " . $_POST['table'] . " SET title= '" . $_POST['title'] . "' WHERE id= " . $_POST['id1'];
 
Try that and let me know
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Variable variables!

Post by AbraCadaver »

You need to quote text in a SQL query. Also, this is very insecure and opens you up to SQL injection attacks. Try:

Code: Select all

$query = "UPDATE `" . mysql_real_escape_string($_POST['table']) . "` SET `title` = '" . mysql_real_escape_string($_POST['title']) . "' WHERE `id` = " . (int)$_POST['id1'];
-or-

Code: Select all

$table = mysql_real_escape_string($_POST['table']);
$title = mysql_real_escape_string($_POST['title']);
$id1 = (int)$_POST['id1'];
 
$query = "UPDATE `$table` SET `title` = '$title' WHERE `id` = $id1";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Variable variables!

Post by spacebiscuit »

Thanks that did the trick guys.

I'm having a bad day today and I'm unable to think straight.

Thanks for your help!

Rob.
Post Reply