Hallo,
Unter jedem Post-Eintrag kann man Kommentare schreiben. In der Datenbank soll die k_id(Kommentar-ID), p_id(Post-Id), u_id(User-ID), Comment, Datum eingetragen werden.
Jeder Post hat eine p_id. Wenn ein Nutzer ein Kommentar schreibt dann muss man auch wissen unter welchen POST. Jetzt wollte ich die p_id in die Datenbank speichern sobald der Nutzer ein Kommentar eingibt jedoch bekomm ich diese Fehlermeldung: Array ( [0] => 23000 [1] => 1048 [2] => Column 'p_id' cannot be null ) .
Bei paar Post-Einträge klappt es und es steht (string(1) "6") jedoch wird das Kommentar nicht gespeichert obwohl erfolg steht. Bei manchen steht das da oben mit Column p_id cannot be Null.
Bin total verwirrt. Den Code werde ich aufjedenfall noch sauberer schreiben jedoch stört mich grad das Problem mit dem Kommentar.
Hier ein Bild von der Situation:
_______________________
Bild von der DB:
__________________
Code:
<?php
if($stmt = $pdo->prepare("SELECT id, username, image, im_type, image_ordner,
u_post.p_id, u_post.u_id, u_post.p_like, u_post.p_heart, u_post.post, u_post.date,
comments.p_id, comments.u_id, comments.comment, comments.date_comment
FROM user JOIN u_post ON u_post.u_id = user.id LEFT JOIN comments ON comments.p_id = u_post.p_id ORDER BY date DESC")) {
if(!$stmt->execute()) {
print_r($stmt->errorInfo());
}
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
?>
<section class="user_post_top_left">
<ul>
<li class="imgbox"><?php echo '<img src="data:image/' . $row['im_type'] . ';base64,' . base64_encode($row['image']) . '"/>'; ?></li>
<li><h2><?php echo $row['username']; ?></h2></li>
</ul>
</section>
<section class="user_post_time">
<p><time><small><?php echo $row['date']; ?></small></time></p>
</section>
</section>
<section class="user_post_index">
<p><?php echo $row['post']; ?></p>
<p><?php echo '<img src="'.$row['image_ordner'].'">'; ?></p>
<p>
<p><?php echo $row['comment']; ?>
<span class="user_set_value_right">
<button type="button" class="like_set"><i class="fa fa-thumbs-o-up" aria-hidden="true"></i></button><small><a href="#"><?php echo $row['p_like']; ?></a></small> <button type="button" class="heart_set"><i class="fa fa-heart-o" aria-hidden="true"></i></button><small><a href="#"><?php echo $row['p_heart']; ?></a></small>
</span>
<span class="user_set_value_left">
<form action="" method="POST">
<?php
if(isset($_POST['show'])) {
$comment = $_POST['comment_msg'];
if($insert = $pdo->prepare("INSERT INTO comments (p_id, u_id, comment) VALUES (?, ?, ?)")) {
$insert->BindValue(1, $row['p_id']);
$insert->BindValue(2, $_SESSION['id']);
$insert->BindValue(3, $comment);
if(!$insert->execute()) {
print_r($insert->errorInfo());
} else {
echo 'erfolg';
}
}
}
?>
<input type="text" name="comment_msg">
<input type="submit" name="show" value="Show">
</span>
</section>
<?php
}
}
?>
Alles anzeigen