我第一次有一个模型视图控制器(MVC)项目,完成该项目后,它可以在本地PHP版本7.3.9服务器(Xampp)上正常运行,但是一旦我托管在远程服务器上。模型函数之一,例如mdlAddUser($ table,$ data)无法插入数据库。该模型中的其他功能在远程服务器和脱机状态下均可正常运行。
mdlAddUser()继续返回“错误”消息,这意味着插入失败。
我需要帮助,以便可以成功将用户添加到数据库。提前致谢 。
这是我的标准代码:
require_once“ connection.php”;
Class UsersModel {
/*=============================================
SHOW USER
=============================================*/
static public function MdlShowUsers($tableUsers, $item, $value){
if($item != null){
$stmt = Connection::connect()->prepare("SELECT * FROM $tableUsers WHERE $item = :$item");
$stmt -> bindParam(":".$item, $value, PDO::PARAM_STR);
$stmt -> execute();
return $stmt -> fetch();
}
else{
$stmt = Connection::connect()->prepare("SELECT * FROM $tableUsers");
$stmt -> execute();
return $stmt -> fetchAll();
}
$stmt -> close();
$stmt = null;
}
/*=============================================
ADD USER
=============================================*/
static public function mdlAddUser($table, $data){
$stmt = Connection::connect()->prepare("INSERT INTO $table(name, user, password, profile, photo) VALUES (:name, :user, :password, :profile, :photo)");
$stmt -> bindParam(":name", $data["name"], PDO::PARAM_STR);
$stmt -> bindParam(":user", $data["user"], PDO::PARAM_STR);
$stmt -> bindParam(":password", $data["password"], PDO::PARAM_STR);
$stmt -> bindParam(":profile", $data["profile"], PDO::PARAM_STR);
$stmt -> bindParam(":photo", $data["photo"], PDO::PARAM_STR);
if ($stmt->execute()) {
return "ok";
} else {
return "error";
}
$stmt -> close();
$stmt = null;
}
/*=============================================
EDIT USER
=============================================*/
static public function mdlEditUser($table, $data){
$stmt = Connection::connect()->prepare("UPDATE $table set name = :name, password = :password, profile = :profile, photo = :photo WHERE user = :user");
$stmt -> bindParam(":name", $data["name"], PDO::PARAM_STR);
$stmt -> bindParam(":user", $data["user"], PDO::PARAM_STR);
$stmt -> bindParam(":password", $data["password"], PDO::PARAM_STR);
$stmt -> bindParam(":profile", $data["profile"], PDO::PARAM_STR);
$stmt -> bindParam(":photo", $data["photo"], PDO::PARAM_STR);
if ($stmt->execute()) {
return 'ok';
} else {
return 'error';
}
$stmt -> close();
$stmt = null;
}
/*=============================================
EDIT USER
=============================================*/
static public function mdlEditUserStudent($table, $data){
$stmt = Connection::connect()->prepare("UPDATE $table set phone = :phone, password = :password, address = :address, photo = :photo WHERE id = :idStudent");
$stmt -> bindParam(":phone", $data["phone"], PDO::PARAM_STR);
$stmt -> bindParam(":idStudent", $data["idStudent"], PDO::PARAM_INT);
$stmt -> bindParam(":password", $data["password"], PDO::PARAM_STR);
$stmt -> bindParam(":address", $data["address"], PDO::PARAM_STR);
$stmt -> bindParam(":photo", $data["photo"], PDO::PARAM_STR);
if ($stmt->execute()) {
return 'ok';
} else {
return 'error';
}
$stmt -> close();
$stmt = null;
}
}
这是我的控制器代码
类ControllerUsers {
/ ============================================ == 创建用户 ======================================== /
static public function ctrCreateUser(){
if (isset($_POST["newUser"])) {
if (preg_match('/^[a-zA-Z0-9ñÑáéíóúÁÉÍÓÚ ]+$/', $_POST["newName"]) &&
preg_match('/^[a-zA-Z0-9]+$/', $_POST["newUser"]) &&
preg_match('/^[a-zA-Z0-9]+$/', $_POST["newPasswd"])){
/*=============================================
VALIDATE IMAGE
=============================================*/
$photo = "";
if (isset($_FILES["newPhoto"]["tmp_name"])){
list($width, $height) = getimagesize($_FILES["newPhoto"]["tmp_name"]);
$newWidth = 500;
$newHeight = 500;
/*=============================================
Let's create the folder for each user
=============================================*/
$folder = "views/img/users/".$_POST["newUser"];
mkdir($folder, 0755);
/*=============================================
PHP functions depending on the image
=============================================*/
if($_FILES["newPhoto"]["type"] == "image/jpeg"){
$randomNumber = mt_rand(100,999);
$photo = "views/img/users/".$_POST["newUser"]."/".$randomNumber.".jpg";
$srcImage = imagecreatefromjpeg($_FILES["newPhoto"]["tmp_name"]);
$destination = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($destination, $srcImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagejpeg($destination, $photo);
}
if ($_FILES["newPhoto"]["type"] == "image/png") {
$randomNumber = mt_rand(100,999);
$photo = "views/img/users/".$_POST["newUser"]."/".$randomNumber.".png";
$srcImage = imagecreatefrompng($_FILES["newPhoto"]["tmp_name"]);
$destination = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresized($destination, $srcImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
imagepng($destination, $photo);
}
}
$table = 'users';
$encryptpass = crypt($_POST["newPasswd"], '$2a$07$asxx54ahjppf45sd87a5a4dDDGsystemdev$');
$data = array('name' => $_POST["newName"],
'user' => $_POST["newUser"],
'password' => $encryptpass,
'profile' => $_POST["newProfile"],
'photo' => $photo);
$answer = UsersModel::mdlAddUser($table, $data);
if ($answer == 'ok') {
echo '<script>
swal({
type: "success",
title: "User added succesfully!",
showConfirmButton: true,
confirmButtonText: "Close"
}).then(function(result){
if(result.value){
window.location = "users";
}
});
</script>';
}
else{
echo '<script>
swal({
type: "error",
title: "User could not save!",
showConfirmButton: true,
confirmButtonText: "Close"
}).then(function(result){
if(result.value){
window.location = "users";
}
});
</script>';
}
}else{
echo '<script>
swal({
type: "error",
title: "No Special characters or blank fields",
showConfirmButton: true,
confirmButtonText: "Close"
}).then(function(result){
if(result.value){
window.location = "users";
}
});
</script>';
}
}
}
/*=============================================
SHOW USER
=============================================*/
static public function ctrShowUsers($item, $value){
$table = "users";
$answer = UsersModel::MdlShowUsers($table, $item, $value);
return $answer;
}
}
这是我从包含HTML表单的View中调用adduser函数的方式
$createUser = new ControllerUsers();
$createUser -> ctrCreateUser();