Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using dynamic variable names in mysql statement

Status
Not open for further replies.

bezierstek

Programmer
Aug 30, 2007
43
FR
Hi,

I need to enter some data into a mysql database, but one of the fields uses a variable which is dynamically created by php.

when I execute the script it gives me a error. I have echoed the sql and it gives me
Code:
insert into photos (pid, photo, photoalt) values('9', V9_1.jpg', '$caption1')

Obviously I want it to use the value of the variable $caption1 rather than the variable name.

the code that creates the dynamic variable name is:
Code:
$picturenumber="picture" . $piccounter;
	$picturealt="caption".$piccounter;
	
	$imagesql="insert into photos (pid, photo, photoalt) values('$propertypid', $newfilename', '$$picturealt') ";

Cana nyone help me on this one?

Thanks
 
You have one dollar sign too many by the name. [tt][!]$[/1]$picturealt[/tt]

___________________________________________________________
[small]Do something about world cancer today: PACT[/small]
 
The extra $ sign is to tell it to use the value of $picturealt as the variable name (caption1 in this case). I want to value of $caption1 to be the value entered in the database, and the variable name $caption1 is generated by the code I posted along with a bit of maths to create the number in variable name.

 
It's alright now. I changed it so the caption variables were in an array and used that to enter the data into the database. Probably what I should have done in the first place!
 
It should not work
you are misssing the first single quote on v9...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top