2

So I have a form running on a page that I'm experimenting with. Atm I just have one input and that is email. Basically I want a register and if a person tries to add the same email again it will return an error msg and otherwise it will add it.

So this is the code I've been posting with:

<?php
    $username="user";
    $password="pass";
    $database="test";
    $email = mysql_real_escape_string( $_POST['email'] );

    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");

    $query = "SELECT * FROM enroll WHERE email='$email' ";
    $result = mysql_query($query) or die(mysql_error());

    if (mysql_num_rows($result) ) {
        print 'user is already in table';
    }
    else {
        $query = "INSERT INTO enroll VALUES ('','$email')";
        $result = mysql_query($query) or die(mysql_error());       
        print 'user added';
    }
?>

That works fine! If I add an email that already exists it says "user is already in table" and if not it says "user added".

Problem is that before I used to run another php script which validated the input and then upon submit it added it to the DB. Now it ALWAYS says "user is already in table" even before I've pressed Submit.

I would like to do something like this, but when posting it doesn't work at all and I can't understand what's wrong.

<?php
    //If the form is submitted
    if(isset($_POST['submit'])) {

        // Required field names
        $required = array('email');

        // Loop over field names, make sure each one exists and is not empty
        $error = false;
        foreach($required as $field) {
            if (empty($_POST[$field])) {
            $error = true;
            }
        }

            if ($error) {
                echo 'You forgot to fill in your email';
            } else {
                $username="user";
                $password="pass";
                $database="test";
                $email = mysqli_real_escape_string( $_POST['email'] );

                mysqli_connect(localhost,$username,$password);
                mysqli_select_db($database) or die( "Unable to select database");

                $query = "SELECT * FROM enroll WHERE email='$email' ";
                $result = mysqli_query($query) or die(mysqli_error());

                if (mysqli_num_rows($result) ) {
                    print 'user is already in table';
                }
                else {
                    $query = "INSERT INTO enroll VALUES ('','$email')";
                    $result = mysqli_query($query) or die(mysqli_error());
                    print 'user added';
                }
            }
    }

?>

In my world, this checks if email is supplied, checks if it exists in db and if not adds it. If it exists it will come back with an error.

What am I doing wrong?

EDIT2: @ExpertSystem This is the code I'm going with now as per your recommendation:

<?php

        if(isset($_POST['submit'])) {

            // Required field names
            $required = array('email');

            // Loop over field names, make sure each one exists and is not empty
            $error = false;
            foreach($required as $field) {
                if (empty($_POST[$field])) {
                $error = true;
                }
            }

                if ($error) {
                    echo 'You forgot to fill in your email';
                } else {
                    $username="root";
                    $password="root";
                    $host="localhost";
                    $database="test";
                    $email = mysqli_real_escape_string( $_POST['email'] );

                    $link = mysqli_connect(localhost, $username, $password);
                    mysqli_select_db($link, $database) or die("Unable to select database");

                    $query = "SELECT * FROM enroll WHERE email='$email'";
                    $result = mysqli_query($link, $query) or die(mysqli_error($link));

                    if (mysqli_num_rows($result) ) {
                        print 'user is already in table';
                    }
                    else {
                        $query = "INSERT INTO enroll (email) VALUES ('$email')";
                        $result = mysqli_query($link, $query) or die(mysqli_error($link));

                        print 'user added';
                    }
                }
        }
?>
4

3 回答 3

0

我的脑袋说,“mysqli_num_rows($result);” 返回一个整数。所以你不能做 if(mysqli_num_rows($result)),它总是会返回一个整数(例如 0 或 1)

所以试试这个:

 if (1==mysqli_num_rows($result)) {
                print 'already in table';
 }

只是一个想法。

于 2013-05-14T15:45:42.957 回答
0

由于您使用的是mysqli_函数的过程形式,因此您需要将mysqli 链接标识符作为第一个参数传递给mysqli_select_db(), mysqli_real_escape_string(), mysqli_query()and mysqli_error()(有关更多详细信息,另请参阅文档)。前面提到的mysqli 链接标识符mysqli_connect().
修改后的代码应如下所示:

...
$link = mysqli_connect(localhost, $username, $password);
mysqli_select_db($link, $database) or die("Unable to select database");
$email = mysqli_real_escape_string($link, $_POST["email"]);
...
$result = mysqli_query($link, $query) or die(mysqli_error($link));
...
if...
    ...
} else {
    ...
    $result = mysqli_query($link, $query) or die(mysqli_error($link));
    ...
...
于 2013-05-14T17:06:55.220 回答
0

感谢你的帮助!希望我至少在您的帮助下改进了我的错误代码。

我发现了导致问题的原因,就是这个。

<input type="submit">

我把它改成了

<input type="submit" name="submit">

当然,现在一切正常。谢谢你!:)

于 2013-05-16T09:38:50.007 回答