Reputation: 402
Using Ajax to save a canvas generated picture to the server, which works perfectly well, but I also want to add the image name to a database table. This doesn't seem to work though.
Javascript:
var ajax = new XMLHttpRequest();
ajax.open("POST",'save.php',false);
ajax.setRequestHeader('Content-Type', 'application/upload');
ajax.send(imgurl);
PHP:
if (isset($GLOBALS["HTTP_RAW_POST_DATA"]))
{
// Get the data
$imageData=$GLOBALS['HTTP_RAW_POST_DATA'];
$filteredData=substr($imageData, strpos($imageData, ",")+1);
$unencodedData=base64_decode($filteredData);
$random_digit=md5(uniqid(mt_rand(), true));
$img = "img_" . $random_digit . ".png";
$fp = fopen( 'gallery/' . $img, 'wb' );
fwrite( $fp, $unencodedData);
fclose( $fp );
$con=mysqli_connect("localhost","root","","database");
mysqli_query($con,"INSERT INTO table (imgurl) VALUES ('$img')");
mysqli_close($con);
}
Upvotes: 0
Views: 923
Reputation: 402
I figured it out, was a stupid mistake. On the real database I did not use the column_name imgurl
, but the reserved name key
.
Upvotes: 0
Reputation: 1098
Try this:
mysqli_query($con, "INSERT INTO table (imgurl) VALUES ('$img')") or die(mysqli_error($con));
Do you receive an error output?
Upvotes: 2