使用Ajax将画布生成的图片保存到服务器,这非常有效,但我还想将图像名称添加到数据库表中。这似乎不起作用。
使用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);
}
答案 0 :(得分:2)
试试这个:
mysqli_query($con, "INSERT INTO table (imgurl) VALUES ('$img')") or die(mysqli_error($con));
您收到错误输出吗?
答案 1 :(得分:0)
imgurl
,而是保留名称key
。