【发布时间】:2013-05-04 19:28:15
【问题描述】:
问题:插入数据库后无法获取email值。
已编辑:
固定:
我的问题是$to = $_POST['$email']; 赋予了错误的价值。它应该是$to = $_POST['email'];。
问题是因为我有 $ 价值。
这就是我创建激活码的方式:
$activation = sha1(uniqid(rand(), true));
这就是我声明我的数据库插入查询的方式:
$query = "INSERT INTO users (activation, email) VALUES (:activation, :email)";
在这里我为我的插入查询填充参数:
$query_params = array(
':activation' => $activation,
':email' => $_POST['email']
);
...下面是我通过执行查询和发送电子邮件将数据添加到数据库的方法:
try
{
// Execute the query to create the user
$stmt = $db->prepare($query);
$stmt->execute($query_params);
if ($stmt->rowCount() > 0) { //If the Insert Query_params was successfull.
echo 'even gets to here.';
// Send the email:
$to = $_POST['email'];
$subject = 'Registeerimis kinnitus';
$from = 'From: mymail@gmail.com';
$WEBSITE_URL = "http://mysite.com";
$message = " Kasutaja aktiveerimiseks, palun vajutage aadressile:\n\n";
$message .= $WEBSITE_URL . "/activate.php?email=" . urlencode($to) . "&key=".$activation;
mail($to, $subject, $message);
}
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query: " . $ex->getMessage());
}
插入数据库有效。
提前感谢您的帮助。
【问题讨论】:
-
或者我是否需要在插入后从数据库中读取数据以再次使用?
-
无论如何都要使用这个 sha1(uniqid(mt_rand(), true));
-
okey tnx 的建议
-
与
try..catch块相关的$activation和$email声明在哪里? -
我不能在声明之前给出值吗?那 $activation = sha1(uniqid(rand(), true));在插入数据库之前声明,因为对于活动帐户,您需要单击该链接。这些代码段在同一个文件中。