php mysql_query无法正常工作

时间:2017-02-23 18:34:13

标签: php mysql

我已调试到mysql_query,我认为这是问题所在。它在没有问题的情况下添加到表中,但是当检查它是否全部准备就绪时,则会出错。查询打印正确但res不打印。

mysql_connction.php:

<?php

    define('DBHOST', '@@@@@');
    define('DBUSER', '@@@@@');
    define('DBPASS', '@@@@@');
    define('DBNAME', '@@@@@');

    $connection = mysqli_connect(DBHOST,DBUSER,DBPASS,DBNAME);
    $db_selected = mysql_select_db('DBNAME', $connection);
    if ( !$connection ) {
        die("Connection failed : " . mysql_error());
    }
?>

Subscribe.php:

<?php require_once'mysql_connection.php';?>

<?php

    $error = false;

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

        $email = trim($_POST['email']);
        $email = strip_tags($email);
        $email = htmlspecialchars($email);

        $email = strtolower($email); 

        if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
            $error = true;
            $emailError = "Please enter valid email address.";
        } 
        else {
            // check email exist or not
            $query = "SELECT COUNT(`id`) FROM `subscribers` WHERE `email` = '$email'";
            echo $query;
            $res = mysqli_query($connection,$query);
            echo $res;
            $count = mysql_num_rows($res);
            echo $count;

            if($count>=0){
                $error = true;
                echo $error;
                $emailError = "Provided Email is already subscribed.";
                echo $emailError;
            }
        }
        if( !$error ) {
            $query = "INSERT INTO `subscribers`(`email`) VALUES ('$email')";
            $res = mysqli_query($connection,$query);

            if ($res) {
                $errTyp = "success";
                $errMSG = "Successfully subscribed";
                unset($email);
            } 
            else {
                $errTyp = "danger";
                $errMSG = "Something went wrong, try again later...";   
            }
        }
    }
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>CKEditor</title>
    </head>
    <body>
        <div id="login-form">
            <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" autocomplete="on">
        <div class="col-md-12">             
        <div class="form-group">
        <div class="input-group">
            <input type="email" name="email" class="form-control" placeholder="Your Email" value="<?php echo $email; ?>" maxlength="40" />
        </div>
        <span class="text-danger"><?php echo $emailError; ?></span>
    </div>
</div>
<div class="form-group">
    <button type="submit" class="btn btn-block btn-primary" name="btn-submit">Submit</button>
</div>

<div class="form-group">
<hr />
</div>          
</div>

</form>
</body>
</html>

0 个答案:

没有答案