首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我一直有未明的TypeError: mysqli_error():参数#1 ($mysql)必须是mysqli类型,

我一直有未明的TypeError: mysqli_error():参数#1 ($mysql)必须是mysqli类型,
EN

Stack Overflow用户
提问于 2021-03-27 23:42:27
回答 2查看 25.9K关注 0票数 0

如果有人能帮我解决我的问题,我将非常感激。其中一个问题是,当我输入正确的数据并按submit键时,它一直向我显示这个错误:

代码语言:javascript
运行
复制
Fatal error: Uncaught TypeError: mysqli_error(): Argument #1 ($mysql) must be of type mysqli, bool given in /opt/lampp/htdocs/Try_Project/ServerForm.php:70 Stack trace: #0 /opt/lampp/htdocs/Try_Project/ServerForm.php(70): mysqli_error(false) #1 /opt/lampp/htdocs/Try_Project/SignUp.php(16): include('/opt/lampp/htdo...') #2 {main} thrown in /opt/lampp/htdocs/Try_Project/ServerForm.php on line 70

另一个是我不能显示所有的错误,这是重复的电子邮件,重复的电话号码和两个密码不匹配。

这是主要的signUp代码:

代码语言:javascript
运行
复制
<?php 
ini_set('display_errors', 1);  
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
<?php
 $duplicateEmail="";
 $duplicatePhone="";
 $notEqual="";
 $insertSuccess=false;
 $emailExist=false;
 $phoneExist=false;
 $passwordER=false;
 ?>

<?php include('ServerForm.php') ?>
<!DOCTYPE html>
<html>
<head>
    <title>The Silk Flower</title>
    <meta charset="utf8">
    <link rel="stylesheet" type="text/css" href="styles1.css">
</head>
<body>
    <?php include('includes/header.php') ?>

     
    <br>
    <br>
    <center><div>
        
        <?php 
    if($insertSuccess){
    header('location: index.html');
    }
        ?>
         <!-- start of the sign up page --> 
        <h1 class="title">Sign up</h1>
    </div>

        <!-- sign up form --> 
    <form method="post" action="<?php $_SERVER['PHP_SELF']?>" name="login" >

  <div class="container">
    <label for="Fname"><b>First Name</b></label>
    <input type="text" placeholder="First Name" name="Fname" autofocus required>
    
    <label for="Lname"><b>Last Name</b></label>
    <input type="text" placeholder="Last Name" name="Lname" required>
    
    <label for="email"><b>Email</b></label>
    <input type="email" placeholder="Enter Email" name="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" minlength="8"  required>
    
    <label for="psww"><b>Confirm Password</b></label>
    <input type="password" placeholder="Re-enter Password" name="psww" minlength="8"  required>
    
    <label for="t"><b>Phone</b></label>
    <input type ="tel" placeholder="eg. 0500000000" name = "t" required pattern="[0]{1}[5]{1}[0-9]{8}" maxlength="10">
    
    <label for="add"><b>Address</b></label>
    <input type="text" placeholder="Address" name="add" maxlength="60" required>
    
        <div class="product">
            <button style="width: auto" type="submit" name = "submit">Sign up</button>
      </div>
      <?php
        if($emailExist){
        print("<br><p style='color:red; margin-left:10px;'>". $duplicateEmail." </p>");}
         if ($phoneExist){
               print("<br><p style='color:red; margin-left:10px;'>". $duplicatePhone." </p>");}
         if ($passwordER){
               print("<br><p style='color:red; margin-left:10px;'>". $notEqual." </p>");}
        ?> 

  </div>
</form>
</center>
 <p> 
    
          <strong> <a href = "#top"> <img src="images/arrowtop.png" width="40" height="40">
              </a></strong>
         </p>
</body>
</html>

这是插入数据库的代码:

代码语言:javascript
运行
复制
<?php 
ini_set('display_errors', 1);  
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
?>
 <?php
// connect to the database
include('includes/db.php');   

if($_SERVER["REQUEST_METHOD"] == "POST"){
    
    
if(isset($_POST['submit'])){
    $Fname=$_POST['Fname'];  
    $Lname=$_POST['Fname'];  
    $email=trim($_POST['email']); 
    $psw=trim($_POST['psw']);
    $psww=trim($_POST['psww']);
    $t=trim($_POST['t']);
    $add=trim($_POST['add']);

    $duplicateEmail=false;
    $emailExist=false;
    $duplicateEmail="";
    
    $duplicatePhone=false;
    $phoneExist=false;
    $duplicatePhone="";
    
    $notEqual=false;
    $passwordEr=false;
    $notEqual="";
   
    $checkexistemail="SELECT Email FROM Users WHERE Email='" .$_POST["email"]."'";
    $checkexistphone="SELECT Pnumber FROM Users WHERE Pnumber='" .$_POST["t"]."'";
    $result2 = mysqli_query($db,$checkexistemail);
    $result3 = mysqli_query($db,$checkexistphone);
 
    if (!($result2) || !($result3))
         {
         print ("<p> Query couldn't be executed </p>");
         echo mysqli_error($result2);
         echo mysqli_error($result3);
         }
     if (mysqli_num_rows($result2)>0){
         $emailExist=true;
         $duplicateEmail="This email address already exists, please enter a new one.";
         }
     if (mysqli_num_rows($result3)>0){
        $phoneExist=true;
        $duplicatePhone="This Phone number already exists, please enter a new one.";
    }
    if($psw != $psww){
        $passwordEr=true;
        $notEqual="The two passwords do not match.";
    }
    else{
        $insertSuccess=false;
          //insert user data into database
    $query="INSERT INTO 'Users'('Fname', 'Lname', 'Email', 'Password', 'Pnumber', 'Address') 
              VALUES('$Fname', '$Lname', '$email','$psw', '$t', '$add)";

        
        $insertResult = mysqli_query($db,$query);
        if($insertResult){
            $insertSuccess=true;
        }
        else{
            echo mysqli_error($insertResult);
        }     
         }
    }
          
}
else{
        echo mysqli_error($db);   
        $duplicateEmail="";
        $duplicatePhone="";
}
    mysqli_close($db);
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-27 23:54:27

您向错误()提供一个结果,而您应该提供一个数据库句柄。因此,替换:

代码语言:javascript
运行
复制
$result2 = mysqli_query($db,$checkexistemail);
$result3 = mysqli_query($db,$checkexistphone);

if (!($result2) || !($result3))
     {
     print ("<p> Query couldn't be executed </p>");
     echo mysqli_error($result2);
     echo mysqli_error($result3);
     }

出自:

代码语言:javascript
运行
复制
$result2 = mysqli_query($db,$checkexistemail);
if (!$result2) {
    echo "<p> Query [$checkexistemail] couldn't be executed </p>";
    echo mysqli_error($db);
}
$result3 = mysqli_query($db,$checkexistphone);
if (!$result3) {
    echo "<p> Query [$checkexistphone] couldn't be executed </p>";
    echo mysqli_error($db);
}
票数 0
EN

Stack Overflow用户

发布于 2021-03-28 01:00:51

我的错误很简单,我忘了在$add附近添加一个单引号.很难为情。

我还修复了所有的mysqli_error

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66837160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档