2015-04-27 33 views
-1

非常感謝您花時間查看我的問題。 我有下面的代碼用於更新準備好的聲明表單,並通過驗證&敏化。當我嘗試沒有驗證&敏感性的代碼時,更新表單完美地工作,但是當我添加我的filter-has-var時,我的代碼不起作用並給我一個錯誤。我知道問題是與我應該把我的驗證條件的地方相關,但我不知道在哪裏以及如何解決它。 請幫忙!更新準備好的聲明表格並進行驗證

<?php 
$missing = null; 
$errors = null; 
require_once('../includes/connection.inc.php'); 
// initialize flags 
$OK = false; 
$done = false; 
// create database connection 
$conn = dbConnect('write'); 
// initialize statement 
$stmt = $conn->stmt_init(); 
// get details of selected record 
if (isset($_GET['article_id']) && !$_POST) { 
    // prepare SQL query 
    $sql = 'SELECT article_id, title, article 
    FROM blog WHERE article_id = ?'; 
    if ($stmt->prepare($sql)) { 
     // bind the query parameter 
     $stmt->bind_param('i', $_GET['article_id']); 
     // bind the results to variables 
     $stmt->bind_result($article_id, $title, $article); 
     // execute the query, and fetch the result 
     $OK = $stmt->execute(); 
     $stmt->fetch(); 
    } 
} 

// if form has been submitted, update record 
if (filter_has_var(INPUT_POST, 'update')) { 
    try { 
     require_once './Validator.php'; 
     $required = array('title', 'comment'); 
     $val = new Pos_Validator($required); 
     $val->checkTextLength('title', 3); 
     $val->removeTags('title'); 
     $val->checkTextLength('article', 0, 150); 
     $val->useEntities('article'); 
     $filtered = $val->validateInput(); 
     $missing = $val->getMissing(); 
     $errors = $val->getErrors(); 

     if (!$errors) { 
      // prepare update query 
      $sql = 'UPDATE blog SET title = ?, article = ? 
      WHERE article_id = ?'; 
      if ($stmt->prepare($sql)) { 
       $stmt->bind_param('ssi', $_POST['title'], $_POST['article'], 
        $_POST['article_id']); 
       $done = $stmt->execute(); 
      } 
     } 
     // redirect if $_GET['article_id'] not defined 
     if ($done || !isset($_GET['article_id'])) { 
      header('Location: http://localhost/phpsols/admin/blog_list_mysqli.php'); 
      exit; 
     } 
     // store error message if query fails 
     if (isset($stmt) && !$OK && !$done) { 
      $error = $stmt->error; 
     } 

    } catch (Exception $e) { 
     echo $e; 
    } 
} 

?> 
<!DOCTYPE HTML> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Update Blog Entry</title> 
    <link href="../styles/admin.css" rel="stylesheet" type="text/css"> 
</head> 

<body> 
    <h1>Update Blog Entry</h1> 
    <p><a href="blog_list_mysqli.php">List all entries </a></p> 
    <?php 
    if (isset($error)) { 
     echo "<p class='warning'>Error: $error</p>"; 
    } 
    if($article_id == 0) { ?> 
    <p class="warning">Invalid request: record does not exist.</p> 
    <?php } else { ?> 
    <form id="form1" method="post" action=""> 
     <p> 
      <label for="title">Title:</label> 
      <input name="title" type="text" class="widebox" id="title" 
      value="<?php echo htmlentities($title, ENT_COMPAT, 'utf-8'); ?>"> 
     </p> 
     <p> 
      <label for="article">Article:</label> 
      <textarea name="article" cols="60" rows="8" class="widebox" 
      id="article"><?php echo htmlentities($article, 
      ENT_COMPAT, 'utf-8');?></textarea> 
     </p> 
     <p> 
      <input type="submit" name="update" value="Update Entry" id="update"> 
      <input name="article_id" type="hidden" value="<?php echo $article_id;  
      ?>"> 
     </p> 
    </form> 
    <?php } ?> 
</body> 
</html> 
+1

'並給我一個錯誤'。那麼你打算分享這個錯誤信息,還是你期望我們猜測? – Sean

回答

0

不幸的是,你的代碼是不是很可讀 - 但是從我所看到的,你有一個}太多的catch面前:

} 
} catch (Exception $e) { 
    echo $e; 
} 
} 

也許應該

} catch (Exception $e) { 
     echo $e; 
    } 
} 
+0

謝謝你的回覆。你是對的,我已經修好了,但它仍然給我一個錯誤。 – aydi

+0

哪個錯誤?你能否用錯誤信息更新你的問題? – jeromegamez

+0

它在每個表格窗體上給我出錯,並且不更新數據庫:注意:未定義變量:標題或通知:未定義變量:文章 aydi