0

My php script seems to not want to update my table although the exact query im trying to run works when i use it directly in the MySQL console. Also I have to say that selection querys have worked for me, its only updates that dont work..

heres my code:

ConnectToMySQL();

function ConnectToMySQL() {
    /* First Connects to the Server */
$link = mysql_connect("localhost", "root", "*******");
    if (!$link) {
        die("Could not connect: " . mysql_error());
    }
    /* Than chooses the DB */
$db_selected = mysql_select_db("irina", $link);
    if (!$db_selected) {
        die ("Can't use internet_database : " . mysql_error());
    }                
}

$Query = "UPDATE subtopics SET SubTopic_Name =  'spirit' WHERE SubTopic_ID='spirituality';";
mysql_query($Query);

again I want to point out to you that the query has proven to work in the MySQL console, and that other querys work for me.

6
  • 1
    PSA: The mysql_* functions are deprecated in PHP 5.5. It is not recommended for writing new code as it will prevent you from upgrading in the future. Instead, use either MySQLi or PDO and be a better PHP Developer. Commented Sep 22, 2013 at 2:02
  • what does mysql_error() output? Commented Sep 22, 2013 at 2:10
  • Try removing the trailing ; in the query. Queries through mysql_query should not end in a semicolon: "An SQL query: The query string should not end with a semicolon. Data inside the query should be properly escaped." (php.net/manual/en/function.mysql-query.php) But as the others said, don't use mysql_query() as it's been depreciated. Commented Sep 22, 2013 at 2:10
  • doesnt help removing the trailing ';'.. Commented Sep 22, 2013 at 2:15
  • do you beleive that if i use pdo i wont have this problem? Commented Sep 22, 2013 at 2:15

3 Answers 3

2

you have an unwanted character in query UPDATE remove ``

enter image description here

Good read

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

1 Comment

Thats funny, I dont see the character you're talking about.
1

I had the same issue but with code that had previously worked. Turned out to be an invalid character in the field name. A couple things that helped find it. When I echoed query and copied from the webpage view into Navicat for MySQL, it worked fine but when I "viewed source" then copied it, it got an error with the field name. If anyone is having similar issues, try that. The browser could be fixing something in the display view. Adding ` around field names also helped narrow it down.

Comments

0

remove the ; in the variable $Query

Comments

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.