我不确定问题出在哪里。所以我将在这里给出一个简短的描述:我试图用PHP从头开始构建一个Web应用程序。有了这个,我希望页面几乎每个表单都是ajax。我创建了2个类(用户和客户端)。代码应该处理一个表单,通过另一个PHP文件提交给数据库。
class User{
public static function edit ($id, $user, $pass, $is_admin, $client){
global $db;
$id = mysqli_real_escape_string($db, $id);
$user = mysqli_real_escape_string($db, $user);
$pass = mysqli_real_escape_string($db, $pass);
$is_admin = mysqli_real_escape_string($db, $is_admin);
$client = mysqli_real_escape_string($db, $client);
if ($_SESSION['user']['is_admin'] == true){
if ($client == '')
$client = 'NULL';
$sql = "UPDATE users SET users.username = '{$user}', users.password = '{$pass}', users.is_admin = {$is_admin}, ";
$sql .= "users.client = '{$client}' WHERE users.id = {$id};";
$result = $db->query($sql);
if ($db->affected_rows > 0) {
$_SESSION['success'] = array( 1 => "All changes with the User has been saved." );
} else {
$_SESSION['errors'] = array( 1 => "No changes are made. Please contact the Administration for further assistance if needed." );
}
}
}
}
class Client{
public static function edit ($id, $name, $logo, $street, $city, $state, $zip, $phone, $email, $active){
global $db;
$sql = "UPDATE clients SET name=?, logo=?, street=?, city=?, state=?, zip_code=?, phone=?, email=?, active=? WHERE id=?;";
if ($stmt = $db->prepare($sql) and $_SESSION['user']['is_admin'] == true) {
$stmt->bind_param('s', $name);
$stmt->bind_param('s', $logo);
$stmt->bind_param('s', $street);
$stmt->bind_param('s', $city);
$stmt->bind_param('d', $state);
$stmt->bind_param('s', $zip);
$stmt->bind_param('s', $phone);
$stmt->bind_param('s', $email);
$stmt->bind_param('d', $active);
$stmt->bind_param('d', $id);
$stmt->excute();
if ($db->affected_rows > 0) {
$_SESSION['success'] = array( 1 => "All changes with the Client has been saved." );
} else {
$_SESSION['errors'] = array( 1 => $db->error );
}
} else $_SESSION['errors'] = array( 1 => $db->error );
}
}
处理ajax表单的javascript代码在这里:
function editUser() {
$.ajax({
type: 'POST',
url: 'editUser.php',
data: $('#editUserForm').serialize(),
cache: false,
dataType:'json'
}).always(function(){
renderUsers();
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
});
return false;
}
function editClient() {
$.ajax({
type: 'POST',
url: 'editClient.php',
data: $('#editClientForm').serialize(),
cache: false,
dataType:'json',
error: function(data){
console.log(data);
}
}).always(function(){
renderUsers();
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
});;
return false;
}
用于执行用户aka editUser.php文件的ajax提交的PHP代码:
<?php
require_once('class_lib.php');
if($_SESSION['user']['is_admin'] == true){
if(isset($_POST['id'])){
User::edit($_POST['id'], $_POST['username'], $_POST['password'], $_POST['is_admin'], $_POST['client']);
}
}
?>
这是editClient.php
<?php
require_once('class_lib.php');
if($_SESSION['user']['is_admin'] == true){
if(isset($_POST['id'])){
Client::edit($_POST['id'], $_POST['name'], $_POST['logo'], $_POST['street'], $_POST['city'], $_POST['state'], $_POST['zip_code'], $_POST['phone'], $_POST['email'], $_POST['active']);
}
if($_POST['process'] == 'newClient'){
Client::add();
}
}
?>
代码几乎完全相同(我复制并粘贴),我首先创建了用户功能并且它工作了所以我继续使用客户端,这就出现了错误。用户功能仍然完美,但客户端不是。我创建了2个虚拟客户端来测试编辑。第一个客户端打印没有错误($db->error
),但第二个虚拟客户端输出此You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' clients.phone = '',clients.email = '', clients.active = 1 WHERE clients.id = 2' at line 1
我现在在这里呆了一天,但仍然不知道为什么它不起作用。
更多信息:我测试了类功能,它工作正常。测试发送表单正常方式,它工作。是因为ajax没有发送正确的数据吗?为什么第一个客户没有显示错误?提前谢谢。
编辑:使用prepare语句而不是escape_string更改查询。
没有显示错误,但声明未执行。