2013-04-17 43 views
-1

我做錯了什麼?我知道一個事實與準備好的陳述有關。謝謝!我的pdo預備聲明有什麼問題?

編輯:這裏是腳本的所有代碼,所以每個人都可以看到發生了什麼。我現在不得不添加更多的單詞b/c表單不喜歡我添加多少代碼。

<? if($_SERVER['REQUEST_METHOD'] == "POST"){ 
    include_once("includes/connect.php");  

    session_start(); 

    $error = array(); 
    $success = "The artist has been added!"; 

    if(!empty($_POST['artist-name'])){ 
     $artistName = $_POST['artist-name']; 
    } else{ 
     $error[] = "Please enter a valid artist name!"; 
    } 

    if(empty($error)){   

     //prepared statement goes here "INSERT INTO artist VALUES(?)" 
     try{ 
      $pdo = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password); 
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
      $stmt = $pdo->prepare("INSERT INTO artist(name) VALUES(:artist)"); 
      $stmt->execute(array("artist" => $artistName)); 
      while($row = $stmt->fetch()) { 
       print_r($row); 
      } 
     } catch(PDOException $e){ 
         echo $e->getMessage(); 
     } 

     //$_SESSION["success"] = $success; 

    } else { 
     $_SESSION["error"] = $error; 
    } 

    header("Location: ../add-artist.php"); 
    exit();        

} 
?> 
+1

您是否收到任何錯誤消息? –

+0

發生了什麼事情或沒有發生? *「這是一些代碼,告訴我它是否有效,爲什麼不」*很難回答。 – deceze

+0

只是500內部服務器錯誤消息。我加了error_reporting(E_ALL); ini_set('display_errors','On');在一點上,但它沒有擺脫500條消息 – heyjohnmurray

回答

1
$stmt = $pdo->prepare("INSERT INTO artist VALUES(:artist)"); 

的參數稱爲:artist

$stmt->execute(array('artist' => $artistName)); 

你叫它artist。冒號也應該在那裏,即使它是內部添加的。

關於錯誤,在這種情況下唯一不會被異常捕獲的錯誤是致命的錯誤。在php.ini中有error_reporting = -1display_errors = On應該使錯誤可見。否則,請使用error_log .ini設置。

+2

它應該沒有冒號。 – pfyod

+0

@傑克,謝謝,但我試過,並沒有解決它。我仍然有錯誤。 – heyjohnmurray

+0

@heyjohnmurray什麼是*錯誤? –