数据库中只有一个用户

时间:2013-05-28 14:08:31

标签: php mysqli

我希望如果电子邮件存在于数据库中,则不应该再次创建数据库。它应该只能再次维持下去!

if($stmt = $this->mysqli->prepare("INSERT INTO `bruger` (`rank`, `email`, `adgangskode`, `navn`, `efternavn`, `profil_img`) VALUES (?, ?, ?, ?, ?, ?)"))
        {
            $stmt->bind_param('isssss', $rank, $email, $adgangskode, $navn, $efternavn, $profil_img);

            $rank = 1;
            $email = $_POST["email"];
            $adgangskode = sha1($_POST["password_adgangskode_1"]);
            $navn = $_POST["fornavn"];
            $efternavn = $_POST["efternavn"];
            $profil_img = $pb;

            $stmt->execute();

            $navn = $_POST["navn"];
            $til = $_POST["email"];
            $password = $_POST["password_adgangskode_1"];


            $fra = "blabla.dk - Opret bruger <" . "info@blabla.dk" . ">";
            $til = $_POST["email"];
            $emne = "Velkommen til blabla.dk";

            $besked = file_get_contents('mail_opret.php');

            $besked = str_replace("%%navn", $navn, $besked);
            $besked = str_replace("%%brugernavn", $til, $besked);
            $besked = str_replace("%%password", $password, $besked);

            $headers = "MIME-Version: 1.0\n";
            $headers .= "Content-type: text/html; charset=UTF-8 \n";
            $headers .= "From: " . $fra . "\n";

            mail($til, $emne, $besked, $headers);

            ?>
            <div class="article-main-content">
            <div class="alert-message" style="background-color:#77b01e;"><span class="icon-text">&#10003;</span><span class="alert-content">Din bruger er blevet opret og du få en email fra os <a href="#" class="destroy-button"></a></div>
            </div>
            <?php

            $stmt->close();


        }
        else
        {
            echo 'Der opstod en fejl i erklæringen: ' . $this->mysqli->error;
        }

它应该使用/访问数据库中的用户,因此无法再次创建电子邮件。那么它应该只能做一次!

我应该怎么做?

EIDT HERE!

foreach($this->mysqli->query("SELECT * FROM bruger WHERE email='$email'") as $row) {
        if ($row['email'] !== $email) {
            $pb = null;

            include "fun_filer/img/class.upload.php";
            $handle = new Upload($_FILES["file"]);

            if($handle->uploaded)
            {
                $handle->image_resize = true;
                $handle->image_ratio_crop = true;
                $handle->image_y = 75;
                $handle->image_x = 75;
                $handle->Process("images/bruger");
                $pb = $handle->file_dst_name;
            }

            if($stmt = $this->mysqli->prepare("INSERT INTO `bruger` (`rank`, `email`, `adgangskode`, `navn`, `efternavn`, `profil_img`) VALUES (?, ?, ?, ?, ?, ?)"))
            {
                $stmt->bind_param('isssss', $rank, $email, $adgangskode, $navn, $efternavn, $profil_img);

                $rank = 1;
                $email = $_POST["email"];
                $adgangskode = sha1($_POST["password_adgangskode_1"]);
                $navn = $_POST["fornavn"];
                $efternavn = $_POST["efternavn"];
                $profil_img = $pb;

                $stmt->execute();

                $navn = $_POST["navn"];
                $til = $_POST["email"];
                $password = $_POST["password_adgangskode_1"];


                $fra = "blabla.dk - Opret bruger <" . "info@blabla.dk" . ">";
                $til = $_POST["email"];
                $emne = "Velkommen til blabla.dk";

                $besked = file_get_contents('mail_opret.php');

                $besked = str_replace("%%navn", $navn, $besked);
                $besked = str_replace("%%brugernavn", $til, $besked);
                $besked = str_replace("%%password", $password, $besked);

                $headers = "MIME-Version: 1.0\n";
                $headers .= "Content-type: text/html; charset=UTF-8 \n";
                $headers .= "From: " . $fra . "\n";

                mail($til, $emne, $besked, $headers);

                ?>
                <div class="article-main-content">
                <div class="alert-message" style="background-color:#77b01e;"><span class="icon-text">&#10003;</span><span class="alert-content">Din bruger er blevet opret og du få en email fra os <a href="#" class="destroy-button"></a></div>
                </div>
                <?php

                $stmt->close();


            }
            else
            {
                echo 'Der opstod en fejl i erklæringen: ' . $this->mysqli->error;
            }
        }
        else
        {
        ?>
        <div class="article-main-content">
    <div class="alert-message" style="background-color:#c22525;"><span class="icon-text">&#9888;</span><span class="alert-content">Error!</span><a href="#" class="destroy-button"></a></div>
    </div>
        <?php
        }
        }

