How to make the comment to be in specific topic? - php

I need help. I have problem when commenting on topic. I want when the user comment in specific topic, it will appear only in the specific topic. The problem is when user comment in the specific topic, the comment can be seen in any topic available including the specific topic. Previously i store in comment.txt file but now i already delete the comment.txt file and make a database to store the comment based on what reply has told me. The problem is still the same. How to make it appear in topic A rather than appear on other topic. Hope anyone can help me because i has been trying to solve this for 4 days. Below is the updated code:
<?php
$conn = mysqli_connect("localhost", "root", "", "ourmusic");
$post_title = $_GET["post_title"];
//echo "<h1>".$post_title."</h1>";
$query = mysqli_query($conn,"SELECT poster, post_title, post_desc FROM posting WHERE post_title = '$post_title';");
$data = mysqli_fetch_assoc($query);
echo "</br><h1>".$data["post_title"]."</h1>";
echo "<sup>by <b>".$data["poster"]."</b></sup <br><br><br><br><hr><br><br>";
echo "<p>".$data["post_desc"]."</p><br><br><br>";
?>
<!-- Back to Forum -->
<a href="forum.php">
<button type="submit" name="back-btn" class="btn btn-success">Back</button>
</a>
<a href="comment.php">
<button type="submit" name="back-btn" class="btn btn-success">Comment</button>
</a><br><br>
<p>Comments:</p>
<!-- Post And Show The Comment -->
<?php
$connection = mysqli_connect("localhost", "root","","ourmusic");
$query_c = "SELECT * FROM [ourmusic].[posting].[dbo].[post_title]";
$result_c = mysqli_query($connection, $query_c);
?>
<?php
$username = $_SESSION['username'];
while($data_c = mysqli_fetch_assoc($result_c))
{
$poster= $data_c['poster'];
$comment_content = $data_c['comment_content'];
?>
<tr>
<td><b><?php echo $poster; ?></b></td><br>
<td><?php echo $comment_content;?></td><br>
</tr>
<?php
}
?>
And this is for my Final Year Project.

Rather than save comments in a text file and read them later on, it will be ideal to save the comments in a database.
Your table could contain columns for comments and topics.
For example, for Topic A, save the topic as well as the comment in the table at the point of commenting.
When fetching the comments, query the database for the comments, filtering them by topics.

Related

MYSQL retrieve data from two tables

