douqian2334 2016-04-07 16:28
浏览 50
已采纳

警告:mysqli_error()期望在第31行的D:\ xampplite \ htdocs \ shop egistration.php中给出正好1个参数0 [复制]

This question already has an answer here:

I m trying to create a registration system but when I press the button register I get this error,can anyone tell me where I m going wrong This is my registration php page where the user enters their details.

      $servername = "localhost"; 
    $username = "root"; $password = ""; 
    $dbname = "users_db";
     $conn = new mysqli($servername, $username, $password, $dbname); 

     if (mysqli_connect_errno())
         { echo "MySQLi Connection was not established: " . mysqli_connect_error();
     } 

    ?> 
    <html>
    <head>
    <title>Beginning PHP5,APACHE AND MySQLi</title>
    </head>
    <body>
    <?php
    if (isset($_POST['submit']) && $_POST['submit'] == "Register") {
    if ($_POST['username'] != "" &&
    $_POST['password'] != "" &&
    $_POST['first_name'] != "" &&
    $_POST['last_name'] != "" &&
    $_POST['email'] != "") {
    $query = "SELECT username FROM user_info " .
    "WHERE username = '" . $_POST['username'] . "';";

    $result = mysqli_query($conn, $query) 
    or die(mysqli_error()); **This is line 31**
     if (mysqli_num_rows($result) != 0){


         ?>

     <p>
    <p>
    <font color="#FF0000"><b>The Username,
    <?php echo $_POST['username']; ?>, is already in use, please choose
    another!</b></font>
    <form action="registration.php" method="post">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"
    value="<?php echo $_POST['password']; ?>"><br>
    Email: <input type="text" name="email"
    value="<?php echo $_POST['email']; ?>"><br>
    First Name: <input type="text" name="first_name"
    value="<?php echo $_POST['first_name']; ?>"><br>
    Last Name: <input type="text" name="last_name"
    value="<?php echo $_POST['last_name']; ?>"><br>
    City: <input type="text" name="city"
    value="<?php echo $_POST['city']; ?>"><br>
    County: <input type="text" name="state"
    value="<?php echo $_POST['county']; ?>"><br>
    Car Models: choose at least one)<br>
    <select name="car_type" size="" multiple>
    <option value="Volkswagen"<?php
    if(in_array("Volkswagen", $_POST['car_type'])){
        echo "selected";
    }?>>BMW</option>
    <option value="BMW"<?php
    if(in_array("BMW", $_POST['car_type'])){
        echo "selected";
    }?>>Ford</option>
    <option value="Ford"<?php
    if(in_array("Ford", $_POST['car_type'])){
        echo "selected";
    }?>>Fiat</option>
    <option value="Fiat"<?php
    if(in_array("Fiat", $_POST['car_type'])){
        echo "selected";
    }?>>Toyota</option>
    <option value="Toyota"<?php
    if(in_array("Toyota", $_POST['car_type'])){
        echo "selected";
    }?>>Other Than Listed</option>
    <option value="Other Than Listed"<?php
    if (in_array("Other Than Listed", $_POST['hobbies'])) {
    echo " selected";
    } ?>>Other Than Listed</option>
    </select><br><br>
    <input type="submit" name="submit" value="Register"> &nbsp;
    <input type="reset" value="Clear">
    </form>
    </p>
    <?php
    } else {
    $query = "INSERT INTO user_info (username, password, email, " .
    "first_name, last_name, city, county, car_type) " .
    "VALUES ('" . $_POST['username'] . "', " .
    "(PASSWORD('" . $_POST['password'] . "')), '" .
    $_POST['email'] . "', '" . $_POST['first_name'] .
    "', '" . $_POST['last_name'] . "', '" . $_POST['city'] .
    "', '" . $_POST['county'] . "', '" .
    implode(", ", $_POST['car_type']) . "');";
    $result = mysqli_query($conn,$query) or die(mysqli_error());
    $_SESSION['user_logged'] = $_POST['username'];
    $_SESSION['user_password'] = $_POST['password'];
    ?>
    <p>
    Thank you, <?php echo $_POST['first_name'] . " " .
    $_POST['last_name']; ?> for registering!<br>
    <?php
    header("Refresh: 5; URL=index.php");
    echo "Your registration is complete! " .
    "You are being sent to the page you requested!<br>";
    echo "(If your browser doesn’t support this, ".
    "<a href=\"index.php\">click here</a>)";
    die();
    }
    } else {
    ?>
    <p>
    <font color="#FF0000"><b>The Username, Password, Email, First Name,
    and Last Name fields are required!</b></font>
    <form action="registration.php" method="post">
    Username: <input type="text" name="username"
    value="<?php echo $_POST['username']; ?>"><br>
    Password: <input type="password" name="password"
    value="<?php echo $_POST['password']; ?>"><br>
    Email: <input type="text" name="email"
    value="<?php echo $_POST['email']; ?>"><br>
    First Name: <input type="text" name="first_name"
    value="<?php echo $_POST['first_name']; ?>"><br>
    Last Name: <input type="text" name="last_name"
    value="<?php echo $_POST['last_name']; ?>"><br>
    City: <input type="text" name="city"
    value="<?php echo $_POST['city']; ?>"><br>
    State: <input type="text" name="state"
    value="<?php echo $_POST['state']; ?>"><br>
    Car models: (choose at least one)<br>
    <select name="car_type[]" size="10" multiple>
    <option value="Volkswagen"<?php
    if (in_array("Volkswagen", $_POST['car_type'])) {
    echo " selected";
    } ?>>Volkswagen</option>
    <option value="BMW"<?php
    if (in_array("BMW", $_POST["car_type"])) {
    echo " selected"; 
    } ?>>BMW</option>
    <option value="Ford"<?php
    if (in_array("Ford", $_POST["car_type"])) {
    echo " selected";
    } ?>>Ford</option>
    <option value="Toyota"<?php
    if (in_array("Toyota", $_POST["car_type"])) {
    echo " selected";
    } ?>>Toyota</option>
    <option value="Fiat"<?php
    if (in_array("Fiat", $_POST["car_type"])) {
    echo " selected";
    } ?>>"Fiat</option>
    <option value=">Other Than Listed"<?php
    if (in_array(">Other Than Listed", $_POST["car_type"])) {
    echo " selected";
    } ?>>Other Than Listed</option>
    </select><br><br>
    <input type="submit" name="submit" value="Register"> &nbsp;
    <input type="reset" value="Clear">
    </form>
    </p>
    <?php
    }
    } else {
    ?>
    <p>
    Welcome to the registration page!<br>
    The Username, Password, Email, First Name, and Last Name fields
    are required!
    <form action="registration.php" method="post">
    Username: <input type="text" name="username"><br>
    Password: <input type="password" name="password"><br>
    Email: <input type="text" name="email"><br>
    First Name: <input type="text" name="first_name"><br>
    Last Name: <input type="text" name="last_name"><br>
    City: <input type="text" name="city"><br>
    County: <input type="text" name="county"><br>
    Car models: (choose at least one)<br>
    <select name="car_type[]" size=”10” multiple>
    <option value="Volkswagen">Golfing</option>
    <option value="BMW">BMW</option>
    <option value="Ford">Ford</option>
    <option value="Toyota">Toyota</option>
    <option value="Fiat">Fiat</option>
    <option value="Other Than Listed">Other Than Listed</option>

    </select><br><br>
    <input type="submit" name="submit" value="Register"> &nbsp;
    <input type="reset" value="Clear">
    </form>
    </p>
    <?php
    }
    ?>
    </body>
    </html>
</div>
  • 写回答

1条回答 默认 最新

  • douyiken0968 2016-04-07 16:35
    关注

    Looks like you're mixing up the object oriented and the procedural approach.

    You may wanna refer to this: http://php.net/manual/en/mysqli.error.php

    From what I see, you prefer the procedural approach. So, you should try something like this to check if your database has been connected successfully:

    $conn = mysqli_connect($servername, $username, $password, $dbname);
    
    if (mysqli_connect_errno()) {
         die(mysqli_connect_error());
    } else {
        die("Connection successful!");
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