将md5哈希插入mysql数据库

时间:2014-03-15 12:15:18

标签: php mysql md5

我试图用php将md5哈希插入到mysql数据库表中。

        $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());

此查询返回错误:

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

但是当我省略键列及其值时,同样的查询效果很好。 那么这里的问题是什么? MySQL的:

key varchar(128)    utf8_general_ci

1 个答案:

答案 0 :(得分:4)

key是MySQL中的reserved word。用反引号括起来!

喜欢这个..

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

mysql_*)扩展程序自PHP 5.5.0起已弃用,将来会被删除。相反,应使用MySQLiPDO_MySQL扩展名。切换到PreparedStatements甚至可以更好地抵御SQL注入攻击!                                    ^ ^