小编典典

mysqli_query()至少需要2个参数和mysqli_query():空查询错误消息

mysql

运行此代码时出现4个错误,这些错误将用户的电子邮件地址添加到名为ecommerce的表中称为的数据库中subscriptions

$ con = new mysqli(’localhost’,’root’,’‘,’ecommerce’);

if (!$con) {
    die('Could not connect: ' . mysql_error());
}

$errors = array();
if($_POST)
    {
        if(empty($_POST['email']))
        {
            $errors['email1'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:30px;float:right;'>Dont forget to write your email!</p>";
        }else {
            $email = test_input($_POST["email"]);

            // check if e-mail address is well-formed
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $errors['email2'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Something wrong is with your email</p>"; 
            }else{

                // check if the email already exists
                $query = mysqli_query("SELECT * FROM subscriptions WHERE email='$email'");
                if(mysqli_num_rows($query) > 0){
                    $errors['email3'] = "<p style='color:red;font-family: BCompset, Arial, Helvetica, sans-serif;font-size:25px;float:right;'>Your had been registered before!</p>";
                }
            }
        }

        //check errors
        if(count($errors) == 0)
        {
            $insert_email = mysqli_query("INSERT INTO subscriptions (email) VALUES ('$email')");
            $insert_email = mysqli_query($con, $insert_email);
            $success = "<script>alert('Your email was successfully added to our database!')</script>";
        }
    }

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>
<form action="" method="POST" class="searchform" dir="ltr">
                                <input type="text" name="email" placeholder="Your email address" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"/>
                                <button name="submit" type="submit" class="btn btn-default"><i class="fa fa-arrow-circle-o-right"></i></button>
                                <p><?php if(isset($errors['email1'])) echo $errors['email1']; ?></p>
                                <p><?php if(isset($errors['email2'])) echo $errors['email2']; ?></p>
                                <p><?php if(isset($errors['email3'])) echo $errors['email3']; ?></p>
                                <p><?php if(isset($success)) echo $success; ?></p>
                                <?php if(count($errors) == 0){echo "<p id='para' dir='rtl'>You can add your email to our emailsshow list.</p>";}?>
</form>

错误是这样的:

警告:mysqli_query()至少需要2个参数,第27行给出1个

第27行:

$ query = mysqli_query(“ SELECT * FROM subscriptions where where email =’$
email’”);

警告:mysqli_num_rows()期望参数1为mysqli_result,第28行给出null

第28行:

if(mysqli_num_rows($ query)> 0){

警告:mysqli_query()至少需要2个参数,第37行给出1个

第37行:

$ insert_email = mysqli_query(“ INSERT INTO subscriptions(电子邮件)VALUES(’$
email’)”);

警告:mysqli_query():第38行的查询为空

38行:

$ insert_email = mysqli_query($ con,$ insert_email);

我是这个论坛的新手,如果您可以帮助我,那么我真的很想做…最好,谢谢!


阅读 342

收藏
2020-05-17

共1个答案

小编典典

代替

$query = mysqli_query("SELECT * FROM subscriptions WHERE email='$email'");

采用

$query = $con->query("SELECT * FROM subscriptions WHERE email='$email'");

要么

$query = mysqli_query($con, "SELECT * FROM subscriptions WHERE email='$email'");

也代替

$insert_email = mysqli_query("INSERT INTO subscriptions (email) VALUES ('$email')");

采用

$insert_email = $con->query("INSERT INTO subscriptions (email) VALUES ('$email')");

这些是我可以看到的仅有的两个错误。

2020-05-17