我试图通过ajax调用上传图像到php文件并存储在mysql中。以下是ajax调用编码:
$('#save_user').click(function () {
if (validateForm()) {
$.ajax({
type: "POST",
url: "includes/user_add.php",
data: {
first_name: $("#first_name").val(),
last_name: $("#last_name").val(),
middle_name: $("#middle_name").val(),
title: $("#title").val(),
add1: $("#add1").val(),
add2: $("#add2").val(),
add3: $("#add3").val(),
city_name: $("#city_name").val(),
gender: $("#gender").val(),
country_name: $("#country_code").val(),
country_code: $("#country_code").val(),
identity: $("#identity").val(),
mobile: $("#mobile").val(),
phone: $("#phone").val(),
email: $("#email").val(),
new_username: $("#new_username").val(),
dob: $("#dob").val(),
image: $("#profile-img").val(),
edit_user_id: $("#role_id").val(),
register_user_role_id: $("#register_user_role_id").val()
},
dataType: "html"
}).done(function (msg) {
$('#success_useradd').html(msg);
//alert generated from the php file after successful or error
location.reload();
//document reloaded
});
} else {
//Any js to run if the validations fail
}
});
php文件如下:
但我无法将文件保存在DB中。我需要通过ajax调用提交信息,因为我的所有DB CRUD操作都是使用ajax调用完成的。 PHP文件如下:我将信息插入到名为users的mysql表中,列名为image。
<?php
session_start();
include 'db_connect.php';
if (isset($_SESSION['logged_in'])) :
$userrole = $_SESSION['role'];
$user_id = $_SESSION['id'];
$username = $_SESSION['username'];
else :
$userrole = '0';
header("location:login.php");
endif;
$image = $_FILES['image']['tmp_name'];
$imgContent = addslashes(file_get_contents($image));
function randomPassword() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass); //turn the array into a string
}
$pass = randomPassword(); //generate random password and assign to the password variable.
$new_username = $_POST['new_username'];
$pass_insert = sha1($pass);
$title = $_POST['title'];
$fname = $_POST['first_name'];
$mname = $_POST['middle_name'];
$lname = $_POST['last_name'];
$mobile = $_POST['mobile'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$add1 = $_POST['add1'];
$add2 = $_POST['add2'];
$add3 = $_POST['add3'];
$identity = $_POST['identity'];
$city = $_POST['city_name'];
$country = $_POST['country_name'];
$country_code = $_POST['country_code'];
$dob = $_POST['dob'];
$gender = $_POST['gender'];
$password_status = '0';
$user_status = 'P';
$role_id = $_POST['register_user_role_id'];
$create_date = date('Y-m-d H:i:s');
$update_date = date('Y-m-d H:i:s');
//User Insert Query
$newsql_add = "INSERT INTO `users`
(`id`,
`role_id`,
`username`,
`password`,
`title`,
`first_name`,
`middle_name`,
`last_name`,
`mobile`,
`phone`,
`email`,
`add1`,
`add2`,
`add3`,
`identity`,
`city`,
`country`,
`country_code`,
`dob`,
`password_status`,
`user_status`,
`create_date`,
`last_updated_date`,
`passwordgen`,`image`)
VALUES ('',
'$role_id',
'$new_username',
'$pass_insert',
'$title',
'$fname',
'$mname',
'$lname',
'$mobile',
'$phone',
'$email',
'$add1',
'$add2',
'$add3',
'$identity',
'$city',
'$country',
'$country_code',
'$dob',
'$password_status',
'$user_status',
'$create_date',
'$update_date',
'$pass','$imgContent') ";
if ($new_user_add_query = mysqli_query($conn, $newsql_add)):
echo "<script>";
echo "alert('User Added successfully')";
echo "</script>";
else:
echo "<script>";
echo "alert('User adding error')";
echo "</script>";
endif;