2013-07-25 40 views
-1

你好,我是一個初學者在PHP和卡住在這一刻。我找不到問題。注意:未定義的指數:post_id在

Notice: Undefined index: post_id in C:\XA\htdocs\Passie Blog\post.php on line 22

這是22行那裏的問題應該是:

$id = $_POST['post_id'];

這是我的PHP代碼

<?php 
if(!isset($_GET['id'])){ 
    header('Location: index.php'); 
    exit(); 
}else{ 
    $id = $_GET['id']; 
} 
include('includes/db_connect.php'); 
if(!is_numeric($id)){ 
    header('Location: index.php'); 
} 
$sql = "SELECT title, body FROM posts WHERE post_id='$id'"; 
$query = $db->query($sql); 
if($query->num_rows !=1){ 
    header('Location: index.php'); 
    exit(); 
} 
if(isset($_POST['submit'])){ 
    $email = $_POST['email']; 
    $name = $_POST['name']; 
    $comment = $_POST['comment']; 
    $id = $_POST['post_id']; 
    if($email && $name && $comment){ 
     // 
     $email = $db->real_escape_string($email); 
     $name = $db->real_escape_string($name); 
     $id = $db->real_escape_string($id); 
     $comment = $db->real_escape_string($comment); 
     if($addComment = $db->prepare("INSERT INTO comments(name, post_id, email_add, comment) VALUES (?,?,?,?)")){ 
      $addComment->bind_param('ssss', $id, $name, $email, $comment); 
      $addComment->execute(); 
      echo "Bedankt! uw bericht is toegevoegd"; 
      $addComment->close(); 

     } else{ 
      echo "Error"; 
     } 
    } else{ 
     echo "ERROR"; 
    } 
} 
?> 

,這是我的網頁

其餘
<div id="container"> 
    <div id="post"> 
     <?php 
      $row = $query->fetch_object(); 
      echo "<h2>".$row->title."</h1>"; 
      echo "<p>".$row->body."</p>"; 
     ?> 
    </div> 
    <hr /> 
    <div id="add-comments"> 
     <form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"?>" method="post"> 
      <div> 
       <label>Email Adres</label><input type="text" name="email" /> 
      </div> 
      <div> 
       <label>Naam</label><input type="text" name="name" /> 
      </div> 
      <div> 
       <label>Commentaar</label><textarea name="comment"></textarea> 
      </div> 
      <input type="hidden" name="post_id" value="<?php echo $id?>" /> 
      <input type="submit" name="submit" value="Toevoegen"/> 
     </form> 
     </div> 
     <hr /> 
     <div id="comments"> 
     <?php 
      $query = $db->query("SELECT * FROM comments WHERE post_id='$id' ORDER BY comment_id DESC"); 
      while($row = $query->fetch_object()): 
     ?> 
      <div> 
       <h5><?php echo $row->name?></h5> 
       <blockquote><?php echo $row->comment?></blockquote> 
      </div> 
     <?php endwhile;?> 
     </div> 
</div> 
+1

您可以發佈表單的HTML嗎? –

+0

你有一個名爲「post_id」的表單元素嗎? – Sean

+0

雖然你可以使用$ _GET和$ _POST,也許堅持一個會更好 – 2013-07-25 23:29:30

回答

0

變量post_id未設置(無論如何都是後置)。我會改變這些

$email = $_POST['email']; 
$name = $_POST['name']; 
$comment = $_POST['comment']; 
$id = $_POST['post_id']; 

喜歡的東西

$email = !empty($_POST['email']) ? $_POST['email'] : ''; 
$name = !empty($_POST['name']) ? $_POST['name'] : ''; 
$comment = !empty($_POST['comment']) ? $_POST['comment'] : ''; 
$id = !empty($_POST['post_id']) ? $_POST['post_id'] : ''; 

這種方式,你有一個回退值,如果表格沒有填寫完整。

+0

完成這個! thx爲此,我已將此代碼放置到窗體中。 」/>這有助於解決問題,但知道我沒有看到網頁上的任何評論有任何想法? – user2619538

1

沒有名稱爲post_id的字段出現在您的表單中。但是,您需要通過表單操作中的網址手動傳遞ID。要獲得ID,您可以使用$_GET['id']而不是$_POST['post_id']