Not able to upload image in DB using Php ajax

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
kirtesh
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Feb 10, 2018 3:09 am

Sat Feb 10, 2018 3:12 am

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">&times;</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");
}
}

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 696
Joined: Mon Feb 22, 2016 5:52 pm

Sat Feb 10, 2018 10:13 am

What errors are you getting?

kirtesh
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Feb 10, 2018 3:09 am

Sun Feb 11, 2018 12:33 am

hyper wrote:
Sat Feb 10, 2018 10:13 am
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

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 696
Joined: Mon Feb 22, 2016 5:52 pm

Sun Feb 11, 2018 2:26 pm

Correct this line:

Code: Select all

<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: Select all

function insertEmployee($params) {
print_r($_POST);
...

kirtesh
New php-forum User
New php-forum User
Posts: 3
Joined: Sat Feb 10, 2018 3:09 am

Tue Feb 13, 2018 8:56 pm

Thanks

Post Reply