drzfnr0275 2017-06-09 20:49
浏览 73
已采纳

jQuery Mobile表单发布问题,PHP警告:(期望参数1为mysqli,null给出)

home.html file:

Two simple jQ Mobile pages with a header and fixed footers. The form is on page one. I haven't added any stipulations like "password must match confirm password" for now. I just want to get it working with the DB first i.e. store in DB.

 <!DOCTYPE html>

 <html>
   <head>
   <!-- Include meta tag to ensure proper rendering and touch zooming -->
<meta name="viewport" content="width=device-width, initial-scale=1">
   <!-- Include jQuery Mobile stylesheets -->
<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" type="text/css" href="https://example.net/Login/styles.css"> 
   <!-- Include the jQuery library -->
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
   <!-- Include the your Ajax-ify file -->
<script type="text/javascript" src="https://example.net/Login/functions.js"></script> 
  <!-- Include the jQuery Mobile library -->
<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
 </head>
    <body>
                  <!--                      -->
                  <!-- Page One starts here -->
                  <!--                      -->

 <div data-role="page" id="pageone">
 <div data-role="header" data-theme="b">
     <h1>Welcome</h1>
 </div> 

 <div data-role="main" class="ui-content">
  <br>
  <br>
              <!-- Form starts here -->
  <form id="NewForm" method="post" action="include.php">  
         <label>Username</label>
    <input type="text" name="username" placeholder="E.g. JoeB95">
  <br>
         <label>Email</label> 
   <input type="text" name="email" placeholder="E.g. Joe@email.com">
  <br>
         <label>Password</label> 
  <input type="password" name="password_1" placeholder="E.g. 8969-545a-r3">
  <br>
  <!--Submit button goes below here -->                                                                                     
  <div id="register"><input type="submit" name="register" data-inline="true" value="Submit"></div>
  </form>
    <br>
    <p>Already a member?</p> <a href="#pagetwo">Sign In</a>
    <br> 
    <span id="result"></span>
    <br>
  </div>  

  <div data-role="footer" id="foot" data-position="fixed" data-theme="b">
    <h1>footer</h1>
  </div>  
  </div>   
                        <!--   Page One ends here    -->
                        <!--                         -->
                        <!--   Page Two starts here  -->


 <div data-role="page" id="pagetwo">
 <div data-role="header" data-theme="b">
     <h1>Welcome</h1>
 </div> 

 <div data-role="main" class="ui-content">
  <br>
  <br>
 <p>Not a member?</p> <a href="#pageone">Signup</a>
  <br>
 </div> 

  <div data-role="footer" id="foot" data-position="fixed" data-theme="b">
  <h1>footer</h1>
 </div>  
 </div>

functions.js file: (Ajax-ify)

Code for Ajax-ifying the form is below. I'm not sure if any errors exist here.

// AJAX-ify the form
$(document).on("pagecreate","#pageone",function()
{

$("#register").click( function() {
  $.post( $("#NewForm").attr("action"),
     $("#NewForm :input").serializeArray(),
        function(info){ $("#result").html(info);
  });
});

$("#NewForm").submit( function() {
   return false;
  });
});

db.php file:

This code does work, as it connects with the DB and I've used it before.

 <?php

 //put your connection code here
      $servername = 'localhost';
      $username = 'admin_example';
      $password = '123456-7';
      $dbname = 'example_DB';

    // create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    } 
    echo "Connected successfully, <br>";
 ?>

include.php file:

This is where I believe the issues are. In the error_log, it points to lines 8 and 11 as the problem areas;

PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in public_html/Login/include.php on line 8

PHP Warning: mysqli_error() expects parameter 1 to be mysqli, null given in public_html/Login/include.php on line 11

I know there are similar topics regarding these errors, but I couldn't relate it directly to mine. Can anyone suggest what the problem is and what needs to be changed? Thanks.

  <?php
  $user  = $_POST['username'];   
  $email = $_POST['email'];
  $password_1 = $_POST['password_1']; 

  $access = md5($password_1);   
  $sql = "INSERT INTO users (`username`, `email`, `password`) VALUES ('$username', '$email', '$password_1')";
  if(mysqli_query($conn,$sql)){
        echo "Successfully Inserted";
  }   else {
        echo ("Error description: " . mysqli_error($conn));
  }
 ?>
  • 写回答

1条回答 默认 最新

  • douyi1341 2017-06-09 21:05
    关注

    The home.html file calls include.php via the form action. However, include.php does not call the db.php file. It appears you're missing something like:

    require db.php;

    in your include.php file. Without it the $conn variable set in db.php on line 10 is not visible in the include.php file. That is causing the error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