Variable variables!
Moderator: General Moderators
-
spacebiscuit
- Forum Contributor
- Posts: 390
- Joined: Mon Mar 07, 2005 3:20 pm
Variable variables!
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.
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!
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!
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!
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Variable variables!
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.
Re: Variable variables!
I agree, this is not a good way.
Pass an array to your function and skim through that.
Then you can do this.
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
}- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Variable variables!
Not what I meant. Use arrays in the HTML form:
Then you can loop through the $_POST array or the $something array. I would use foreach().
Code: Select all
<input type="text" name="something[]">
<!-- or -->
<input type="text" name="something[whatever][]">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!
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.
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!
Not sure if it will help or not but try this
Try that and let me know
Code: Select all
$query="UPDATE " . $_POST['table'] . " SET title= '" . $_POST['title'] . "' WHERE id= " . $_POST['id1'];
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: Variable variables!
You need to quote text in a SQL query. Also, this is very insecure and opens you up to SQL injection attacks. Try:
-or-
Code: Select all
$query = "UPDATE `" . mysql_real_escape_string($_POST['table']) . "` SET `title` = '" . mysql_real_escape_string($_POST['title']) . "' WHERE `id` = " . (int)$_POST['id1'];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!
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.
I'm having a bad day today and I'm unable to think straight.
Thanks for your help!
Rob.