I am trying to update some fields in a mysql database from php. This always gives me error, and I don't know why. Could you have a quick look?
$sqlupdate_ = "UPDATE $db SET city=$city, country=$country WHERE userIP=$ip;";
$sqlupdate = mysql_query($sqlupdate_);
echo $sqlupdate_;
if($sqlupdate){
//The query returned true - now do whatever you like here.
echo 'success';
}else{
//The query returned false - you might want to put some sort of error reporting here. Even logging the error to a text file is fine.
echo "error: ";
}
First of all:, use mysqli or PDO since mysql functions are depracated.
Second: avoid sql injections with escape your strings.
I think, your problem is that, your city, country and ip are varchars not integers, so you need to add quotes around them, and you want to update your table not your database.
Try to echoes your query and run directly in mysql.
$sqlupdate_ = "UPDATE " . mysqli_real_escape_string($link, $db). " SET city= '" .mysqli_real_escape_string($link, $city) ."', country='".mysqli_real_escape_string($link, $country)."' WHERE userIP='".mysqli_real_escape_string($link, $ip)."'";
to see the error, you could use try/catch like so:
<?php
function throw_ex($er){
throw new Exception($er);
}
try {
$sqlupdate_ = "UPDATE $db SET city=$city, country=$country WHERE userIP=$ip;";
$sqlupdate = mysql_query($sqlupdate_);
echo $sqlupdate_;
if($sqlupdate){
echo 'success';
} else {
throw_ex(mysql_error());
}
} catch (Exception $e) {
echo $e;
}
hope this helps