質問

I'm trying to insert a md5 hash into a mysql database table with php.

        $key = $email . date('mY');
        $key = md5($key);

        $query = "INSERT INTO confirm (key, angemeldet_von, geschlecht, geburtstag)
            VALUES('$key', '$angemeldet_von', '$geschlecht', '$geburtstag')";
        $confirm = mysql_query($query, $connectionID)
          or die('failed connecting: ' . mysql_error());

This query returns the error:

failed connecting: 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, angemeldet_von, geschlecht, geburtstag) VALUES('8fe3ec75be1a43b49eacd3' at line 1

But the same query works well when I leave out the key column and its value. So what is the problem here? Mysql:

key varchar(128)    utf8_general_ci
役に立ちましたか?

解決

key is a reserved word in MySQL. Enclose it in backticks !

Like this..

$query = "INSERT INTO confirm (`key`, angemelde
                               ^   ^ ------ Like that

This(mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. Switching to PreparedStatements is even more better to ward off SQL Injection attacks ! ^ ^

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top