Php 注意:未定义索引:中的post_id
您好,我是一名php初学者,一直坚持到现在。我找不到问题Php 注意:未定义索引:中的post_id,php,Php,您好,我是一名php初学者,一直坚持到现在。我找不到问题 注意:未定义索引:第22行C:\XA\htdocs\Passie Blog\post.php中的post\u id 这是第22行,问题应该是: $id=$\u POST['POST\u id'] 这是我的php代码 <?php if(!isset($_GET['id'])){ header('Location: index.php'); exit(); }else{ $id = $_GET['id']; }
注意:未定义索引:第22行C:\XA\htdocs\Passie Blog\post.php中的post\u id
这是第22行,问题应该是:
$id=$\u POST['POST\u 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";
}
}
?>
未设置变量post\u id(无论如何在post中)。我要换这些
$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'] : '';
这样,如果表单未完全填写,则会有一个回退值。表单中没有名为post\u id eists的字段。但是,您正在表单操作中通过URL手动传递ID。要获取ID,您可以使用$\u get['ID']
而不是$\u POST['POST\u ID']
来获取表单的HTML
?是否有名为POST\u ID
的表单元素?而您可以使用$\u get和$\u POST,也许坚持一个会更好。post\u id
在您的HTML
中没有定义,这样做吧!谢谢,我已经把这个代码写进表格了。
$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'] : '';