2013-06-15 3 views
0

이 문제는 며칠 동안 지속되었으므로 문제가있는 곳을 볼 수 없습니다. 나는 여러면에서 함수를 살펴 보았고, 심지어 PHP 사이트를 들여다 보았지만 이것을 풀 수는 없다.PHP가 MySQL에 삽입되지 않았습니다.

$sql = mysqli_query($db_connect, "INSERT INTO forum_post(post_author, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) VALUES('$log_username', '$log_id', now(), 'a', '$forum_section_title', '$forum_section_id', '$post_title', '$post_body')"); 
    if(mysqli_error($db_connect)) { 
     $message = mysqli_error($db_connect); 
     header("location: ../message.php?msg=$message");  
    } 
    $this_id = mysqli_insert_id($db_connect); 
    header("location: ../view_thread.php?id=$this_id"); 
    exit(); 
:

그래서 내가 (내가 메시지를 볼 수 있습니다 오히려 그래서) 처리 오류를 처리하는 방법을 보여줄 수 ...

$sql = mysqli_query($db_connect, "INSERT INTO forum_post(post_author, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) VALUES($log_username, $log_id, now(), 'a', '$forum_section_title', '$forum_section_id', '$post_title', '$post_body')"); 
    $this_id = mysqli_insert_id($db_connect); 
    header("location: ../view_thread.php?id=$this_id"); 

편집 : 이것은 내가하는 데 문제가 있어요 코드입니다

+0

당신이 지금까지 시도 무엇입니까? mysqli_error ($ db_connect);를 사용하고 리디렉션을 주석 처리하는 것이 가장 먼저해야 할 일입니다. – jeroen

+0

항상 $ mysqli-> error'를 디버깅 용으로 사용하면 오류로 안내 할 수 있습니다. [링크] http://www.php.net/manual/en/mysqli.error.php [링크] – amigura

+0

지금 mysqli_error를 사용하고 있습니다. 이것을 알아낼 수 있는지 확인하겠습니다. –

답변

0

더 안전하고 간단합니다. PDO를 사용하는 것이 좋습니다. 왜냐하면 당신은 실수를하거나 문제를 일으키지 않기 때문입니다.

다음은 PDO의 코드입니다. 이 테스트 : 당신이 오류가 발생해야하기 때문에 당신이 mysqli_error($db_connect);을 사용하는 방법

try { 
    // Prepare our connection string 
    $con = new PDO("mysql:host=".HOST.";dbname=".DBNAME, USER, PASS); 
} 
catch(PDOException $e) { 
    // Connection error jumps here 
    echo $e->getMessage(); 
} 
// Prepare our Insert Query 
$query = $con->prepare("INSERT INTO forum_post(post_author, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) VALUES(:post_author, :post_author_id, :date_time, :type, :section_title, :section_id, :thread_title, :post_body)"); 
// Get our data aligned in an array. 
$data = array('post_author' => $log_username, 'post_author_id' => $log_id, 'date_time' => now(), 'type' => 'a', 'section_title' => $forum_section_title, 'section_id' => $forum_section_id, 'thread_title' => $post_title, 'post_body' => $post_body); 
try { 
    // Try to execute our Insert Query with the given Data 
    $query->execute($data); 
} 
catch(PDOException $e) { 
    // Insert error jumps here 
    echo $e->getMessage(); 
} 
$this_id = $con->lastInsertId(); 
header("location: ../view_thread.php?id=$this_id"); 

하는 호스트, 데이터베이스, 사용자를 대체하고 코드에서 언급 한 자리에 통과해야합니다 ...

+0

나는이 코드에도 여전히 몇 가지 이슈가있다 .. 이것은 매우 이상하다. –

0

나는 확실하지 않다 할 때.

하나의 문제는 당신이 당신의 문자열 값을 인용하지 않는 것입니다 :

$sql = mysqli_query($db_connect, "INSERT INTO forum_post(post_author, post_author_id, date_time, type, section_title, section_id, thread_title, post_body) 
     VALUES($log_username, $log_id, now(), 'a', '$forum_section_title', '$forum_section_id', '$post_title', '$post_body')"); 
//    ^^^^^^^^^^^^^ here  ^^^^^ and here 
//    (assuming $log_id is an integer...) 

하지만 난 당신이 (뿐만 아니라 mysqli에있을 수 있습니다) 준비된 문을 사용한다 @MiroMarkarian에 동의합니다.

+0

좋은 지적을하고, 나는 그 이유를 잊어 버렸고, mysqli_error()는 그렇지 않았다. 그것을 잡으십시오 ... 나는 그것을 지금 파악하는 것을 시도하고있다 –

+0

당신은 $ log_id와 $ log_username에 관하여 맞았다. 나는 ''을 추가했지만 충분하지 않고 심지어 ""또는 die (mysqli_error ($ db_connect))를 추가했으나 결국에는 오류가 발생하지 않습니다 ... –

+0

@EltonRodriguez 동안 리디렉션하지 마십시오 디버깅. – jeroen

0

PHP 설치가 오류를보고하도록 설정되어 있는지 확인하십시오. 그런 다음 실패한 MySQL 쿼리 실행에서 죽고 얻은 오류를 검사하십시오.

는 오류보고를 사용하여 PHP 파일의 맨 위에 다음 줄을 넣어 :

<?php 
error_reporting(E_ALL^E_NOTICE); 
관련 문제