dpy87530 2015-09-12 11:33
浏览 276
已采纳

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

I got 4 errors while running this code that add the email address of the user to the database called ecommerce in the table called 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>

The errors are like this:

Warning: mysqli_query() expects at least 2 parameters, 1 given on line 27

Line 27:

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

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given on line 28

Line 28:

if(mysqli_num_rows($query) > 0){

Warning: mysqli_query() expects at least 2 parameters, 1 given on line 37

Line 37:

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

Warning: mysqli_query(): Empty query on line 38

Line 38:

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

I'm new at this forum and it would best if you can help me with that cause I really do know to do ... thanks in advance!

  • 写回答

3条回答 默认 最新

  • douwaif22244 2015-09-12 11:42
    关注

    Instead of

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

    use

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

    or

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

    Also instead of

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

    use

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

    These are the only 2 errors that I could see.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试