我正在尝试将数据插入 MySQL 数据库。数据插入成功。但在成功插入ajax后我没有得到返回消息。我也使用最新的 jquery 版本和 PHP 版本。
剧本
<script>
$(document).ready(function () {
$('#data_form').on("submit",() => {
var formData = $('#data_form').serialize();
if (!$.active) {
$.ajax({
type: 'POST',
url: '../../../controller/add_user.php',
data :formData,
cache: false,
success: function (data) {
console.log(data.message);
},
error: function(xhr, status, error) {
console.log(xhr.responseJSON.message);
}
});
}
});
});
</script>
add_user.php
<?php
include_once('../model/database_11.php');
$db_obj = new Dbconnection();
$dbconn = $db_obj->ConnectToDB();
class admin
{
public $dbconn;
public $name;
public $account_type;
public $telephone = null;
public $mobile;
public $address;
public $pincode;
public $email_user;
public $gender;
public $dob;
public $status;
public $username;
public $password;
public $query;
public function __construct($dbconn, $name, $account_type, $telephone, $mobile, $address, $pincode, $email_user, $gender, $dob, $status, $username, $password)
{
$this->dbconn = $dbconn;
$this->name = $name;
$this->account_type = $account_type;
$this->telephone = $telephone;
$this->mobile = $mobile;
$this->address = $address;
$this->pincode = $pincode;
$this->email_user = $email_user;
$this->gender = $gender;
$this->dob = date('Y-m-d', strtotime($dob));
$this->status = $status;
$this->username = $username;
$this->password = $password;
}
function adduser()
{
$this->query = "INSERT INTO `users` (`ID_USER`, `WEB_USERNAME`, `WEB_USERPASSWORD`, `USER_TYPE`, `NAME`, `IMAGE`, `TELEPHONE`, `MOBILE`, `ADDRESS`, `PIN`, `EMAIL`, `GENDER`, `DATE_OF_BIRTH`, `STATUS`) VALUES (NULL, '$this->username', '$this->password', '$this->account_type', '$this->name', NULL, '$this->telephone', '$this->mobile', '$this->address', '$this->pincode', '$this->email_user', '$this->gender', '$this->dob', '$this->status');";
if ($this->dbconn->query($this->query) === TRUE) {
return "200";
} else {
return "500";
}
}
public function __destruct()
{
$this->dbconn->close();
}
}
extract($_POST);
$admin = new admin($dbconn, $name, $account_type, $telephone, $mobile, $address, $pincode, $email_user, $gender, $dob, $status, $username, $password);
$res = $admin->adduser();
echo $res;
?>
我尝试将 echo 语句更改为 http_response_code。但还是不行。
正如您在评论中看到的,您需要发回 json
function adduser()
{
$response = array();
$this->query = "INSERT INTO `users` (`ID_USER`, `WEB_USERNAME`, `WEB_USERPASSWORD`, `USER_TYPE`, `NAME`, `IMAGE`, `TELEPHONE`, `MOBILE`, `ADDRESS`, `PIN`, `EMAIL`, `GENDER`, `DATE_OF_BIRTH`, `STATUS`) VALUES (NULL, '$this->username', '$this->password', '$this->account_type', '$this->name', NULL, '$this->telephone', '$this->mobile', '$this->address', '$this->pincode', '$this->email_user', '$this->gender', '$this->dob', '$this->status');";
if ($this->dbconn->query($this->query) === TRUE) {
$response['status'] = "success";
$response['message'] = "User added successfully";
} else {
$response['status'] = "error";
$response['message'] = "Error adding user: " . $this->dbconn->error;
}
return json_encode($response);
}