Save image url to Mysql Database

I have a database named 'mysqlproject' that contains two tables. One of the tables that i am interesting in is named markers and declared in database like this :

`id` int(11) NOT NULL,
`TitleEvent` varchar(60) NOT NULL,
`Description` varchar(80) NOT NULL,
`lat` float(10,6) NOT NULL,
`lng` float(10,6) NOT NULL,
`type` varchar(255) NOT NULL,
`status` varchar(30) NOT NULL,
`EventUserName` varchar(60) NOT NULL,
`PhotosEvent` varchar(255) NOT NULL

One of the columns is PhotosEventthat in there i want to save photo's url. So i capture a photo through a javascript file and i am sending it through ajax to a php file "camera.php"

The ajax sending procedure is:

        type: "POST",
        url: "camera.php",
        data: { 
        imgBase64: dataURL
        }).done(function( respond ) {
          // you will get back the temp file name
          // or "Unable to save this image."

So i am sending with method POST the image to the file "camera.php". In php file i am taking that image and saving it first in my "local server" in folder "EventImages". Ok that works .. But i want to save the url of the image in database in the field "PhotosEvent" only in raw where the active id is located..

Connecting with database :

$connection = mysql_connect('localhost', 'root', '');
if (!$connection) //Success $Connection with server returns 1
die("Database Connection Failed" . mysql_error());
//Connection with server established

// Try Connection with mysql Database
$select_db = mysql_select_db('mysqlproject');
if (!$select_db) //Success $Connection with Database returns 1
die("Database Selection Failed" . mysql_error());
//Connection with Database established   

Save image to folder as .png (works) and attemp saving url to database (don't work):

if ( isset($_POST["imgBase64"]) && !empty($_POST["imgBase64"]) ) {    

define('UPLOAD_DIR', 'EventImages/');

// get the dataURL
$dataURL = $_POST["imgBase64"];  

// the dataURL has a prefix (mimetype+datatype) 
// that we don't want, so strip that prefix off
$parts = explode(',', $dataURL);  
$data = $parts[1];  

// Decode base64 data, resulting in an image
$data = base64_decode($data);  

// create a temporary unique file name
$file = UPLOAD_DIR . uniqid() . '.png';

// write the file to the upload directory
$success = file_put_contents($file, $data);

// return the temp file name (success)
// or return an error message just to frustrate the user (kidding!)
print $success ? $file : 'Unable to save this image.';

 $results = mysql_query("INSERT INTO markers (PhotosEvent) WHERE id ='".$_SESSION['id']."'VALUES('$dataURL')");

My problem is saving the image url - path to database. How can i do this? Thanks in advance..



Found it!

define('UPLOAD_DIR', 'EventImages/');
$img = $_POST['imgBase64'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);

$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';

$result=mysql_query("SELECT id FROM markers ORDER BY id DESC LIMIT 1");
while ($row = mysql_fetch_array($result)){
$results = mysql_query("UPDATE markers SET PhotosEvent='".$file."' WHERE id = '".$LastInsertID."'");    

now it works ok.. I am fetcing the last known id that is in table Markers and i save the image's url that is stored in variable $file in the database!! I also save the image to folder EventImages as i did before!!


If you want to do an INSERT, then your VALUES is misplaced.


$results = mysql_query("INSERT INTO markers (PhotosEvent) VALUES ('$dataURL')...

Also make sure session_start(); is loaded, since you are using sessions.

After seeing the comments in your question, then if

UPDATE markers SET PhotosEvent = '$dataUrl' WHERE id = $_SESSION['id']


UPDATE markers SET PhotosEvent = '$dataUrl' WHERE id = '".$_SESSION['id']."'

did not work, then that could be the reason.

Add error reporting to the top of your file(s) which will help during production testing.

ini_set('display_errors', 1);

and mysql_error()

Using those error reporting methods will help find errors found in your code.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.