gpt4 book ai didi

PHP 电子邮件确认功能 MySQL 数据库错误

转载 作者:行者123 更新时间:2023-11-30 22:58:47 25 4
gpt4 key购买 nike

在 Stackoverflow 上有很多关于电子邮件确认、数据库和权限的问题,但我找不到任何可以帮助我解决这个问题的问题。

这个具体问题针对的是用 PHP 构建的电子邮件确认功能。我正在使用的教程可以在这里找到:http://www.phpeasystep.com/phptu/24.html .一切正常,但是当用户单击电子邮件确认链接(这会将他们的信息从 temp_table 移动到 confirmed_table)时,我收到此错误:

Error updating database: No database selected

根据我从不同站点/研究/Stackoverflow 收集到的问题,这是由于我正在使用的数据库的权限所致(如果这是另一个问题,请纠正我)。我读到我需要更改所有用户才能READ,但我不确定是否应该对整个数据库执行此操作(我找不到您是否可以设置数据库中的所有用户自动拥有 READ 特权)或当我将它们添加到 temp_table 时的 PHP。我上面显示的教程对此没有任何说明,所以我很困惑。

注册表单代码:

<?php
session_start();

if(isset($_SESSION['aI']) || isset($_SESSION['pss'])) {
header("Location: pa.php");
}

include 'db.php';

if(isset($_POST['rSub'])) {
// connects to database using PHP Data Objects, throws exception if error in connection
try {
$conn = new PDO("mysql:host=$svrHost;db=$svrDb", $sUme, $sp);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "ERROR: " . $e->getMessage();
}

$error = '';

if($_POST['fN'] == '' || $_POST['lN'] == '' || $_POST['aI'] == '' || $_POST['eml'] == '' || $_POST['pss'] == ''
|| $_POST['pss2'] == '') {
$error = "<li style=\"color:#C70000; font-weight:bold;\"><center>- All fields are required. Try again.</font><center></li>";
}
if($error == '') {
$fN = ucfirst($_POST['fN']);
$lN = ucfirst($_POST['lN']);
$aI = $_POST['aI'];
$eml = $_POST['eml'];
$pss = $_POST['pss'];
$pss2 = $_POST['pss2'];
$admin = 0;


if($error != '') {
$error = "<ul>".$error."</ul>";
$_SESSION['error'] = $error;
}
else {
$hF = "$2y$10$"; // 2y = blowfish and 10 = num of hashes
$sa = "testsaltforwebsite1219"; //"random" 22-character sa
$fAS = $hF.$sa;
$sha = crypt($pss, $fAS);
// Random confirmation code
$c_cd=md5(uniqid(rand()));

$insert = $conn->prepare("INSERT INTO t_awhole (c_cd, fN, lN, aI, eml, pss)
VALUES (:c_cd, :fN, :lN, :aI, :eml, :pss)");

$insert->bindParam(':c_cd', $c_cd);
$insert->bindParam(':fN', $fN);
$insert->bindParam(':lN', $lN);
$insert->bindParam(':aI', $aI);
$insert->bindParam(':eml', $eml);
$insert->bindParam(':pss', $sha);
$result=$insert->execute();

// ---------------- Confirmation email ---------------- \\
// table name
$t_apart=t_awhole;

if($result){

// send e-mail to ...
$to=$eml;

// Your subject
$subject="Registration Confirmation";

// From
$header="from: no-reply@example.com"; //Need the address to send the eml to.

// Your message
$message="Copy and paste this link in your browser to activate your account: \r\n";
$message.="\n";
$message.="(serverAddress)/confirmation.php?passkey=$c_cd \r\n";
$message.="\n";
$message.="Thank you";

// send eml
$sml = mail($to,$subject,$message,$header);
}

// if not found
else {
echo "Your email Is Not Registered. Please Register.";
}

// if your email succesfully sent
if($sml){
echo '<script> window.location.href="emlC.php"</script>';
}

else {
echo "Cannot Send Confirmation Link To Your email Address.";
}
// ---------------- Confirmation email ---------------- \\
$_SESSION['aI'] = $aI;
$_SESSION['pss'] = $pss;
$_SESSION['admin'] = 0;

$stmt = $conn->prepare("SELECT DISTINCT dN, dU, ex FROM doc WHERE aI != '0'");
$stmt->execute();

$result = $stmt->fetchAll();
foreach ($result as $row)
{

$ex = $row['ex'];
$dU = $row['dU'];
$dN = $row['dN'];


$insert = $conn->prepare("INSERT INTO doc (dN, dU, aI, ex)
VALUES (:dN, :dU, :aI, :ex)");


$insert->bindParam(':aI', $aI);
$insert->bindParam(':ex', $ex);
$insert->bindParam(':dU', $dU);
$insert->bindParam(':dN', $dN);

$insert->execute();

}
}
}
?>

确认页面代码:

<?php

include('db.php');

// passkey that got from link
$pk=$_GET['pk'];
$t_awhole_conf="t_awhole";

// Retrieve data from table where row that match this passkey
$sql_conf1="SELECT * FROM $t_awhole_conf WHERE confirm_code ='$pk'";
$result_conf=mysql_query($sql_conf1) or die ('Error updating database: '.mysql_error());


// If successfully queried
if($result_conf){
// Count how many row has this passkey
$count=mysql_num_rows($result_conf);

// if found this passkey in our database, retrieve data from table "t_awhole"
if($count==1){

$rows=mysql_fetch_array($result_conf);
$fN = $rows['fN']; // capitalizes the first letter (6-26-14)
$lN = $rows['lN']; // capitalizes the first letter (6-26-14)
$aI = $rows['aI'];
$eml = $rows['eml'];
$pss = $rows['pss'];
$pss2 = $rows['pss2'];

$a_whole_conf="a_whole";

// Insert data that retrieves from "t_awhole" into table "a_whole"
$sql_conf2="INSERT INTO $a_whole_conf(fN, lN, aI, eml, pss, admin)
VALUES ($fN, $lN, $aI, $eml, $pss, $admin)";
$result_conf2=mysql_query($sql_conf2);
}

// if not found passkey, display message "Wrong Confirmation code"
else {
echo "Wrong Confirmation code";
}

// if successfully moved data from table"t_awhole" to table "a_whole" displays message "Your account has been activated" and don't forget to delete confirmation code from table "t_awhole"
if($result_conf2){

echo "Your account has been activated";

// Delete information of this user from table "t_awholeb" that has this passkey
$sql_conf3="DELETE FROM $t_awhole_conf WHERE confirm_code = '$pk'";
$result_conf3=mysql_query($sql_conf3);
}
}
?>

最佳答案

在您的注册表单代码中,您有两行创建与数据库的连接(新 PDO ...)。您可以进一步使用 $conn 来执行语句。

在您的确认代码中,您在调用 mysql_query 之前没有创建任何连接(为什么要从 PDO 切换到 mysql 函数?)。

请参阅 mysql_query 文档 here .

关于PHP 电子邮件确认功能 MySQL 数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25003460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com