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.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
  • 1
    *PSA:* The `mysql_*` functions are [deprecated in PHP 5.5](http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated). It is not recommended for writing new code as it will prevent you from upgrading in the future. Instead, use either [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php) and [be a better PHP Developer](http://jason.pureconcepts.net/2012/08/better-php-developer/). – Jason McCreary Sep 22 '13 at 02:02
  • what does `mysql_error()` output? – Patrick Evans Sep 22 '13 at 02: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." (http://php.net/manual/en/function.mysql-query.php) But as the others said, don't use `mysql_query()` as it's been depreciated. – Taj Morton Sep 22 '13 at 02:10
  • doesnt help removing the trailing ';'.. – Avriel Moscovitz Sep 22 '13 at 02:15
  • do you beleive that if i use pdo i wont have this problem? – Avriel Moscovitz Sep 22 '13 at 02:15
  • have you checked what `mysql_error` function returns? as that will give you an error if there is an error executing the query. – Patrick Evans Sep 22 '13 at 02:17

3 Answers3

2

you have an unwanted character in query UPDATE remove ``

enter image description here

Good read

Community
  • 1
  • 1
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.

user2246924
  • 105
  • 3
0

remove the ; in the variable $Query

Angel
  • 146
  • 2
  • 11