使用 Ajax 将 Canvas 生成的图片保存到服务器,效果非常好,但我还想将图像名称添加到数据库表中。但这似乎不起作用。
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);
}
最佳答案
试试这个:
mysqli_query($con, "INSERT INTO table (imgurl) VALUES ('$img')") or die(mysqli_error($con));
您收到错误输出吗?
关于javascript - 将canvas图片插入MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22436989/