mysqli_query和errror查询问题

时间:2018-04-03 07:46:19

标签: javascript php

我收到警告信息

  

警告:mysqli_query()期望参数1为mysqli,给定字符串   在第24行的C:\ xampp \ htdocs \ gurukul \ user \ view \ register_user.php

     

警告:mysqli_error()期望参数1为mysqli,给定字符串   在第24行的C:\ xampp \ htdocs \ gurukul \ user \ view \ register_user.php

以下是代码:

<?php             
    $fname = '';
    $lname = '';
    $email = ''; 
    $conn = '';
        if(isset($_POST ['register']))
        {
            // Assign local variables to form data
            $fname = trim($_POST['fname']);
            $lname = trim($_POST['lname']);
            $email = trim($_POST['email']);

            $fname = stripcslashes($fname) ;                                      
            $lname = stripcslashes($lname);
            $email = stripcslashes($email);

            if(!empty($fname and $lname and $email)){
                            // Connect to db


                // Validate if the email account already exists
                $query = "SELECT email FROM tbl_users  WHERE email ='$email'";
                //$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
                $result = mysqli_query($conn, $query) or die(mysqli_error($conn));

                $count = mysql_num_rows($result);
            /*
               echo $row[0];
               echo $count;
               echo $fname;
               echo $email;           
               echo $lname; 
              */
            // Generate random 32 character hash and assign it to a local variable.
            // Example output: f4552671f8909587cf485ea990207f3b
                $hash = md5( rand(0,1000) );
                $password = rand(1000,5000); // Generate random number between 1000 and 5000 and assign it to a local variable.    // Example output: 4568
                $md5_password  = md5($password) ;

            if (empty($row = mysqli_fetch_array($result))) 
               {              

                    $to      = $email; // Send email to our user
                    $subject = 'Signup | Verification'; // Give the email a subject 
                    $message = '

                                Thanks for signing up!
                                Your account has been created, you can login with the following credentials after you have activated your account by pressing the url below.

                                ------------------------
                                    Username: '.$email.'
                                    Temporary Password: '.$password.'
                                ------------------------

                                Please click this link to activate your account:
                               http://localhost/gurukul/user/verify.php?email='.$email.'&hash='.$hash.'

                               ';  // Activation link -Concatenate email and md5 hash - direct to verify.php to validate the link
                                 // Our message above including the link

                                $headers = 'From:noreply@yourwebsite.com' . "\r\n"; // Set from headers
                                $usertype = "Stu_Account";
                        if(mail($to, $subject, $message, $headers)){// Send our email  --- and if email sent successfully then insert data into db
                         // Insert new user data into users table
                         $query = "INSERT INTO tbl_users(f_name,l_name,email,hash,temp_password,user_type) VALUES ('$fname','$lname','$email','$hash','$md5_password','$usertype') ";
                         $result = mysqli_query($conn, $query) or die(mysqli_error($conn));
                         mysqli_close($conn);    

               //          echo $md5_password;
               //          echo 'check mail';                                                  
               //          echo $password;
                         ?>
                             <script type="text/javascript"> 
                              //   history.back(); 
                                 window.location.replace("index.php");
                                 alert("Your account is successfully created! \n An email is sent on your account.  Please check your email shortly to activate your account. ");                             
                            </script> 

                         <?php    

                        }  else{
                            ?>
                             <script type="text/javascript"> 
                                 history.back();
                                 alert("Email Could Not be Sent. \n\nYour Account is Not Created, Try Again Shortly. \n \nPlease Contact Administator if the Issue Persists. ");                             
                             </script>    
                            <?php
                        }                        
                }else{
                 // The Email Account is Already Registered';
                 ?>
                      <script type="text/javascript"> 
                        history.back(); 
                        alert("This email id is already registered.\n\n Please go to home page to login or create a new account with another email id.");                             
                    </script> 
                  <?php       

                }                

            }else{
                  ?>
                      <script type="text/javascript"> 
                        history.back(); 
                        alert("Some data is missing, please enter all the details..");                             
                    </script> 
                  <?php 
            }

        }
  ?>

1 个答案:

答案 0 :(得分:3)

你需要在

之前定义:$ conn

$ conn = mysqli_connect(&#34; localhost&#34;,&#34; my_user&#34;,&#34; my_password&#34;,&#34; db&#34;);