在这里:

if ($stmt = $this->mysqli->prepare("SELECT `id` FROM `bruger` WHERE `email`"))
         {
         $stmt->bind_param('s', $email_indhold);
         $email_indhold = $_POST["email"];


         $stmt->execute();
         $stmt->store_result();
         $stmt->bind_result($id);
         $stmt->fetch();
         $count = $stmt->num_rows;
         $stmt->close();

         if($count >= 1)
         {
            $pb = null;

            include "fun_filer/img/class.upload.php";
            $handle = new Upload($_FILES["file"]);

            if($handle->uploaded)
            {
                $handle->image_resize = true;
                $handle->image_ratio_crop = true;
                $handle->image_y = 75;
                $handle->image_x = 75;
                $handle->Process("images/bruger");
                $pb = $handle->file_dst_name;
            }

            if($stmt = $this->mysqli->prepare("INSERT INTO `bruger` (`rank`, `email`, `adgangskode`, `navn`, `efternavn`, `profil_img`) VALUES (?, ?, ?, ?, ?, ?)"))
            {
                $stmt->bind_param('isssss', $rank, $email, $adgangskode, $navn, $efternavn, $profil_img);

                $rank = 1;
                $email = $_POST["email"];
                $adgangskode = sha1($_POST["password_adgangskode_1"]);
                $navn = $_POST["fornavn"];
                $efternavn = $_POST["efternavn"];
                $profil_img = $pb;

                $stmt->execute();

                $navn = $_POST["navn"];
                $til = $_POST["email"];
                $password = $_POST["password_adgangskode_1"];


                $fra = "blabla.dk - Opret bruger <" . "info@blabla.dk" . ">";
                $til = $_POST["email"];
                $emne = "Velkommen til blabla.dk";

                $besked = file_get_contents('mail_opret.php');

                $besked = str_replace("%%navn", $navn, $besked);
                $besked = str_replace("%%brugernavn", $til, $besked);
                $besked = str_replace("%%password", $password, $besked);

                $headers = "MIME-Version: 1.0\n";
                $headers .= "Content-type: text/html; charset=UTF-8 \n";
                $headers .= "From: " . $fra . "\n";

                mail($til, $emne, $besked, $headers);

                ?>
                <div class="article-main-content">
                <div class="alert-message" style="background-color:#77b01e;"><span class="icon-text">&#10003;</span><span class="alert-content">Din bruger er blevet opret og du få en email fra os <a href="#" class="destroy-button"></a></div>
                </div>
                <?php

                $stmt->close();


            }
            else
            {
                echo 'Der opstod en fejl i erklæringen: ' . $this->mysqli->error;
            }
        }
        else
        {
        ?>
        <div class="article-main-content">
    <div class="alert-message" style="background-color:#c22525;"><span class="icon-text">&#9888;</span><span class="alert-content">Error</span><a href="#" class="destroy-button"></a></div>
    </div>
        <?php
        }
        }

2 个答案:

答案 0 :(得分:1)

您可以从数据库中查找电子邮件,看它是否与$_POST上的电子邮件相匹配。如果没有,你可以继续,如果没有,你可以显示错误。

foreach($this->mysqli->query("SELECT * FROM Users WHERE email='$email'") as $row) {
  if ($row['email'] !== $email) {  // if email doesn't equal to the email given
     // your code goes here
  } else {
     // you can put an error code here
}
}

如果您不想使用此功能,只需将表格中的电子邮件字段设置为'unique'
编辑:正如@NielsKeurentjes在下面的评论中所说,最好同时使用两者(如上所述的php并将表格中的电子邮件字段设置为'unique'

答案 1 :(得分:0)

只需使用select执行查询即可检索具有给定电子邮件的用户。 当行数等于0时,您可以从那里继续执行插入操作。 否则,显示错误“电子邮件已经注册。”

在电子邮件字段中添加唯一索引也是首选。