I'm trying to make it possible to display user comments linked to posts on a blog. The text information is stored in a mysql database, and the images are stored on a server. I've managed to get it to work, sort of, if there's only one comment, but if more than one comment is made on one post, every single post repeats twice.
I have another problem: I just added a new variable $name to the posting form, so that the poster can put their name in the post. Suddenly, the information no longer sends to my mysql database. It passes the IF test, saying that all the information has been sent.. but it's just not there.
Please help with any part of these problems! I'm going to post both scripts here. (there is not enough room for me to post the html form where users upload data too; let me know if you'd like to see it)
next, the PHP form to upload the information:
<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
($_FILES["uploaded_file"]["size"] < 350000)) {
//Determine the path to which we want to save this file
$newname = 'posts/'.$filename;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
echo "File uploaded: ".$newname;
} else {
echo "Error: A problem occurred during file upload!<br/>";
}
} else {
echo "File ".$_FILES["uploaded_file"]["name"]." already exists on the server<br/>";
}
} else {
echo "Error: Only .jpg images under 350Kb are accepted for upload<br/>";
}
} else {
echo "No pictures were uploaded<br/>";
}
//FILE2
//Сheck that we have a file
if((!empty($_FILES["uploaded_file2"])) && ($_FILES['uploaded_file2']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename2 = basename($_FILES['uploaded_file2']['name']);
$ext2 = substr($filename2, strrpos($filename2, '.') + 1);
if (($ext2 == "jpg") && ($_FILES["uploaded_file2"]["type"] == "image/jpeg") &&
($_FILES["uploaded_file2"]["size"] < 350000)) {
//Determine the path to which we want to save this file
$newname2 = 'posts/'.$filename2;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname2)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file2']['tmp_name'],$newname2))) {
echo "File uploaded: ".$newname2;
} else {
echo "Error: A problem occurred during file upload!<br/>";
}
} else {
echo "File ".$_FILES["uploaded_file2"]["name"]." already exists on the server<br/>";
}
} else {
echo "Error: Only .jpg images under 350Kb are accepted for upload<br/>";
}
} else {
echo "";
}
//FILE3
//Сheck that we have a file
if((!empty($_FILES["uploaded_file3"])) && ($_FILES['uploaded_file3']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename3 = basename($_FILES['uploaded_file3']['name']);
$ext3 = substr($filename3, strrpos($filename3, '.') + 1);
if (($ext3 == "jpg") && ($_FILES["uploaded_file3"]["type"] == "image/jpeg") &&
($_FILES["uploaded_file3"]["size"] < 350000)) {
//Determine the path to which we want to save this file
$newname3 = 'posts/'.$filename3;
//Check if the file with the same name is already exists on the server
if (!file_exists($newname3)) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['uploaded_file3']['tmp_name'],$newname3))) {
echo "File uploaded: ".$newname3;
} else {
echo "Error: A problem occurred during file upload!<br/>";
}
} else {
echo "File ".$_FILES["uploaded_file3"]["name"]." already exists on the server<br/>";
}
} else {
echo "Error: Only .jpg images under 350Kb are accepted for upload<br/>";
}
} else {
echo "";
}
$dbh = mysql_connect("--", "--", "--", "--")
or die(mysql_error()) ;
mysql_select_db("--") or die(mysql_error()) ;
$title = $_POST['title'];
$post = $_POST['post'];
$date = $_POST['date'];
$name = $_POST['name'];
$id= mysql_insert_id();
$size= ($_FILES["uploaded_file"]["size"]);
$size2= ($_FILES["uploaded_file2"]["size"]);
$size3= ($_FILES["uploaded_file3"]["size"]);
if (mysql_query("INSERT INTO Posts (Title,Content,Date, name, image_url, image_name, image_type, image_size, image_url2, image_name2, image_type2, image_size2, image_url3, image_name3, image_type3, image_size3)
VALUES ('$title', '$post', '$date', $name, '$newname', '$filename', '$ext', '$size', '$newname2', '$filename2', '$ext2', '$size2', '$newname3', '$filename3', '$ext3', '$size3')" ));
{
echo "<br/><font color='#FF0000' face='Georgia, Times New Roman, Times, serif' style='font-style:italic; text-decoration:underline; font-size:44px'>Success!!!</font><br/><br/>";
echo "<a href='http://eadersdigest.com'><font color='#66FF99' style='font-style:italic' face='Times'> Please click here to see post </font>";
}
?>
and here is the index that should retrieve and display the information (i know the html is a mess!):
<?php
$dbh = mysqli_connect("--", "--", "--", "--");
$sql = "SELECT Title,Content,Date, name, image_url, image_url2, image_url3, id, comment, comment_id, person FROM Posts, Comments ORDER BY id DESC";
$query = mysqli_query($dbh,$sql);
echo "<html>
<head></head><title>velcome</title>
<body><center>
<font style='color:#00FFFF;font-family:futura,helvetica; font-size:240px'>
EATERS DIGEST
</font></center>
<div style='background: white;
border-width: medium;
font-family:times;
font-size:16px;
float: left;
position: absolute;
top: 615px;
width:80px'>
<br/><br/><br/><br/>
<a style='text-decoration:none' href='http://eadersdigest.com/projecks/projecks.html'><font color='#99CC00'><i>projecks</i></font></a><br/>
<a style='text-decoration:none' href='http://eadersdigest.com/members.html'><font color='#99CC00'><i>digestion</i></font></a>
<br/><br/><br/>
frendz >><br/>
<a style='text-decoration:none' href='http://eadersdigest.com/login.html'><font color='#FF6600'><i>LAWG IN</i></font></a>
<br/><br/>
</div>
<table style='border-style:solid; margin-left: auto;
margin-right: auto; margin-top: 40px; width:800px'
bordercolor='#999999'
cellpadding='20'>";
if($query) {
$rows = mysqli_num_rows($query);
for($i = 0; $i < $rows; $i++) {
$feed = mysqli_fetch_assoc($query);
$title = $feed['Title'];
$post = $feed['Content'];
$date = $feed['Date'];
$id = $feed['id'];
$name = $feed['name'];
$pic = $feed['image_url'];
$pic2 = $feed['image_url2'];
$pic3 = $feed['image_url3'];
echo "<tr >\n";
echo "<td >\n";
echo "<a href='archive.php'><font face='Georgia, Times New Roman, Times, serif' color='#999999'>$title</font></a>\n";
echo "<div align='right'>$date</div><br/>";
echo "<div align='right'>$name</div><br/>";
echo " <p>$post</p><br/>\n";
if ( !empty($feed['image_url']) ) {
echo "<img src= '$pic'/><br/><br/>";
} else {
echo " ";
}
if ( !empty($feed['image_url2']) ) {
echo "<img src= '$pic2'/><br/><br/>";
} else {
echo " ";
}
if ( !empty($feed['image_url3']) ) {
echo "<img src= '$pic3'/><br/><br/>";
} else {
echo " ";
}
echo " <a href='delete.php?id=$id&Title=$title&Content=$post&Date=$date&image_url=$pic&image_url2=$pic2&image_url3=$pic3'><div align='right'>delete<br/></a>\n";
echo " <a href='comment1.php?id=$id&Title=$title&Content=$post&Date=$date&image_url=$pic&image_url2=$pic2&image_url3=$pic3'>comment on this post<br/></a>\n";
$comment = $feed['comment'];
$person = $feed['person'];
$commentid = $feed['comment_id'];
if ( ($commentid == $id) ) {
echo "comments <br/> $person <br/> $comment";
} else {
echo " ";
}
echo "<hr />\n";
echo "</td >\n";
echo "</tr>\n";
}
echo "</table>\n";
echo "</center>";
echo"<a style='text-decoration:none' href='http://eadersdigest.com/archive.php'><font color='#FF6600'><center><br/>archive</center></font></a>";
}
echo"</body>
</html>"
?>