【发布时间】:2014-11-22 01:18:02
【问题描述】:
我有一个名为“mysqlproject”的数据库,其中包含两个表。我感兴趣的表之一是命名标记,并在数据库中声明如下:
CREATE TABLE IF NOT EXISTS `markers` (
`id` int(11) NOT NULL,
`TitleEvent` varchar(60) NOT NULL,
`Description` varchar(80) NOT NULL,
`lat` float(10,6) NOT NULL,
`lng` float(10,6) NOT NULL,
`type` varchar(255) NOT NULL,
`status` varchar(30) NOT NULL,
`EventUserName` varchar(60) NOT NULL,
`PhotosEvent` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=73 ;
其中一列是PhotosEvent我想在其中保存照片的网址。所以我通过 javascript 文件捕获照片,然后通过 ajax 将其发送到 php 文件“camera.php”
ajax发送过程为:
$.ajax({
type: "POST",
url: "camera.php",
data: {
imgBase64: dataURL
}
}).done(function( respond ) {
// you will get back the temp file name
// or "Unable to save this image."
console.log(respond);
});
所以我使用 POST 方法将图像发送到文件“camera.php”。在 php 文件中,我正在拍摄该图像并将其首先保存在文件夹“EventImages”中的“本地服务器”中。好的,那行得通.. 但我想将图像的 url 保存在“PhotosEvent”字段中的数据库中,仅保存在活动 ID 所在的原始位置..
连接数据库:
$connection = mysql_connect('localhost', 'root', '');
if (!$connection) //Success $Connection with server returns 1
{
die("Database Connection Failed" . mysql_error());
}
else
{
//Connection with server established
}
// Try Connection with mysql Database
$select_db = mysql_select_db('mysqlproject');
if (!$select_db) //Success $Connection with Database returns 1
{
die("Database Selection Failed" . mysql_error());
}
else
{
//Connection with Database established
}
将图像保存为 .png(有效)并尝试将 url 保存到数据库(无效):
if ( isset($_POST["imgBase64"]) && !empty($_POST["imgBase64"]) ) {
define('UPLOAD_DIR', 'EventImages/');
// get the dataURL
$dataURL = $_POST["imgBase64"];
// the dataURL has a prefix (mimetype+datatype)
// that we don't want, so strip that prefix off
$parts = explode(',', $dataURL);
$data = $parts[1];
// Decode base64 data, resulting in an image
$data = base64_decode($data);
// create a temporary unique file name
$file = UPLOAD_DIR . uniqid() . '.png';
// write the file to the upload directory
$success = file_put_contents($file, $data);
// return the temp file name (success)
// or return an error message just to frustrate the user (kidding!)
print $success ? $file : 'Unable to save this image.';
$results = mysql_query("INSERT INTO markers (PhotosEvent) WHERE id ='".$_SESSION['id']."'VALUES('$dataURL')");
}
我的问题是保存图像 url - 数据库路径。我怎样才能做到这一点?提前谢谢..
【问题讨论】:
-
您是要插入行还是更新现有行?
-
更新现有的..
-
更新标记 SET PhotosEvent = '$dataUrl' WHERE id = $_SESSION['id']
-
改了...没炒锅
-
No 不起作用...可能是什么问题?
标签: javascript php mysql ajax database