Forum Moderators: coopster

Message Too Old, No Replies

fieldnames from variables

         

anomalyguy

1:18 am on Feb 16, 2004 (gmt 0)

10+ Year Member



Hello all,

I've been working on a recent PHP/SQL project and ran into a bit of a snag.

Regularly I'll be updating some fields in the table, but not all of them. I didn't want to list input fields for every field in the table on a page if I didn't have to. I also did not want to write queries for each and every field.

I thought that maybe I'd be able to use a drop-down menu and an input field. The selected item on the drop down menu would equal the field in the database, and the input field would equal the new variable for the field in the table.

I tried several variations on an update query, specifically:
mysql_query("UPDATE table SET $dropdown='$input'");

It hasn't been successful, mainly for the fact that I can't get the code to recognize $dropdown as anything other than an actual field name itself, and not a variable. Of course the script stops as soon as it finds there's no field named $dropdown.

IF this makes sense to anyone, and if anyone can confirm whether or not this is even possible, please let me know.

Thanks,
A.Guy

vincevincevince

1:48 am on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



right, a smart quotes problem i think... (hunch)

try:

mysql_query("UPDATE table SET `".$dropdown."`='".$input."'");

anomalyguy

2:12 am on Feb 16, 2004 (gmt 0)

10+ Year Member



Tried what you suggested.

Right now, that particular section of code looks like:

if ($dropdown && $inputtext!= null) {
mysql_query("UPDATE table SET '".$dropwodwn."'='$inputtext'");
}

I didn't make the adjustments to the inputtext variable because that's the way all of my other variables have been entered up to this point and they have been working fine in queries without dynamic field names.

anomalyguy

2:16 am on Feb 16, 2004 (gmt 0)

10+ Year Member



Whooops!

I didn't notice that those were accent marks, not single quotes.

I tried it your way. It worked.

Thank you for the assist. I knew there had to be a way for it to work. :)