问题:插入数据库后无法获取email值。
编辑:
固定:
我的问题是 $to = $_POST['$email'];
给出了错误的值。它应该是 $to = $_POST['email'];
。
问题是因为我有 $ 来提供值(value)。
这是我创建激活码的方式:
$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());
}
插入数据库有效。
在此先感谢您的帮助。
最佳答案
试试这个
$message .= $WEBSITE_URL . "/activate.php?email=" . urlencode($email) . "&key=".$activation;
无论如何使用这个sha1(uniqid(mt_rand(), true));
关于php - 为什么我用PHP插入数据库后不能使用数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16476723/