Ajax PHP Scope问题
I'm having some problems connecting to my functions and database from an ajax call in a form.
<?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 Looks like this;
$(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;
});
I've got the ajax running on submit which is fine. Then it's to run the php, but I keep getting an error;
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/public/Assets/ajax/userdetails.php on line 12
And lastly the php called looks as follows, I assume it's something to do with the scope, but I'm still learning and been struggling with this for a couple of days, if anyone could give me some pointers it would be greatly appreciated.
Thanks
<?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));
}
?>
EDIT
$connection is defined in 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() . ")"
);
}
?>
You have invlid/dont have connection syntax as per mysqli.
Use it like:
$connection=mysqli_connect("localhost","Uname","PWD","DBName");
if (mysqli_connect_errno()) {
throw new Exception(mysqli_connect_error(), mysqli_connect_errno());
}
Update
As per your edited Question:
Use <?php require_once("../includes/db_connection.php"); ?>
inside your userdetails.php. As you need $connection parameter.
You forgot to define the $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));
}
?>
As pointed out by the others, you need to set the connection first. Moreover, your query string lacks single quotation marks around the $current_user variable:
$query = "UPDATE users SET FirstName = '$firstname', LastName = '$lastname', Gender = '$gender' WHERE id = '$current_user'";