2011-03-16 145 views
1

錯誤發生在第65行:}趕上(例外$ E){解析錯誤:語法錯誤,意外的T_CATCH在...行65上?

<?php 

require "includes/connect.php"; 



function generateCode($length = 5) 
{ 
    $characters = 'bcdfghjkmnpqrstvwxyz'; 
    $string = ''; 

    for ($i = 0; $i < $length; $i++) { 
     $string .= $characters[rand(0, strlen($characters) - 1)]; 
    } 
    return $string; 
} 


$msg = ''; 


if($_POST['email']) { 

    // Requested with AJAX: 
    $ajax = ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); 


    try { 

     if(!filter_input(INPUT_POST,'email',FILTER_VALIDATE_EMAIL)){ 
      throw new Exception('Invalid Email!'); 
     } 

     if($ajax){ 
      die('{"status":1}'); 
     } 

     $unique_code = ""; 
     $inserted = false; 

     // Keep looping until we've inserted a record 
     while(!$inserted) { 

      // Generate a code 
      $unique_code = generateCode(); 

      // Check if it exists 
      if ($result = $mysqli->query("SELECT unique_code FROM coming_soon_emails WHERE unique_code = '$unique_code'")) { 

      // Check no record exists 
      if ($result->num_rows == 0) { 

       // Create new record 
       $mysqli->query("INSERT INTO coming_soon_emails (email,unique_code) VALUES ('" . $mysqli->real_escape_string($_POST['email']) . "','$unique_code')"); 

       // Set inserted to true to ext loop 
       $inserted = true; 

       // Close the result object 
       $result->close(); 
      } 
     } 

    } catch (Exception $e){ 

     if($ajax){ 
      die(json_encode(array('error'=>$e->getMessage()))); 
     } 

     $msg = $e->getMessage(); 
     die($msg); 
    }  

} else { 
    // Quit if we can't check the database 
    die('Something went wrong with select'); 
} 



?> 

回答

3

你的括號不匹配。 您的while循環在結尾處沒有正確的匹配大括號(在兩個if語句之後)......只需在catch行之前添加一個}即可。

你應該得到類似Notepad ++的東西來寫PHP!

+0

@Kevin Hikaru Evans:謝謝!現在它出錯了:select出錯了。任何想法爲什麼?直到現在它一直連接起來。 //退出,如果我們無法檢查數據庫死('選擇出了問題'); – jeremycollins 2011-03-16 01:51:28

+0

嘗試做'死(「出錯了:」。$ mysqli-> error);'。 – 2011-03-16 01:53:17

+0

@Kevin Hikaru Evans:它只是輸出:「出錯了:」 – jeremycollins 2011-03-16 01:57:00