0

I am trying save to database row. But I get always error.

My code is:

function save_activation_key($connection, $username, $key) {
    $date = time();
    $is_used = 0;
    $query = "INSERT INTO account_activation_key (  key, date,  is_used)
    VALUES (    '" . $username . "',"
                . " '" . $date . "',"
                . " '" . $is_used . "')";
    $retval = mysql_query($query, $connection);
    echo $retval;
    $retval = mysql_query( $query, $connection );
    if(! $retval )
    {
      die('Could not enter data: ' . mysql_error());
    }    
}

In $connection is valid connection to database.

Database structure:

id : int
key: varcha(45)
date: date
is_used: tinyint(1)

When I call my code I get error:

Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key, date, is_used) VALUES (   'uzivatelsky_jmeno', '1459971829', '0')' at line 1

Where is a problem?

Thanks for help

chelocre
  • 73
  • 1
  • 2
  • 9
  • Can you `echo` your query statement and check if it is valid ? – Maximus2012 Apr 06 '16 at 19:50
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Apr 06 '16 at 19:55

2 Answers2

2

key is a MYSQL reserved word and should not really be used as column names.

MYSQL Reserved Words List can be found here https://dev.mysql.com/doc/refman/5.7/en/keywords.html

However if you wrap these column names in backticks you can get away with it.

You can also simplify your query string concatenation like the following, which makes it a lot easier to debug.

function save_activation_key($connection, $username, $key) {
    $date = time();
    $is_used = 0;
    $query = "INSERT INTO `account_activation_key` 
                     (  `key`, `date`,  `is_used`)
              VALUES ( '$username', '$date', '$is_used')";

    $retval = mysql_query($query, $connection);
    echo $retval;
    $retval = mysql_query( $query, $connection );
    if(! $retval )
    {
      die('Could not enter data: ' . mysql_error());
    }    
}

BIG NOTE

Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Which means this code will never run when all that is available is PHP7 or greater. Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
-2

probably your query contains an error at the place where you are giving integer as a string , like your string '" . $username . "'," . " '" . $date . "'," . " '" . $is_used . "'

should be : '" . $username . "',"." . $date . ","." . $is_used . "

the integers should'nt be with single qoutes " ' "

probably this is the mistake!

usmani93
  • 1
  • 3