MySQL Forums
Forum List  »  PHP

Re: Problems with UPDATE
Posted by: Jonathan Stephens
Date: June 12, 2005 02:23AM

$query = "UPDATE tidbokning SET " . $_POST['uppdatera'] . " = '$info_to_update' WHERE id = '1'";


$query = "UPDATE tidbokning SET {$_POST['uppdatera']} = '$info_to_update' WHERE id = '1'";

Generally speaking, the backticks are optional, unless the database, table, or column name is a reserved word in MySQL. (IMO best practise is never to use reserved words for identifiers.)

If you want variable interpolation in PHP, you must use double quotes. If you want to interpolate an array element, you must surround the variable name with curly brackets. Example:

$something = "word";
print "a $something"; # (double quotes) prints: a word
print 'a $something'; # (single quotes) prints: a $something

$test = array();
$test['hi'] = 'Hello';

print "{$test['hi']}, world!"; # prints: Hello, word!
print "{$test["hi"]}, world!"; # prints: Hello, word!

print "$test['hi'], world!"; # *syntax error*
print "$test["hi"], world!"; # *syntax error*

Also, you should ALWAYS quote strings when used as associative array indexes (I missed this mistake before when looking at your code). See "Why is $foo[bar] Wrong?" in the PHP Manual:

Please spend some time with the PHP Manual sections on strings, arrays, and variable interpolation. Experiment until you're comfortable with how these work.

Jon Stephens
MySQL Documentation Team @ Oracle
Orlando, Florida, USA

MySQL Dev Zone
MySQL Server Documentation

Options: ReplyQuote

Written By
June 10, 2005 03:35AM
June 11, 2005 10:02AM
June 11, 2005 01:10PM
Re: Problems with UPDATE
June 12, 2005 02:23AM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.