2012-09-18 5 views
2
그 mysqli를 사용하여 "이미지"테이블에 파일을 삽입 데이터를 업로드 아래 내가 코드를 사용하고

에 삽입되지 않은 :데이터 테이블

<?php 
/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    die(); 
} 

$result = 0; 

//UPLOAD IMAGE FILE 

move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]); 

$result = 1; 

//INSERT INTO IMAGE DATABASE TABLE 

$imagesql = "INSERT INTO Image (ImageFile) VALUES (?)"; 

if (!$insert = $mysqli->prepare($imagesql)) { 
    // Handle errors with prepare operation here 
} 

//Dont pass data directly to bind_param store it in a variable 
$insert->bind_param("s", $img); 

//Assign the variable 
$img = 'ImageFiles/' . $_FILES['fileImage']['name']; 

$insert->execute(); 

$insertimagequestion->execute(); 

//IF ANY ERROR WHILE INSERTING DATA INTO EITHER OF THE TABLES 
if ($insert->errno) { 
    // Handle query error here 
} 

$insert->close(); 


$lastID = $mysqli->insert_id; 

$imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId) 
    VALUES (?, ?, ?)"; 


    if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) { 
     // Handle errors with prepare operation here 
     echo "Prepare statement err"; 
    } 

$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : ''); 

$insertimagequestion->bind_param("isi",$lastID, $sessid, $_POST['numQuestion'][$i]); 

     $insertimagequestion->execute(); 

       if ($insertimagequestion->errno) { 
      // Handle query error here 
     } 

     $insertimagequestion->close(); 

} 
?> 

그래서 예를 들어 나는이 개 이미지를 삽입하는 경우 "cat.png "와"이미지 "데이터베이스 테이블"로 "dog.png,이처럼 삽입합니다 :

ImageId   ImageFile 

    220    cat.png 
    221    dog.png 

(ImageId is an auto increment) 

을 어쨌든 제가하고 싶은 것은 파일이 업로드 될 때뿐만 아니라 데이터가 삽입되어 있다는 것입니다 위의 표를 참조하십시오. 그러나 위에 삽입 된 ImageId를 검색하여 아래의 'Image_Question'표에 배치 할 수 있기를 바랍니다. 그래서 다음과 같을 것입니다 :

ImageId   SessionId  QuestionId 

    220   AAA    1 
    221   AAB    4 

그러나 Image_Question 테이블에는 아무 것도 삽입하지 않습니다. 이미지를 업로드 할 때 "이미지"테이블에 데이터를 삽입 할뿐만 아니라 "Image_Question"테이블에도 데이터를 삽입 할 수 있습니까?

내가 해제 방법이야 경우
+0

오류가 있습니까? '$ insert-> close()'가 아마 유효하지 않은 것 같습니다. –

+0

PHP 오류 보고서에서 오류가 없습니다. – user1681039

답변

1

는 용서하지만, PHP는 즉, 순차적으로 실행되지 않습니다, 해서는 안이 : if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) 이 후 $insertimagequestion->execute(); 이동? $insertimagequestion->execute();의 첫 번째 어쩌면 삽입 ID를 지울 수 있습니다. 나는 &을 인쇄하기 위해 무언가를 넣었습니다. 확실히 insert_id를 얻었습니다.

+0

준비 문 다음에 실행을 넣으려고했으나 변경 사항이 없으므로 코드 배치 방법을 보여줄 수 있습니까? 그러나 만약 당신이 그것을 어떻게 내놓을 지 알 수 있다면 나는 그것을 따라갈 것이고 어떤 일이 일어날지를 볼 것입니다. – user1681039

+0

'$ insert-> execute();'다음에 나타나는'$ insertimagequestion-> execute();'의 첫 번째 인스턴스를 주석 처리하면'$ lastID = $ mysqli-> insert_id;'그리고 삽입 ID를 가져 왔는지 확인하십시오. –

+0

문제는이 스크립트가 백그라운드에서 수행되기 때문에 어쨌든 어떤 인쇄물도 볼 수 없게됩니다. – user1681039