我的网站上有一个注册表,供用户注册成为会员。
当他们注册时,他们的详细信息被发送到mysql数据库,他们的电子邮件,姓名等都被输入。 他们还获得了一个唯一的注册码,该注册码由随机MD5在提交时生成,该代码被输入数据库并通过电子邮件发送给他们。
我遇到的问题是每次新用户注册时都会生成一个新代码,但它会为数据库中的所有其他用户重写以前的代码。我想要的是让每个用户的代码保持唯一,并且每次新用户注册时都不会重写其他用户的代码,所以我认为我需要编辑我的代码来说
UPDATE `ptb_registrations` SET `registration_code` = '%s'
WHERE email = (whatever unique email the user enters)
有人有什么想法吗?谢谢
<?php
/**
* ShuttleCMS - A basic CMS coded in PHP.
* random code - Used for allowing a user to reset password
*
* @author Dan <dan@danbriant.com>
* @version 0.0.1
* @package ShuttleCMS
*/
define('IN_SCRIPT', true);
// Start a session
session_start();
/*
* Generates registration code and puts it on database
*/
//Generate a RANDOM MD5 Hash for a code
$random_code=md5(uniqid(rand()));
//Take the first 8 digits and use them as the code we intend to email the user
$emailcode=substr($random_code, 0, 8);
//Encrypt $emailcode in MD5 format for the database
$registrationcode=($emailcode);
// Make a safe query
$query = sprintf("UPDATE `ptb_registrations` SET `registration_code` = '%s' ",
mysql_real_escape_string($registrationcode));
mysql_query($query)or die('Could not update members: ' . mysql_error());
?>
在您的 MySQL 查询中,您不会放置一个 where 子句来将其限制为仅该新用户。您正在更新"所有"用户,因为您没有限制它。
您的代码看起来不完整,因为您没有包含有关注册的部分,但我认为处理正常。
然后,您希望将查询更新为类似
更新ptb_registrations
设置registration_code
= '%s' 其中 user_id = '%s'
显然,替换为架构的正确值。
这是我的问题:为什么不在注册用户时生成该代码?这样,您只需执行INSERT INTO
,而无需执行另一个UPDATE
查询。
过程可能是这样的
- 获取用户凭据
- 生成唯一键
- 将所有内容存储到数据库中
- 发送包含该唯一密钥的电子邮件以批准电子邮件地址
- 全部完成
嗯?
通过运行
UPDATE `ptb_registrations` SET `registration_code`
您正在更新所有用户,但缺少WHERE
子句:
UPDATE `ptb_registrations` SET `registration_code` = "safkhsakjfdha"
WHERE `email`='bla@bla.bla'
除此之外,假设所有变量都有值,其余的看起来都不错。