Not able to upload image in DB using Php ajax

A

Anonymous

Guest
I am uploading image with some other fields using php ajax, but the image is not getting uploaded in the DB. Here is the code :

<div id="add_model" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Add Products</h4>
</div>
<div class="modal-body">
<form method="post" id="frm_add">
<input type="hidden" value="add" name="action" id="action">
<div class="form-group">
<label for="product_title" class="control-label">Product Title:</label>
<input type="text" class="form-control" id="product_title" name="product_title"/>
</div>
<div class="form-group row">
<div class="col-sm-6">
<input type='file' name='file' class="form_file_upload" onchange="preview_image_01()" id="upload_file_01" multiple/>
<div id="image_preview_01"></div>
</div>

</div>
<div class="form-group">
<label for="product_description" class="control-label">Product Description:</label>
<textarea name="product_description" id="product_description" class="ckeditor" >Enter</textarea>
</div>

</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type='submit' class="btn btn-primary" id="btn_add" value='Save' name='but_upload'>

</div>
</form>
</div>
</div>
</div>


'$db = new dbObj();
$connString = $db->getConnstring();

$params = $_REQUEST;

$action = isset($params['action']) != '' ? $params['action'] : '';
$empCls = new Employee($connString);

switch($action) {
case 'add':
$empCls->insertEmployee($params);
break;
case 'edit':
$empCls->updateEmployee($params);
break;
case 'delete':
$empCls->deleteEmployee($params);
break;
default:
$empCls->getEmployees($params);
return;
}'
class Employee {
protected $conn;
protected $data = array();
function __construct($connString) {
$this->conn = $connString;
}

public function getEmployees($params) {

$this->data = $this->getRecords($params);

echo json_encode($this->data);
}

function insertEmployee($params) {
if(isset($_POST['btn_add'])){
$name = $_FILES['file']['name'];
$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif");
// Convert to base64
$image_base64 = base64_encode(file_get_contents($_FILES['file']['tmp_name']) );
$image = 'data:image/'.$imageFileType.';base64,'.$image_base64;

$data = array();
$sql = "INSERT INTO `images` (id, product_title, name, image, product_description) VALUES('" . $params["id"] . "', '" . $params["product_title"] . "','" . $params["name"] . "','" . $params["image"] . "','" . $params["product_description"] . "'); ";
$return = $_POST;
echo $result = mysqli_query($this->conn, $sql) or die("error to insert employee data");
}
}
 
hyper said:
What errors are you getting?
I am not getting any error, when i am using isset($_POST['btn_add']), the value is not updated in DB, & when i am removing this code i am not able to upload image
 
Correct this line:
Code:
<input type='submit' class="btn btn-primary" id="btn_add" value='Save' name='but_upload'>
And if you are still struggling, try doing this in your function:
Code:
function insertEmployee($params) {
print_r($_POST);
...
 
Back
Top