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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UPDATE variable problem? 1

Status
Not open for further replies.

dle46163

IS-IT--Management
Jul 9, 2004
81
US
I'm having a problem with the UPDATE command as it pertains to a MySQL database.

My query code is this:

$query_fee = "UPDATE customer_contacts SET '{$_SESSION['type']}' = '{$_POST['amount']}'
WHERE Company_Name = '{$_SESSION['comp_name']}'";


The problem is after the SET command, I need the field I'm setting to be a variable earlier defined. If I echo the $_SESSION['type'] before I execute the query the data looks correct. However, when I run the query I get a bad query return. I can also replace the variable with the actual name of the field and it works. But I need it to be dynamic. Any thoughts?


 
Wrong button, sorry.

PHP does not properly interpret array variable expansion in double quotes. You'll either have to assign the array elements to scalars, or take them out of the quotes and concatenate the strings to get the sql string.

Code:
$string = "update tab set col = '" . $_SESSION[ 'var' ] . "'";
 
$query_fee = "UPDATE customer_contacts SET '{".$_SESSION['type']."}' = '{".$_POST['amount']."}'
WHERE Company_Name = '{".$_SESSION['comp_name']."}'";


one more thing:
why the use of {}???

Known is handfull, Unknown is worldfull
 
hi,
you should remove the "simple quote" from '{$_SESSION['type']}', like:


Code:
$query_fee = "UPDATE customer_contacts SET {$_SESSION['type']} = '{$_POST['amount']}'
  WHERE Company_Name = '{$_SESSION['comp_name']}'";

___
____
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top