0

I have a problem updating record on a table. Below is the detail:

I have a table named tbl_option, it has 2 field : option_name, option_value. The current table record is follow:

option_name | option_value
site_name   | MySite
site_desc   | About anything

I want to update both site_name and site_desc option_value, here is my php script to update the site_name and site_desc option_value:

require "include/config.php";
$name = "MyNewSitess";
$desc = "About Computer";

$query = mysql_query("UPDATE tbl_option SET option_value='$name' WHEREoption_name='site_name';# UPDATE tbl_option SET option_value='$desc' WHERE option_name='site_desc'");

if ($query) { echo "Saved"; }
else echo "Not saved : ".mysql_errno()." | ".mysql_error();

after executed I get the following error:

Not saved : 1064 | You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE tbl_option SET option_value='About Computer' WHERE option_name='site_desc' at line 1

The record not updatd. But when I add (#) before the second UPDATE query :

<?php
...
$query = mysql_query("UPDATE tbl_option SET option_value='$name' WHERE option_name='site_name'; #UPDATE tbl_option SET option_value='$desc' WHERE option_name='site_desc'");
...
?>

It not showing any error. I get the following :

Saved

and the record is updated

What is the problem ?

Yes, I imitate the wordpress table concept but I don't know how to do the UPDATE query to the table.

2
  • 2
    First off, this doesn't belong here. This belongs on StackOverflow, on which it's already been answered. Read the documentation on mysql_query() on php.net (that should be your first stop whenever you run into problems or first start learning about a function). It clearly states that you can only make a single query at a time. If you look at how the mysql_* functions work, you'll see why that is. Lastly, the mysql extension is deprecated. Use mysqli or pdo instead. Again, all of this info is obvious if you read the manual. Commented Dec 31, 2012 at 7:37
  • mysql_query() doesn't support multiple queries. It's right in the description for mysql_query. Commented Dec 31, 2012 at 14:54

1 Answer 1

2

Change from

$query = mysql_query("UPDATE tbl_option SET option_value='$name' WHEREoption_name='site_name';# UPDATE tbl_option SET option_value='$desc' WHERE option_name='site_desc'");

to

$query = mysql_query("UPDATE tbl_option SET option_value='$name' WHERE option_name='site_name'"); 

if($query) $query = mysql_query("UPDATE tbl_option SET option_value='$desc' WHERE option_name='site_desc'");

However, if this is a WordPress update, then you are better off posting this question here. You might just get a better solution :-)

Another option if you are doing this only once is to directly the console or a popular tool by the name of of PhpMyAdmin to do the updates.

Sign up to request clarification or add additional context in comments.

1 Comment

I assume the WHEREoption_name= (lack of space after the WHERE) is a typo in the question, as it's not in the second code sample.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.