Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未将数据插入数据库_Php_Html_Mysql_Pdo - Fatal编程技术网

Php 未将数据插入数据库

Php 未将数据插入数据库,php,html,mysql,pdo,Php,Html,Mysql,Pdo,嘿,我正试图为一个php项目的特定帖子制作一个评论部分,我正在学习本教程。我找不到任何有用的东西。它所做的只是将我重定向到操作页面,但不提交任何内容 show-post.php: <?php require 'config.php'; include 'templates/header.php'; $id = isset($_GET['id']) ? $_GET['id'] : ''; $query = $pdo->prepare( "SELECT * FROM post

嘿,我正试图为一个php项目的特定帖子制作一个评论部分,我正在学习本教程。我找不到任何有用的东西。它所做的只是将我重定向到操作页面,但不提交任何内容

show-post.php:

   <?php

require 'config.php';
include 'templates/header.php';

$id = isset($_GET['id']) ? $_GET['id'] : '';

$query = $pdo->prepare( "SELECT * FROM posts WHERE id = ?");
$query->execute(array($id));
$data = $query->fetchAll(PDO::FETCH_ASSOC);




?>
<div class="container">
<?php foreach($data as $row) :?>

   <h2><?= $row['title'];?></h2>
   <?= '<img src="/createpost/images/'.$row['image'].'" alt="">' ?>
<?php endforeach ?>


<form  method="POST" id="comment_form" action="add_comment.php" enctype="multipart/form-data">
<input type="hidden" name="id" value="<?php echo $id; ?>">
   <div class="form-group">
       <textarea name="comment" cols="30" rows="10"></textarea>
   </div>
   <div class="form-group">
       <input type="submit" name="submitcomment" id="submit" class="btn-success btn" value="Add Comment">
   </div>
</form>
<span id="comment_message"></span>
<br />
<div class="display_comment"></div>
<a href="/browse.php">Go Back</a>
</div>



 </php include "templates/footer.php"; ?>



我通过删除用户ID来提交它。userid返回null,我将研究原因。另外,我也不知道为什么教程需要用户id,而评论已经获得了登录的用户名。谢谢你们的帮助

请在execute and prepare语句之后尝试$stmt->error或mysqli_error($connection)),并向我们显示提交表单时出现的错误。检查如何在我添加的$stmt->error上获取mysqli错误;在执行之后,我仍然没有得到一个错误语句。。。我在使用pdo,所以mysli函数不起作用吗?注意:第13行C:\Users\Zak\Documents\Code\crudapp\add_comment.php中未定义的索引:id致命错误:未捕获pdo异常:SQLSTATE[23000]:完整性约束冲突:1048列“user\u id”在im获取这些错误时不能为null这意味着user\u id字段有时获取null或空值。。。您可以在发送到MySQL查询之前打印这些值,并检查user\u id变量获取空值的条件
<?php
session_start();
require 'config.php';

include 'templates/header.php';

if(!isset($_SESSION['username'])){
    header("location:login.php");
} else{
    if(isset($_POST['submitcomment'])){
        $userid = $_SESSION['id'];
        $postid = $_POST['id'];
        $username = $_SESSION['username'];
        $comment = $_POST['comment'];

        if($comment != ""){

                    $query = "INSERT INTO comments (user_id, post_id, username, comment) VALUES(:userid, :postid, :username, :comment)";

                $stmt = $pdo->prepare($query);
                $stmt->execute(['user_id' => $userid,'postid' => $postid, 'username'=>$username, 'comment'=>$comment]);
                if($stmt){
                    header("location:show-post?id=".$postid);
                }


        }

    }
}