Php 使用mysql创建过期日期
我正在用mysql和php创建一个用户注册系统,并在用户表中添加了一个名为“date_expires”(日期不为NULL)的列,以使注册用户的注册日期过期。使用“我的表单”,用户可以选择其注册期限。例:1年,2年,3年。等。我已经得到了用户提交表单时的注册期值。。像这样Php 使用mysql创建过期日期,php,mysql,Php,Mysql,我正在用mysql和php创建一个用户注册系统,并在用户表中添加了一个名为“date_expires”(日期不为NULL)的列,以使注册用户的注册日期过期。使用“我的表单”,用户可以选择其注册期限。例:1年,2年,3年。等。我已经得到了用户提交表单时的注册期值。。像这样 $registrationPeriod = $_POST['registration_period]; 我的问题是如何将具有上述值的过期日期插入到我的用户表中 我试图将数据插入到用户表中,但将如何操作与“date\u exp
$registrationPeriod = $_POST['registration_period];
我的问题是如何将具有上述值的过期日期插入到我的用户表中
我试图将数据插入到用户表中,但将如何操作与“date\u expires”列混淆了
这是我到目前为止的代码
$q = "INSERT INTO users (username, email, pass, first_name, last_name, date_expires)
VALUES ('$u', '$e', '$p, '$fn', '$ln', ????????????? )";
希望有人能帮我解决这个问题。。
多谢各位 你可以用两种方法 PHP 使用:
MySQL 使用:
如果您的
$\u帖子['registration\u period']
以1年、2年的形式出现。。。然后,您可以最轻松地去掉整数值并在MySQL中执行日期计算,如NOW()+INTERVAL n YEAR
其中n
是数字
// Extract it from the registration_period
// Since it is formatted as "n years" with a space between,
// we can split the string on the space. list() assigns an array (returned from explode())
// to individual variables. Since we only actually need one of them (the number),
// we can throw away the second (which is the string "years") by just giving list() one variable
// It still needs a placeholder for the second though, hence the extra comma.
list($years,) = explode(" ", $_POST['registration_period']);
// Make sure it is an int to protect against SQL injection...
$years = intval($years);
在查询中,将数字替换为VALUES()
列表中的日期计算:
INSERT INTO users (.......) VALUES (....., (NOW() + INTERVAL $years YEAR));
请考虑切换到支持准备好的语句的API,比如MySQL或PDO。我们只能希望并假设您的所有查询输入变量都已针对查询的当前表单中的SQL注入进行了正确的清理和过滤
$u = mysql_real_escape_string($_POST['u']);
$e = mysql_real_escape_string($_POST['e']);
// etc for all query vars...
(谢谢你的回答。请你解释一下这句话。列出($years,)=explode(“,$u POST['registration\u period');1年、2年、3年或仅1年、2年、3年等php@TharangaNuwan然后像上面一样使用list()
。substr($\u POST['registration\u period'],0,strpos($\u POST['registration\u period'],'')如果字符串是1年
等,则
还应用于提取年份。使用“年”或“年”是否有问题?
INSERT INTO users (.......) VALUES (....., (NOW() + INTERVAL $years YEAR));
$u = mysql_real_escape_string($_POST['u']);
$e = mysql_real_escape_string($_POST['e']);
// etc for all query vars...