2014-09-26 3 views
1

두 개의 테이블이 포함 된 'mysqlproject'라는 데이터베이스가 있습니다. 나는이 같은 데이터베이스에 마커 이름과 선언에 흥미 오전 테이블 중 하나 열이미지 URL을 MySQL 데이터베이스에 저장

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 ; 

하나는 거기에 내가 사진의 URL을 저장하려는 PhotosEvent입니다. 그래서 난 자바 스크립트 파일을 통해 사진을 캡처하고 난 PHP 파일 "camera.php"

아약스 보내는 절차는에 아약스를 통해 보낸다 :

$.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"폴더의 "로컬 서버"에 먼저 저장합니다.

$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 
} 

: 좋아

은 데이터베이스와 연결 .. 그 작품 .. 하지만 난 분야는 활성 ID가있는 위치를 원료에서 "PhotosEvent"에서 데이터베이스에 이미지의 URL을 저장하려면 .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을 저장 - 경로를 데이터베이스에. 내가 어떻게 할 수 있니? 미리 감사드립니다.

+0

행을 삽입하거나 기존 행을 업데이트하려고합니까? –

+0

기존 업데이트 중 .. – monakons

+0

UPDATE 마커 SET PhotosEvent = '$ dataUrl'WHERE id = $ _SESSION [ 'id'] –

답변

0

INSERT를 수행하려면 VALUES의 위치가 잘못되었습니다.

을 수행하십시오 세션을 사용하고 있기 때문에

$results = mysql_query("INSERT INTO markers (PhotosEvent) VALUES ('$dataURL')... 

또한, session_start(); 확인이로드되어 있는지 확인합니다.

UPDATE markers SET PhotosEvent = '$dataUrl' WHERE id = $_SESSION['id'] 

또는

이 가
UPDATE markers SET PhotosEvent = '$dataUrl' WHERE id = '".$_SESSION['id']."' 

이 작동하지 않은 경우 귀하의 질문에 의견을보고 한 후

후, 그 다음은 이유가 될 수 있습니다.

생산 테스트 중에 도움이되는 파일 상단에 오류보고를 추가하십시오.

error_reporting(E_ALL); 
ini_set('display_errors', 1); 

mysql_error() 코드에서 발견 된 오류를 찾는 데 도움이됩니다 이러한 오류보고 방법을 사용.

+0

그것은 작동하지 않습니다 .. 나는 많은 compries을 tryed .. 새로운 ID를 생성하고 그 모든 데이터에 삽입 코드의 유일한 라인 :'$ imageContentsEscaped = mysql_real_escape_string ($ success, $ connection);''mysql_query ("INSERT INTO 마커 (PhotosEvent) VALUES ('$ imageContentsEscaped') ", $ connection);하지만 그 값을 삽입합니다 : 161078. 나는 그 가치가 정확히 무엇인지 모른다. "UPDATE markers SET"을 사용하여이 코드 줄을 시도하면 엉망이 되어도 작동하지 않습니다! – monakons

+0

@monakons 질의 직후에'var_dump ($ results);'를 사용하면 그 결과는 무엇입니까? –

+0

@monakons 호기심. 양식에'enctype = "multipart/form-data"'가 포함되어 있습니까? –

1

발견!

define('UPLOAD_DIR', 'EventImages/'); 
$img = $_POST['imgBase64']; 
$img = str_replace('data:image/png;base64,', '', $img); 
$img = str_replace(' ', '+', $img); 


$data = base64_decode($img); 
$file = UPLOAD_DIR . uniqid() . '.png'; 
$success = file_put_contents($file, $data); 
print $success ? $file : 'Unable to save the file.'; 


$result=mysql_query("SELECT id FROM markers ORDER BY id DESC LIMIT 1"); 
while ($row = mysql_fetch_array($result)){ 
$LastInsertID=$row['id']; 
} 
$results = mysql_query("UPDATE markers SET PhotosEvent='".$file."' WHERE id = '".$LastInsertID."'");  

지금이 작품을 좋아 .. 나는 테이블 마커에 마지막으로 알려진 ID를 fetcing와 나는 데이터베이스에 변수 $ 파일에 저장되어있는 이미지의 URL을 저장! 나는 또한 전에 EventImages 폴더에 이미지를 저장 !!

관련 문제