0
我有問題將喜歡/不喜歡的值存儲到我的數據庫中,我需要每個帖子都有自己的喜歡/不喜歡的列存儲在其行中,並且當有人喜歡帖子時,就像後沒什麼其他的,我遇到的問題是它喜歡數據庫中的第一個條目,有人可以提供指導嗎?更新不是特定於某一行
如果它有助於繼承人的數據庫
$likes = (empty($_POST['like'])) ? : $_POST['like'] ;
$dislikes = (empty($_POST['dislike'])) ? : $_POST['dislike'] ;
$ip = $_SERVER['REMOTE_ADDR'];
if(isset($_POST['like'])){
$likes1 = $likes+1;
$voted1 = $voted+1;
$query2 = $db->prepare("INSERT INTO data (voted, ip) VALUES (:voted, :ip)");
$query2->bindParam(':voted', $voted1, PDO::PARAM_STR);
$query2->bindParam(':ip', $ip, PDO::PARAM_STR);
$query2->bindParam(':pid', $pid, PDO::PARAM_STR);
$query2->execute();
$update1 = $db->prepare("UPDATE data SET likes = :likes");
$update1->bindParam(':likes', $likes1, PDO::PARAM_STR);
$update1->bindParam(':quote', $actual_quote, PDO::PARAM_STR);
$update1->execute();
header("Location: inspirational.php?");
}
if(isset($_POST['dislike'])){
$dislikes1 = $dislikes+1;
$voted1 = $voted+1;
$query2 = $db->prepare("INSERT INTO data (voted, ip) VALUES (:voted, :ip)");
$query2->bindParam(':voted', $voted1, PDO::PARAM_STR);
$query2->bindParam(':ip', $ip, PDO::PARAM_STR);
$query2->execute();
$update1 = $db->prepare("UPDATE data SET dislikes = :dislikes");
$update1->bindParam(':dislikes', $dislikes1, PDO::PARAM_STR);
$update1->bindParam(':quote', $actual_quote, PDO::PARAM_STR);
$update1->execute();
header("Location: inspirational.php?");
}
$stmt = $db->query("SELECT * FROM data");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$row3 = $stmt->fetch();
$query = $db->prepare("SELECT * FROM data WHERE formtype = 'Inspirational' ");
$query->execute();
$row = $query->fetch(PDO::FETCH_ASSOC);
//delete this-> $info = mysql_fetch_array($query1); ?
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<div class="wrapper">
<div class="submissions">
<div class="logo-logo"><h2>Questions.</h2>
<div class="checkboxes"><?php echo htmlentities($row["formtype"]); ?></div>
</div>
<div class="top-submit">
“<?php echo htmlentities($row["actual_quote"]); ?>”
</div>
<div class="poster">- <?php echo htmlentities($row["poster"]);
if(isset($row3["voted"]) && isset($row3["ip"]))
{
echo "You have already voted for this.";
}
else
{
?>
<form action="" method="post">
<input type="submit" name="dislike" value="dislike" />
<input type="submit" name="like" value="like" />
</form> <?php
}
?>
聽說過ORM?喜歡雄辯嗎?這使得這個很容易編寫,使用和維護。只是在說' – MightyPork