使用PHP

时间:2016-09-05 18:14:23

标签: php mysql database

您好我试图让我的注册页面检查用户名或电子邮件是否已被使用。但它只是代码而不是它们,而在你将它标记为Check if username already exists using PHP之前,我已经去了那里,我尝试了修复它们但我没有做到这一点这一点,我一无所知,我已经尝试了所有我知道的事情!

注册页面的HTML

<?php
    session_start();
        include 'header.php';

    $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (strpos($url, 'error=username') !== false) {
        echo "<div class='transition' style='height: 20px; background-color: #ff6b66; text-align: center; margin-top: 30px; margin-right: 40%; margin-left: 40%; border-radius: 20px; padding: 5px;'>Fill out username box!</div>";
    }
        $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (strpos($url, 'error=password') !== false) {
        echo "<div class='transition' style='height: 20px; background-color: #ff6b66; text-align: center; margin-top: 30px; margin-right: 40%; margin-left: 40%; border-radius: 20px; padding: 5px;'>Fill out password box!</div>";
    }
        $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (strpos($url, 'error=first') !== false) {
        echo "<div class='transition' style='height: 20px; background-color: #ff6b66; text-align: center; margin-top: 30px; margin-right: 40%; margin-left: 40%; border-radius: 20px; padding: 5px;'>Fill out First Name box!</div>";
    }
        $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (strpos($url, 'error=last') !== false) {
        echo "<div class='transition' style='height: 20px; background-color: #ff6b66; text-align: center; margin-top: 30px; margin-right: 40%; margin-left: 40%; border-radius: 20px; padding: 5px;'>Fill out Last Name box!</div>";
    }
        $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (strpos($url, 'error=email') !== false) {
        echo "<div class='transition' style=' transition-delay: 1s;box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); height: 20px; background-color: #ff6b66; text-align: center; margin-top: 30px; margin-right: 40%; margin-left: 40%; border-radius: 20px; padding: 5px;'>Fill out Email box!</div>";
    }
        $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (strpos($url, 'error=user_name_taken') !== false) {
        echo "<div class='transition' style='height: 20px; background-color: #ff6b66; text-align: center; margin-top: 30px; margin-right: 40%; margin-left: 40%; border-radius: 20px; padding: 5px;'>This username is already in use!</div>";
    }
        $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    if (strpos($url, 'error=user_email_taken') !== false) {
        echo "<div class='transition' style='height: 20px; background-color: #ff6b66; text-align: center; margin-top: 30px; margin-right: 40%; margin-left: 40%; border-radius: 20px; padding: 5px;'>This email is already in use!</div>";
    }


    if (isset($_SESSION['id']) !== true) {
          header('Location: ../login.php');
    }


?>

<html>
<head>
    <title>Add Teacher</title>
    <link rel="stylesheet" type="text/css" href="../assets/css/adduser.css">
</head>
    <body>
        <div class="loginbox">
            <h1 class="longintitle" style="font-family: Tahoma;">Add Teacher</h1>
            <form class="form" action="../includes/adduser.php" method="post" enctype="multipart/form-data">
                <input autocomplete="off" placeholder="Username" name="username" type="text" >
                <input autocomplete="off" placeholder="Password" name="password" type="password">
                <input autocomplete="off" placeholder="First Name" name="first" type="text">
                <input autocomplete="off" placeholder="Last Name" name="last" type="text">
                <input autocomplete="off" placeholder="Email" name="email" type="email">
                <input class="loginbutton" name="create" type="submit" value="Create">
            </form>
            <p>Students will be in beta copie THIS IS ALPHA</p>
        </div>
    </body>
</html>

php for it

<?php
session_start();
    include_once("../includes/db.php");

    $id = $_POST['id'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $first = $_POST['first'];
    $last = $_POST['last'];
    $email = $_POST['email'];

            if (empty($username)) {
        header('Location: ../teacher/adduser.php?error=username');
        exit();
    }
            if (empty($password)) {
        header('Location: ../teacher/adduser.php?error=password');
        exit();
    }
            if (empty($first)) {
        header('Location: ../teacher/adduser.php?error=first');
        exit();
    }
            if (empty($last)) {
        header('Location: ../teacher/adduser.php?error=last');
        exit();
    }
            if (empty($email)) {
        header('Location: ../teacher/adduser.php?error=email');
        exit();
    } else {

        $sql = "SELECT * FROM user WHERE username='".$username."'";
        $result = mysqli_query($conn, $sql);
        $usernamecheck = mysql_num_rows($result);

        if ($usernamecheck > 0) {
            header('Location: ../teacher/adduser.php?error=user_name_taken');
            exit();
        }

        $sql = "SELECT * FROM user WHERE email='$email'";
        $result = mysqli_query($conn, $sql);
        $emailtaken = mysql_num_rows($result);

        if ($emailtaken > 0) {
            header('Location: ../teacher/adduser.php?error=user_email_taken');
            exit();

        } else {
    $sql = "INSERT INTO user (id, username, password, first, last, email) VALUES ('$id', '$username', '$password', '$first', '$last', '$email')";
    $result = mysqli_query($conn, $sql);
    header('Location: ../teacher/adduser.php');
        }


    }
?>

如果需要,但是怀疑它是什么&#34; db.php

<?php  

$conn = mysqli_connect("localhost", "dbuser", "dbpass", "dbmain");

if (!@mysqli_connect("localhost", "dbuser", "dbpass", "dbmain")) {
    echo "<div style=' box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); padding: 3px; background-color: red; height: 20px;'><h3 style='text-align: center;'>Cannot connect to database have the admin take a look!</h3></div>";
    die(mysql_error());
}
 else {
    echo "<div style=' box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); padding: 3px; background-color: lightgreen; height: 20px;'><h3 style='text-align: center;'>Connected to database Successfully!</h3></div>";
 }

?>

请帮助我不知道如何解决这个问题!如果您需要更多信息,请询问。

提前致谢!

2 个答案:

答案 0 :(得分:0)

当你使用mysqli时,我认为你可能需要用mysql i _num_rows替换mysql_num_rows。 (缺少&#39;我在mysqli_num_rows中)。

答案 1 :(得分:-1)

替换&#34; mysql_num_rows&#34;用&#34; mysqli_num_rows&#34; ,同时获取行。因为$ conn是一个&#34; mysqli_connect&#34;实例