user3074164
user3074164

Reputation: 1

ql-No Mysql error but database table not updating

I am running a script with no mysql errors but nothing is being written in the database table.

$team1 = 75;
$con = mysqli_connect("localhost","user","pwd");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
mysqli_select_db($con,"database") or die ("no database");

$sql=" update TABLENAME set 
GAME1 = case when GAME1 = '' then $team1 else GAME1 end,
GAME2 = case when GAME2 = '' then $team1 else GAME2 end,
GAME3 = case when GAME3 = ''  then $team1 else GAME3 end
WHERE ID = 140 ";
if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);

When I ran the php code, it echoes "1 record added", but there’s no record added in the table.

What is wrong?

Thanks.

EDIT

When I ran echo $sql; I get this:

update TABLENAME set GAME1 = case when GAME1 = '' then 75 else GAME1 end, GAME2 = case when GAME2 = '' then 75 else GAME2 end,GAME3 = case when GAME3 = '' then 75 else GAME3 end WHERE ID = 140 

Upvotes: 0

Views: 839

Answers (1)

Borniet
Borniet

Reputation: 3546

mysqli_query will return false in case of an error. Your code catches if an error occurs when executing the query. Very good :-) However, the query can execute without any errors, but without providing the result you expected. This can be for instance because one of the where/when statements isn't met. To debug this, echo your query, and execute it straight on the db:

echo $sql;

Upvotes: 2

Related Questions