Jump to content

PHP with Sql UPDATE command not working for numbers > 2


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.