Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP电子邮件确认函数MySQL数据库错误_Php_Mysql_Database_Error Handling - Fatal编程技术网

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

PHP电子邮件确认函数MySQL数据库错误,php,mysql,database,error-handling,Php,Mysql,Database,Error Handling,有很多关于电子邮件确认、数据库和Stackoverflow权限的问题,但我找不到任何有助于我解决这些问题的方法 这个特定的问题直接指向用PHP构建的电子邮件确认函数。我正在使用的教程可以在这里找到:。一切正常,但当用户单击电子邮件确认链接,将其信息从临时表移动到确认表时,我收到以下错误: 更新数据库时出错:未选择任何数据库 从我从不同网站/研究/Stackoverflow收集的信息来看,问题是这是由于我正在使用的数据库的权限造成的。如果是其他问题,请纠正我。我已经读到,我需要更改所有用户以使其能

有很多关于电子邮件确认、数据库和Stackoverflow权限的问题,但我找不到任何有助于我解决这些问题的方法

这个特定的问题直接指向用PHP构建的电子邮件确认函数。我正在使用的教程可以在这里找到:。一切正常,但当用户单击电子邮件确认链接,将其信息从临时表移动到确认表时,我收到以下错误:

更新数据库时出错:未选择任何数据库

从我从不同网站/研究/Stackoverflow收集的信息来看,问题是这是由于我正在使用的数据库的权限造成的。如果是其他问题,请纠正我。我已经读到,我需要更改所有用户以使其能够阅读,但我不确定是否应该对两个数据库作为一个整体执行此操作。我找不到是否可以为数据库中的所有用户设置权限,以便在我将其添加到temp_表中时自动拥有阅读权限,或者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);
        }
    }
?>

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

在您的确认代码中,在调用mysql之前,您没有创建任何连接。为什么从PDO切换到mysql功能


请参阅mysql\u查询文档。

您如何初始化数据库连接?请出示codeSidenote:该教程很旧,过时了,并且对SQL注入非常开放。我建议您使用mysqli_uuu和准备好的语句查找其他内容,或者将其转换为mysqli_uu,同时确保在使用正确/安全的哈希算法时,不要将密码存储为纯文本。如果你按原样使用教程,那么被黑客攻击只是时间问题;“你已经被告知了。@Fred ii-对。在基本函数运行后,我开始处理安全问题。@Alex我用代码更新了问题。我不认为这是必要的,因为我问的是一个更一般的问题。这是必要的,因为这是您选择使用哪个数据库的点。如果要从一个数据库移动到另一个数据库,则需要在使用该数据库之前选择该数据库。或者在插入查询中引用其全名:database.tableOK。你建议做什么?我这样做是因为我遵循了我在问题中发布的指南。我的第一反应是完全更改一个文件或另一个文件,选择使用PDO注册文件还是mysql确认文件。我会选择PDO,因为PHP5.5.0中的另一个已弃用。相反,如果您只想让它快速工作,我认为您只会错过确认文件中的一条连接线就成功了。类似于mysql\u connecthost、user、pass,然后mysql\u select\u dbdb。请参见此处的mysql函数:。