我試圖使圖像上傳網站,用戶可以發佈他們的圖像和其他登錄用戶可以回覆他們。我試圖使用php,mysql和ajax。我實際上是遵循一個教程,並且修改了他們的代碼以適合我的網站。但每次我點擊提交,網頁似乎刷新,因爲我被重定向到我的網站的頂部,新的評論不會貼在舊評論的頂部,因爲我希望它,也沒有插入的信息數據庫中commentstable中的新評論。我想使圖像上傳網站使用PHP,MySQL和AJAX ..但我一直沒有得到任何輸出
下面是代碼:
這是在其中予顯示圖像和打印出的評論和由用戶設置一個textarea輸入新的註釋的PHP文件。在這個文件中,我導入一個JavaScript文件,其中包含一個名爲postcomment()的函數,用於執行ajax部分。評論表有一個名爲commentForImageId的列,存儲發佈特定評論的圖片的ID。
<?php
//image is displayed above this set of code with all the required data
date_default_timezone_set('Asia/Kolkata');//to set my country's timezone
?>
<form method="post" action="" onsubmit="return postcomment();">
<input type="hidden" id="imageId" value="<?php echo $imageId; ?>">
<input type="hidden" id="datetime" value="<?php echo date('Y-m-d H:i:s'); ?>">
<textarea id="comment" placeholder="Write comment"></textarea><br>
<button type="submit">post comment</button>
</form>
<div id="allcomments">
<?php
$sql= "SELECT * FROM commentstable ORDER BY datetime DESC";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($result))
{
$commentForImageId=$row['commentForImageId'];
if($commentForImageId==$imageId){
$username=$row['commentByUserName'];
$comment=$row['comment'];
$datetime=$row['datetime'];
?>
<hr>
<div class="comment_div">
<p class="comment"><?php echo $comment; ?></p>
<p class="username">Posted By:<?php echo $username; ?></p>
<p class="datetime"><?php echo $datetime; ?></p>
</div>
<hr>
<?php
}}?>
</div>
這是執行ajax部分的函數。 「commentsystem.php」執行將數據存儲在數據庫中的部分:
function postcomment(){
var comment = document.getElementById("comment").value;
var datetime = document.getElementById("datetime").value;
if(comment && datetime)
{
$.ajax
({
type: 'POST',
url: 'commentsystem.php',
data:
{
comment:comment,
datetime:datetime
},
success: function (response)
{
document.getElementById("allcomments").innerHTML=response+document.getElementById("allcomments").innerHTML;
document.getElementById("comment").value="";
}
});
}
return false;
}
這是commentsystem.php。在這裏,「dbh.php」是建立到數據庫的連接的數據庫處理文件:
<?php
session_start();
include 'dbh.php';
if(isset($_SESSION['id'])){//if user has logged in
if(isset($_POST['comment']) && isset($_POST['datetime']) && isset($_POST['imageId']))
{
//if user has submitted the comment
$comment=$_POST['comment'];
$datetime=$_POST['datetime'];
$imageId=$_POST['imageId'];
$username=$_SESSION['username'];
$userId=$_SESSION['id'];
$sql="INSERT INTO commentstable (commentForImageId, commentByUserId, commentByUserName, likes, numberOfReplies, comment, datetime) VALUES ('$imageId', '$userId', '$username', 0, 0, '$comment', '$datetime')";
$result=mysqli_query($conn,$sql);
?>
<div class="comment_div">
<p class="comment"><?php echo $comment; ?></p>
<p class="username">Posted By:<?php echo $username; ?></p>
<p class="datetime"><?php echo $datetime; ?></p>
</div>
<?php
exit;
}
}
else{
header("LOCATION: signup.php");
}
?>
非常感謝您的幫助!我對此很新,並且對發生了什麼問題感到困惑!
再次提前致謝!
看看你的控制檯,使用php的錯誤報告和查詢/查詢來檢查錯誤。 –
在不相關的說明中,您的代碼非常容易受到來自黑客的SQL注入攻擊和JavaScript注入攻擊。你應該修復這些之前,你被黑客攻擊 – hanshenrik
我沒有得到任何錯誤..只是沒有任何反應,所以我不能夠明白什麼是錯的 – meagler