Saving Picture to MySQL Database


#1

Can someone please help me. I am trying to save the picture taken by the camera using the example provided by NSBApp Studion examples(Which is just the greatest product!) using the following Function and PHP code but it is just not saving the image or ID to the database The id is an Int and content is LongBlob

Function AddImageToDB_onclick()

Dim postData

postData = postData & "&imgBase64=" & encodeURIComponent(PictureBox3.toDataURL())
req=Ajax("/TestImage.php/?myText=" & postData)

If req.status=200 Then 'success
  MsgBox(req.responseText)
Else
  MsgBox(req.responseText)
End If
End Function
<?php

$base64_data= $_GET['myText'];

$dbhost = 'localhost';		//MySQL database is located on your server
$dbname = 'db';		//the name of your database
$dbuser = 'db';	// your user ID for the database
$dbpass = 'ghhggghhgh$';		// your password for the database

$link = mysqli_connect($dbhost, $dbuser, $dbpass, $db_name);

if (mysqli_connect_errno($link)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$stmt = mysqli_prepare($link, "INSERT INTO test (id, content) VALUES (?, ?)");

$stmt->bind_param('ib', $id, $content);

$id = 1;

$content = null;

$stmt->send_long_data(1,  $base64_data );

mysqli_stmt_execute($stmt);
//printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt));
mysqli_stmt_close($stmt);
?>:

#2

A little easier to read:

$base64_data= $_GET['myText']; 
$dbhost = 'localhost'; //MySQL database is located on your server 
$dbname = 'db'; //the name of your database 
$dbuser = 'db'; // your user ID for the database 
$dbpass = 'ghhggghhgh$'; // your password for the database 

$link = mysqli_connect($dbhost, $dbuser, $dbpass, $db_name); 

if (mysqli_connect_errno($link)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } 
$stmt = mysqli_prepare($link, "INSERT INTO test (id, content) VALUES (?, ?)"); 
$stmt-&gt;bind_param('ib', $id, $content); 
$id = 1; 
$content = null; 
$stmt-&gt;send_long_data(1, $base64_data ); 
mysqli_stmt_execute($stmt); 
//printf("%d Row inserted.\n", mysqli_stmt_affected_rows($stmt)); mysqli_stmt_close($stmt);

#3

I am getting req.Status of 0


#4

0 does not mean there is a problem - I’ve seen that returned on successful operations (as well as 200).

Can you put this statement just before the Ajax call?

console.log(postData)

#5

is this syntax correct

postData = postData & “&imgBase64=” & encodeURIComponent(PictureBox1.toDataURL())


#6

What happened when you tried as I suggested?


#7

Hello.

Perhaps storing the photo on the database is not the most practical idea. Because of the following reasons:

  1. The databases with the hosting are normally limited to 1 gb, but generally gives you more space (50, 80, 160 gb) to store files.

  2. If you store the photo in a table with more comumns than the photo, if for some reason for a record a photo is not added the record size will still be huge since the record separates the blob the size.

3.everytime you need go retrieve the photo you will need to enter to the database AnD that consumes time. Then you should convert the image so it can travel back, and then the client should convert the received data to jpg or png. This add more time to this process.

My recomendation is that you receive the information of the picture, then you generate a .jpg or .png file on the server and in the database only store the name of the file.

Now, whenever you need to retrive the photo you just call the file.