Jump to content

PHP with Sql UPDATE command not working for numbers > 2


gazapo

Recommended Posts

This is the weirdest thing ever. I've been programming php for years yet I have been pulling my hair out for hours over something that seems so simple!

 

I have a table in my db named last_num_used with 2 fields: id, num which are both type int.

It only has one row, so I don't need to use WHERE to update

 

when I execute the command via php:

	$sql = "UPDATE last_num SET num='4'";
echo $sql;
if (!mysql_query($sql)) {die('Error: ' . mysql_error());}

everything works fine and I see the echoed command UPDATE last_num SET num='4'

 

But, when I have a variable $var and try to update it, it will only update properly if $var=1 or $var=2. If $var equals a value greater than 2, then for some reason it updates the column so that num=1. Note the exact problem in example 3 below.

 

Example 1:

	$var=1;
$sql = "UPDATE last_num SET num='$var'";
echo $sql;
if (!mysql_query($sql)) {die('Error: ' . mysql_error());}

Result: echoed to screen: UPDATE last_num SET num='1'

database updated and num=1

 

 

Example 2:

$var=2;
$sql = "UPDATE last_num SET num='$var'";
echo $sql;
if (!mysql_query($sql)) {die('Error: ' . mysql_error());}

Result: echoed to screen: UPDATE last_num SET num='2'

database updated and num=2

 

 

Example 3:

	$var=3;
$sql = "UPDATE last_num SET num='$var'";
echo $sql;
if (!mysql_query($sql)) {die('Error: ' . mysql_error());}

Result: echoed to screen: UPDATE last_num SET num='3'

database updated and num=1

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.