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



home.html file:

两个简单的jQ移动页面,带有页眉和固定页脚。表格在第一页。我暂时没有添加任何规定,例如"密码必须与确认密码匹配"。我只想让它先与数据库一起工作,即存储在数据库中。

<!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>

函数.js文件:(Ajax-ify)

Ajax化表单的代码如下。我不确定这里是否存在任何错误。

// 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 文件:

这段代码确实有效,因为它与数据库连接,我以前用过它。

<?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>";
?>

包括.php文件:

这就是我认为问题所在。在error_log中,它指出第 8 行和第 11 行是问题区域;

PHP 警告:mysqli_query() 期望参数 1 为 mysqli,在第 8 行的 public_html/Login/include.php 中给出 null

PHP 警告:mysqli_error() 期望参数 1 是 mysqli,空值在第 11 行的 public_html/Login/include.php 中给出

我知道关于这些错误有类似的主题,但我无法将其与我的错误直接联系起来。谁能建议问题是什么以及需要改变什么?谢谢。

<?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));
}
?>

home.html 文件调用包括.php通过表单操作。 但是,include.php 不会调用 db.php 文件。 您似乎缺少以下内容:

require db.php;

在您的包含.php文件中。 没有它,第 10 行的 db.php 中设置的 $conn 变量在 include.php 文件中不可见。 这导致了错误。

最新更新