首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP确认函数MySQL数据库错误

问PHP确认函数MySQL数据库错误
EN

Stack Overflow用户
提问于 2014-07-28 20:08:38
回答 1查看 374关注 0票数 1

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

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

更新数据库错误:没有选择数据库

根据我从不同站点/研究/堆栈溢出问题中收集到的信息,这是由于我所使用的数据库的权限(如果这是另一个问题,请纠正我)。我读到需要更改所有用户以使其能够READ,但不确定是否应该对整个数据库(我无法找到是否可以为数据库中的所有用户设置自动拥有READ特权的权限),或者在将它们添加到temp_table时设置它们的权限。我上面所展示的教程并没有提到任何关于它的内容,所以我很困惑。

登记表代码:

代码语言:javascript
复制
<?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();

            }
        }
    }
?>

确认页代码:

代码语言:javascript
复制
<?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);
        }
    }
?>
EN

回答 1

Stack Overflow用户

发布于 2014-08-01 17:20:29

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

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

请参阅mysql_query文档这里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25003460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档