0

I'm trying to update a MySQL database with PHP.

Here is my code:

$tableName = "Licenses";
$searchVariable = "used";
$selectVariable = "verCode";
$verTime = date('Y-m-d H:i:s');
$userUUID = "test_string";
$verID = "79A4D";

mysql_connect("localhost", "my_user", "my_pass") or die(mysql_error());
mysql_select_db("licenses_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM `{$tableName}` WHERE `{$searchVariable}`='{$verID}'") or die(mysql_error());

while($info = mysql_fetch_array($data)) {
    //Verification ID unused, so verify the user
    foreach($info as $key => $value) {
        echo "$key: $value</br>";
        }
    if ($info['used'] == 0) {
        echo "<br/>UPDATE `{$tableName}` SET '{$selectVariable}'=1,'time'=`{$verTime}`,'UUID'=`{$userUUID}`; WHERE `{$searchVariable}`='{$verID}'<br/>";
            // the above is to see what command is used
        mysql_query("UPDATE `{$tableName}` SET '{$selectVariable}'=1,'time'=`{$verTime}`,'UUID'=`{$userUUID}`; WHERE `{$searchVariable}`='{$verID}'");
        echo "data updated";
        return 'Success';
        }
    //Verification ID was used already
    else {
        echo "found but used";
        return 'Error Message';
        }
    }
echo "not found";
return 'Error Message';

However, the database doesn't update. I have the table Licenses created in license_db. In addition, I have one row with the following values:

verCode = 79A4D
used = 0
UUID = NULL
time = NULL

If I run the program the first time, it should update the database. This is printed out:

0: 79A4D
verCode: 79A4D
1: 
used: 
2: 
UUID: 
3: 
time: 

UPDATE `Licenses` SET 'used'=1,'time'=`2014-02-15 19:14:13`,'UUID'=`test_string`; WHERE `verCode`='79A4D'
data updated

When I run it the second time, used is now 1, and so it should print out found but used. However, the data updated section (with the UPDATE ...) is printed out.

So, the database is not updating. How can I solve this? Thanks!

9
  • 1
    The mysql functions are deprecated and therefore not a great place to start learning how to access a MySQL database from PHP. Commented Feb 16, 2014 at 0:26
  • You noticed you entered a ; delimiter ? try removing it any see what is gonna happen.. Commented Feb 16, 2014 at 0:27
  • @OrelEraki Same result. Commented Feb 16, 2014 at 0:29
  • @F3AR3DLEGEND, Can you put or die(mysql_error()); after every mysql_query you got, and run so we could see the errors ? Commented Feb 16, 2014 at 0:34
  • 1
    You should use backtick for table name and columns and quotes for values, you inverted them in last query Commented Feb 16, 2014 at 0:38

1 Answer 1

1

e.g.

"
UPDATE `$tableName`  
   SET `$selectVariable` = 1
     , `time` = '$verTime'
     , `UUID` = '$userUUID'
 WHERE `$searchVariable` = '$verID';
";
Sign up to request clarification or add additional context in comments.

1 Comment

I had another issue. $info['user'] == 0 is always True, even once the user = 1 (I checked the database using phpAdmin). If I try == '0', it's always False...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.