0

One of my PHP functions just not work. Mysql returns success but it doesnt UPDATE column that i want. I really no have idea why that problem exist.

So here is the function that UPDATING it:

function savePhone($phone) {

dbConnect();
$q = "UPDATE site SET phone = '$phone' WHERE id = 0";
$r = mysql_query($q) or die (mysql_error());
if ($r) return $q;
else return "error"; 

}

Here is var_dump-ed example query that works fine when using phpMyAdmin.

'UPDATE site SET phone = '111111' WHERE id = 0'

Im using Apache

  • 5
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You may also be **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Dec 24 '13 at 14:57
  • Your `else return error` is pointless. If the query fails, the `or die()` will kill the script right there. – Marc B Dec 24 '13 at 14:57
  • 2
    do you really have a record with id = 0? – arilia Dec 24 '13 at 15:00
  • What error do you get? Are you connected to the database? – putvande Dec 24 '13 at 15:02
  • 1
    Add a `echo mysql_error();` after the mysql_query call and you'll see what's going on. Possibly did not select database? – pp19dd Dec 24 '13 at 15:02
  • I AM CONNECTED TO DATABASE and i get NO ERRORS. Function saying that record has been updated, when checking that column in db its has not changed. But when I paste that var_dump-ed statment in phpmyadmin its actually updating record - working well // using root (master) user in phpmyadmin and as php user – user3132766 Dec 24 '13 at 15:05
  • DATABASE is also selected in dbConnect function. Sorry but i can't use any pdo (too much changes) – user3132766 Dec 24 '13 at 15:16
  • what is the result of `var_dump($r)` ? – Alireza Fallah Dec 24 '13 at 15:19
  • No error. Mysql says nothing bad happened. There are two possible explanations. 1) You are viewing the wrong database. 2) You do not have record with id = 0. – N.B. Dec 24 '13 at 15:30
  • There is another solution i just find out. I have forgotten to put unique key for id comumn in db. Thats all. – user3132766 Dec 27 '13 at 15:14

1 Answers1

0

This is wrong actually die() stops the script from executing

$r = mysql_query($q) or die (mysql_error());

if die() is called this part is never gonna be executed and no value is returned to $q

if ($r) return $q; 
  else return "error"; 
}

use instead

$r = mysql_query($q);

if(empty(mysql_error())) return $q;
   else return "error";
Hmmm
  • 1,774
  • 2
  • 13
  • 23