无法将验证码数据插入mysql数据库

时间:2015-11-30 18:14:30

标签: php mysql database

我正在尝试将数据插入到mysql数据库中,这是一个带验证的电子邮件注册。唯一的随机数未插入数据库表。请帮我。非常感谢。

else {
    $user_confirm = md5(uniqid(rand()));
    echo "$user_confirm";
    //the form has been posted without, so save it
    //notice the use of mysql_real_escape_string, keep everything safe!
    //also notice the sha1 function which hashes the password
    $sql = "INSERT INTO users(
                   user_name, 
                   user_pass, 
                   user_email,
                   user_date,
                   user_level,
                   user_confirm
            )
            VALUES('" . mysql_real_escape_string($_POST['user_name']) . "',
                   '" . sha1($_POST['user_pass']) . "',
                   '" . mysql_real_escape_string($_POST['user_email']) . "',
                   NOW(),
                   0, 
                  '$user_confirm'
            )";

    $email = $_POST["user_email"];  
    $result = mysql_query($sql);

2 个答案:

答案 0 :(得分:1)

您有两个问题:$user_confirm是一个字符串(我使用您的代码生成beba256f79e888013bb34e5a774fe7f9)并且您正在尝试将其存储在指定为整数的列中({{1 }})。

哈希字符串存储在INT(11)字段中,以允许TEXTmd5()生成的字符串大小。

其次,你真的不应该使用MD5密码哈希,你真的应该使用PHP的built-in functions来处理密码安全性。如果您使用的PHP版本低于5.5,则可以使用sha1() compatibility pack

Your script is at risk for SQL Injection Attacks.

如果可以,你应该stop using mysql_* functions。已在PHP 7中删除These extensions。了解preparedPDOMySQLi语句,并考虑使用PDO,it's really pretty easy

答案 1 :(得分:0)

MySQL md5函数返回32个十六进制数字的字符串,如果参数为NULL,则返回NULL。对字段使用varchar(32)而不是int(11)。

Refer to MySQL Document for more info.

另外,请确保正确地在字符串字段中添加引号。

相关问题