0

I'm pretty new to web development, and I'm trying to save a base64 encoded image on my sql database. The sql statement is working fine as I'm able to place user registration data into the database through PHP, but I'm running into trouble using XMLhttprequest to send the image to PHP so that I can use the $_GET method to receive the data. JQuery is strictly forbidden, so I'm stuck pretty bad. The image variable has the data in, and I did check to see that it's saved correctly. This is how I'm sending the XML request:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
    if (this.readyState == 4 && this.status == 200)
    {
        alert(this.responseText);
    }
}
xhttp.open("POST", "saveimage.php?img="+image, true);
xhttp.send();

The image variable contains the data. This code runs as well, since I placed alerts in between to make sure it does. Onto the saveimage.php file then:

<?php

session_start();

$email = $_SESSION['email'];
$image = $_GET['img'];
$conn = new PDO("mysql:host=localhost; dbname=camagru", "root", "admin");
$conn->setAttribute(PDO::ERRMODE_EXCEPTION);
$sql = $conn->prepare("INSERT INTO `images` (`image`, `email`) VALUES (:image, :email");
$sql->bindParam(':image', $image);
$sql->bindParam(':email', $email);
$sql->execute();

?>

Sadly from here, nothing happens. My database remains empty, no matter what. Am I missing something? It doesn't look like the PHP script is running at all.

NodziGames
  • 325
  • 3
  • 13

2 Answers2

1

I imagine the base64 encoded image string is too long for a get request - limited to something like 2083 characters on IE for example.

Have you tried POST?

You could check the web server logs to see if if does receive the whole string.

Heres Some Updated Code:

xhttp.setRequestHeader("Content-type" , "application/x-www-form-urlencoded");
xhttp.send("img=" + image);

See this answer to see how to POST the data

Community
  • 1
  • 1
hairmot
  • 2,844
  • 1
  • 10
  • 23
1

You are sending the Request via Post but yet you are checking the $_GET request.

set the params like this

var params =  "img"+image;

and your call like this

xhttp.open("POST", "saveimage.php, true);
xhttp.send(param);

and on your server response script

<?php 
session_start();

if($_SERVER['REQUEST_METHOD'] == "POST" && (isset($_FILES['img']['name']))
{

$email = $_SESSION['email'];
$image = $_POST['img'];
$conn = new PDO("mysql:host=localhost; dbname=camagru", "root", "admin");
$conn->setAttribute(PDO::ERRMODE_EXCEPTION);
$sql = $conn->prepare("INSERT INTO `images` (`image`, `email`) VALUES (:image, :email");
$sql->bindParam(':image', $image);
$sql->bindParam(':email', $email);
$sql->execute();


}

?>

Are you trying to upload a file or just passing the name of a file threw ajax? If you are uploading a file remember you have to check if file exisist on the server before you record it to the database. If you are not uploading you don't need that (isset).

Sal Orozco
  • 45
  • 1
  • 7
  • I'm trying to pass the base64 encoded file onto the server to store it on a table. Am I being inneficient? – NodziGames Oct 18 '16 at 08:39
  • Ok so you cant send it threw get. Is how i modified your code to send it threw POST. I WOULD HAVE TO SEE YOUR FORM OR WHERE YOU ARE GETTING YOUR FILE FROM. – Sal Orozco Oct 18 '16 at 08:42