我在使用表单中的ajax调用连接到我的函数和数据库时遇到了一些问题。
<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>
<?php include("../includes/layouts/header.php"); ?>
<form action="Assets/ajax/userdetails.php" id="ajax-user" method="post">
<div class="well" style="border-color: #b81e1e; display: none;" id="details1">
<div>
<label for="FirstName">First Name:<br /></label>
<input name="FirstName" type="text" />
</div>
<div>
<label for="LastName">Last Name:<br /></label>
<input name="LastName" type="text" />
</div>
<div>
<select name="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
</div>
<input class="btn btn-hero right" type="submit">
</div>
</form>
<?php include("../includes/layouts/footer.php"); ?>
Javascript看起来像这样;
$(document).on("submit", "form#ajax-user", function() {
var that = $(this),
url = that.attr("action"),
type = that.attr("method"),
data = {};
that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
console.log(response);
}
});
return false;
});
我已经在提交上运行了ajax,这很好。然后是运行php,但我一直收到错误;
警告:mysqli_query()要求参数1为mysqli,在 /home/public/Assets/ajax/userdetails.php 行中给出null
最后,php调用看起来如下,我认为它与范围有关,但我仍然在学习并且在这几天内一直在努力,如果有人能给我一些指示它会很大赞赏。
由于
<?php
if (isset($_POST['FirstName'], $_POST['LastName'], $_POST['gender'])) {
$current_user = $_SESSION["admin_id"];
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$gender = $_POST['gender'];
$query = "UPDATE users SET FirstName = '$firstname', LastName = '$lastname', Gender = '$gender' WHERE id = $current_user";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
}
?>
修改
$ connection在db_connection;
中定义<?php
define("DB_SERVER", "localhost");
define("DB_USER", "****");
define("DB_PASS", "****");
define("DB_NAME", "****");
// 1. Create a database connection
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
// Test if connection succeeded
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
答案 0 :(得分:2)
根据mysqli,你有enlid /没有连接语法。
使用它像:
$connection=mysqli_connect("localhost","Uname","PWD","DBName");
if (mysqli_connect_errno()) {
throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
}
<强>更新强>
根据您编辑的问题:
使用<?php require_once("../includes/db_connection.php"); ?>
在你的userdetails.php里面。因为你需要$ connection参数。
答案 1 :(得分:0)
正如其他人所指出的,您需要先设置连接。此外,您的查询字符串在$ current_user变量周围缺少单引号:
$query = "UPDATE users SET FirstName = '$firstname', LastName = '$lastname', Gender = '$gender' WHERE id = '$current_user'";
答案 2 :(得分:-1)
您忘了定义$ connection
<?php
if (isset($_POST['FirstName'], $_POST['LastName'], $_POST['gender'])) {
$current_user = $_SESSION["admin_id"];
$firstname = $_POST['FirstName'];
$lastname = $_POST['LastName'];
$gender = $_POST['gender'];
$connection = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($connection));
$query = "UPDATE users SET FirstName = '$firstname', LastName = '$lastname', Gender = '$gender' WHERE id = $current_user";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
}
&GT;