I am trying to make a small discussion system where people can login and post a discussion. On the homepage I want to show the discussions.
First I retrieve the data for the discussions (topic, category, username, etc) from the table 'discussions'and order them by 'date_time'.
I put these values in div's that are working just how I want it. The problem I am having now is to show the avatar of the user. The avatars are stored in a table 'users' under the column 'avatar'.
So I need to retrieve the value from the column 'avatar' in the table 'users' where 'username' matches the username of the discussion.
This is the code that I have now but it's not working. I have tried different things but I am not very familiar yet with PHP so I don't really know how to go from here.
Thanks in advance!
$result = mysql_query("SELECT
topic,
category,
date_time,
username,
SUBSTRING(discussion, 1, 80) AS discussion
FROM discussions
ORDER BY date_time DESC");
while($record = mysql_fetch_array($result))
{
?>
<div class="discusscolumn">
<div>
<p><? echo $record['category'] . ": <b>" . $record['topic'] . "</b>"?></p>
</div>
<div>
<?php
$discussion_username=$record['username'];
$getavatar = mysql_query("SELECT avatar, username FROM users WHERE username='$discussion_username' ");
$avatarrecord = mysql_fetch_array($getavatar);
echo'<span class="smallpic"><img src="user/'.$record['username'].'/'.$getavatar['avatar'].'"></span>';
?>
</div>
<div>
<p>Posted by <? echo "<a href='user.php?u=".$record['username']."'>".$record['username'] ?> </a></p>
</div>
<div>
<p><br><? echo $record['discussion'] ?>&nbsp...</p>
</div>
<div>
<p><? echo $record['date_time'] ?></p>
</div>
<div>Discuss</div>
</div>
<?php } ?>
PS: I know I am working with mysql instead of mysqli and that I'm mixing HTML and PHP code but I just want the basics to work now.
Please check your img source(src), that you have passed as user/username/avatar instead of passing like this please put url like http://www.w3schools.com/images/w3schools_green.jpg so that you will be getting out. But in your case you need to have all your user image in some other global storage area for eg.Amazon s3 bucket, and you need to get string from database construct like above url and append that in img src inside input tag

View page based on a record

I'm new to PHP and pardon me for asking this very basic question. What I want to do is to display or view a page based on a specific record. For example, I have a home.php page which lists records of lessons. And when I click on a specific record, it will go a page named lesson.php . I have to view the relevant information/data from my dB of that specific lesson. I tried to use GET but I think it's not going to meet the requirement of my system.
This is what I've tried so far:
$qry1stQuarter = $conn->prepare("SELECT l.lesson_title FROM tbllessons as l
JOIN tblstudents as s
ON l.grade_level = s.grade_level
WHERE quarter_code = '1st'
AND s.grade_level=:grade_level");
$qry1stQuarter->execute(array(':grade_level' => $grade_level));
<div id="tabs-2">
<div id="accordion">
<h3><strong>Yunit 1</strong></h3>
<div>
<?php
for($i=0; $row = $qry1stQuarter->fetch(); $i++){
$lesson_title = $row['lesson_title'];
?>
<div id = "lessons">
<?php
echo "<a href = 'lesson_view.php'>$lesson_title </a>";?>
</div>
<?php
} // end of for loop
?>
</div> <!-- end of Yunit 1 -->
What is the best way to do this? Your help is pretty much appreciated. Thanks.
In your database, I assume you have an ID column. A typical way to do what you are asking is to use that ID as a GET parameter on a link, and then include that in your WHERE clause in your SQL statement.
Eg:
echo "<a href='lesson_view.php?id=$lesson_id'>$lesson_title</a>";?>
And then on your lesson_view.php page, your SQL has something like this:
SELECT * FROM tbllessons WHERE id = mysql_real_escape_string($_GET['id'])

update delete column to 1 where review id is 'x' using mysql?

I've asked a similar question before however i've done alot of changes to my script and my question is now different to my last question. So now i've done these changes this should all work fine however it isn't.
I've basically got user reviews where a user can delete or approve reviews. I am focusing purely on the delete function at the moment.
We pull the reviews through in reviews.php and a the user can click delete which points to delete_review.php which then runs the sql function stored in functions.php.
I've listed the exact code layed how it works below but besides me spending ages trying to work out why it won't work i can't figure it out. When we click delete it links to delete_review.php but it won't find the review id which is the id of the review in which to delete.
Can someone please show me how i can fix this. thank you.
review.php
<?php
$reviews_set = get_new_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="pend-review-content">
<?php echo "{$reviews['content']}"; ?>
</div>
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" />";?>
</div>
<div class="forum-text">
<?php echo "Posted by {$reviews['display_name']}"; ?> <?php echo "".$age." days ago"; ?>
</div>
<div class="delete"></div>
</div>
<? } ?>
function.php
function delete_review($review, $user) {
global $connection;
global $_SESSION;
$query = "UPDATE ptb_reviews
SET deleted='1'
WHERE id=$review
AND to_user_id=$user";
mysql_query($query, $connection);
}
function get_new_reviews() {
global $connection;
global $_SESSION;
$query = "SELECT r.from_user_id, p.display_name, r.content, r.id reviews_id, r.date_added
FROM ptb_reviews r, ptb_profiles p
WHERE r.to_user_id =".$_SESSION['user_id']."
AND r.deleted = '0'
AND r.read_review = '0'
AND p.user_id = r.from_user_id
ORDER BY r.date_added DESC ";
$reviews_set = mysql_query($query, $connection);
confirm_query($reviews_set);
return $reviews_set;
}
delete_review.php
<?php
require_once("session.php");
require_once("functions.php");
require('_config/connection.php');
delete_review ($_GET['review'], $_SESSION['user_id']);
?>
1- check if you make session start session_start(); in the begining of your review.php file
2- look at this
$query = "SELECT r.from_user_id, p.display_name, r.content, r.id reviews_id, r.date_adde
^--------what this space ?
3- try to do global $_SESSION['user_id'] in your function.
4 be sure if its $reviews or $reviews_id here as ROY Finley mentioned
<a href="includes/delete_review.php?review=<?php echo $reviews['review']
^----------here
I think this:
$reviews['review']
maybe should be this:
$reviews['reviews_id']
not sure, your code needs some improvement. You should really look into mysqli or PDO. All of this could easily be done with a simple class.

getting article to update in mysql table by id?

I have a reviews system on my site where users can rate other users. When a user logs in they can go to their pending reviews section where they can either approve or delete a review.
I pull through the reviews from the table ptb_reviews and list them on the page reviews.php. Each review then has an approve or delete link which goes to either approve_review.php or delete_review.php and in there is the SQL function to set the relevant column in my table, so approve or delete, from 0 to 1.
So if a user approves a review it updates approve in the table to 1 where that specific review has been chosen to be approved and then its the same for deleted.
I'm having a problem in that when I try to approve or delete a review, I echo out the query to test it to see what's happening and to see if its doing what I'm asking it to do and I get this:
UPDATE ptb_reviews SET approved = 1 WHERE id = '' LIMIT 1
this is implying that it cant find the review id and it wont therefore apply the update to 1 in the table where appropriate for that review.
here's my code for reviews.php where the reviews are pulled through:
<?php
$reviews_set = get_new_reviews();
while ($reviews = mysql_fetch_array($reviews_set)) {
$review_id = (int) $_GET['review_id'];
// Just this, is a great security enhancement. Forces the variable to be int (like all id's are).
// You can also check if its a numeric by doing
if (is_numeric($review_id)){
// continue with the update query
} else {
// something fishy is going on..
}
?>
<div class="prof-content-pend-reviews" id="reviews">
<div class="pend-review-content">
<?php echo "{$reviews['content']}"; ?>
</div>
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"data/photos/{$reviews['from_user_id']}/_default.jpg\" />";?>
</div>
<div class="forum-text">
<?php echo "Posted by {$reviews['display_name']}"; ?> <?php echo "".$age." days ago"; ?>
</div>
<div class="approve"></div>
<div class="delete"></div>
</div>
<? } ?>
And next is my code that carry's out the SQL function for approve_review.php:
<?php
require_once("session.php");
require_once("functions.php");
require('_config/connection.php');
$query = "UPDATE ptb_reviews SET approved = 1 WHERE id = '$review_id' LIMIT 1";
mysql_query($query, $connection);
echo $query;
die();
// See what is really sent to MySQL
if (!mysql_query($query, $connection)){
die(mysql_error());
}
?>
I'm basically asking is the reason nothing updates because it can not find the id for the review which is being either approved or deleted and what can I do to get it to update and so that it knows which review I'm asking it to approve or delete?
I'm still learning PHP and MySQL so if someone could show me or explain what to do that'd be great.
You are getting the query variable like this:
$review_id = (int) $_GET['review_id'];
But in your link you use:
<a href="includes/approve_review.php?review=<?php echo $reviews['review_id']; ?>">
^^^^^^
So your review ID is stored in $_GET['review'] and not $_GET['review_id'].
To start you should change that line to (or the other way around...):
$review_id = (int) $_GET['review'];

Using PHP (and maybe AJAX?) with jQTouch

I am having some difficulty using PHP with jQTouch. I am fairly
confident with JavaScript however my PHP skills are little to none.
I am creating an application for my final year project at University
what displays football rumours posted by different users. My problem
is as follows:
I have one screen that displays each individual rumour, using a while
loop in PHP I am able to get each rumour from the database and display
them correctly. However I want to be able to click on one rumour which
then displays this rumour in a different screen, along with options to
reply/share etc. However I do not know how to tell which rumour has
been clicked on.
Snippets of my code:
All rumours page:
<?php
$q1 = "SELECT * FROM tblrumours;";
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_assoc($r1) ){
?>
<a class="rumourTag submit" id="<?php echo $row1['rumourID']; ?>">
<div class='oneRumour'>
<div class='standardBubble'>
<p>
<?php
$userID = $row1['userID'];
$q2 = "SELECT * FROM tblusers WHERE userID = $userID;";
$r2 = mysql_query($q2);
while( $row2 = mysql_fetch_array($r2) ){
$username = $row2['username'];
$teamID = $row2['teamID'];
}
$q5 = "SELECT * FROM tblteams WHERE teamID = $teamID;";
$r5 = mysql_query($q5);
while( $row5 = mysql_fetch_array($r5) ){
echo "<img src='img/".$row5['teamPicture']."' alt=''
class='teamImg' />";
}
?>
<span class='username'>
<?php
echo $username;
?>
</span>
<br/>
<span class='rumourMsg'><?php echo $row1['rumourText']; ?></
span>
</p>
</div>
</a>
SINGLE RUMOURS PAGE:
<?php
$q1 = "SELECT * FROM tblrumours WHERE rumourID = 1;"; /* NEED
TO SELECT WHERE RUMOUR ID IS THE ONE THAT IS CLICKED */
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_array($r1) ){
?>..........
I have tried using Session variables, storing the ID's in an array,
creating a separate php file for the single rumour page, and all to no
avail. I am guessing I have to use AJAX in some way, but I have no
idea where to even begin. Any help is greatly appreciated!
Thanks!
If you need to click on a rumour to see more details about it, you could always output in the HTML a unique value used to reference that rumour in the DB.
e.g. have <span class='rumourMsg' id='rumourName'> where rumourName is a unique value stored in your database to reference that rumour. Then when a user clicks to see more details, you can make a request to the PHP page with that value and return the content.
e.g. rumourDetails?rumourName=uniqueRumourName
(make sure to escape all your data properly to avoid SQL injection vulnerabilities.)

Categories