I have some PHP code:
mysql_query("UPDATE people " .
"SET first_name = '$fname' , last_name = $lname , email = $email , " .
"age = $age , cityids = $city , gender = $gender WHERE id = $Recedit"
or die (mysql_error));
But the query does not run. What am I doing wrong?
Your MySQL query is incorrectly formatted. The or die
should be after the query.
mysql_query("UPDATE people SET first_name = '$fname' , last_name = $lname , email = $email , age = $age , cityids = $city , gender = $gender WHERE id = $Recedit") or die (mysql_error);
But also, for readability, I recommend you do something like this:
$query = "UPDATE people SET"
. " first_name = '" . $fname . "',"
. " last_name = " . $lname . ","
. " email = " . $email . ","
. " age = " . $age . ","
. " cityids = " . $city . ","
. " gender = " . $gender
. " WHERE id = " . $Recedit;
mysql_query($query) or die (mysql_error);
Formatting queries so they are human readable always helps you in the log run when debugging logic & formatting errors.
You have to get the "... or die" thing out of the brackets. Right now you're making a logical "or" between the string itself and the "die" command. Also, mysql_error is a function, so:
// WRONG
mysql_query("..............." or die(mysql_error))
// RIGHT
mysql_query("...............") or die(mysql_error())
Maybe there's something else too but this is surely a mistake.
Once you've sorted out everything else that's wrong/out-of-date, your query might look something like this...
UPDATE people
SET first_name = '$fname'
, last_name = '$lname'
, email = '$email'
, age = $age
, cityids = $city
, gender = '$gender'
WHERE id = $Recedit
I'll suggest you to
1. Write the simplest code. like this:
$concat_query = "upate ...
$result = mysql_query($concat_query);
if ($result) {
if ((mysql_num_rows($result) ) > 0) {
...
2. Use Notepad++ to see pieces of code in different colors.
3. First execute the dbcode on database and later on write the code.