displaying 2 private messages to same destination with SQL - php

So i'm trying to implement a Private messaging system and so i use this code to extract the info from an SQL table:
$query = mysql_query("SELECT * FROM `pm` WHERE `to` = '{$_SESSION['user_id']}'");
$pm = mysql_fetch_array($query);
and essentially all this code does is pulls values from columns
(INT) from, (INT) to, (VARCHAR) subject and (VARCHAR) message
and it all worked until i sent more then 1 message to the same user.
thise code only shows the first message, and i have no idea how to make it show all the messages, so any help would be appriciated.
EDIT
Forgot to mention that the table itself is named 'pm'
EDIT2
this is how i echo the output:
<h3>From: </h3><?php echo $pm['from']; ?>
<h3>Subject: </h3> <?php echo $pm['subject']; ?>
<h3>Message: </h3> <?php echo $pm['message']; ?>

You didn't really tell us how you were echoing out the PM's. But here's how you would print every one.
while ($row = mysql_fetch_array($query)) {
echo $row['message'];
}

Not sure if this is the most efficient but you can add a time_created column and a was_sent column. Have your query grab the oldest unsent message and then mark it as sent. Repeat.

Related

PHP - Dynamically changing the URL, Error ID Not found

New to php and decided to build a little template to display some fake content from a database. The goal is:
loop through the DB, print the items,
wrap them each in a link to the same page (in this case, details.php)
use a GET (details.php?id) to put the id column of the DB.
I've got the printing/looping happening, and the linking but I'm running into a strange problem. When I try to echo out the ID, I get the following error: "Notice Undefined index: id in /opt/lampp/htdocs/arrayTest/index.php on line...". When I use another any other column from the DB, it echos properly. What am I missing?
Query:
$sql = 'SELECT firstName FROM userNames';
$query = $dbh->query($sql);
PHP Page:
<h1>Names of People</h1>
<?php
if($query->rowCount() > 0) {
echo 'Found some shit';
foreach($query as $row) { ?>
<p><?php echo $row['firstName']; ?></p>
<?php } } ?>
DB Screen shot attached
Use this:
$sql = 'SELECT * FROM userNames';
You don't have the id because you are only selecting the firstName
You need to use id in your query modify your query as
$sql = 'SELECT id,firstName FROM userNames';

Mysql and PHP leaderboard acting strange

I am trying to make a leaderboard and sort my data by kills, but when I try to make it so it only grabs name, kill, death it doesnt grab anything but when I have it grab it all it works. Anyone know why? Code is below please assist.
<?php
$query = $koneksi->prepare("SELECT * from `player`");
$query->execute();
if($query->rowCount() == 0)
I am grabbing my mysql data here, if I change the * to the data I need no data is displayed.
echo "<tr><td colspan='6'><small>There's no player on ban list</small></td></tr>";
}
while($data = $query->fetch())
{
echo "<tr><td>".$data['name']."</td>";
echo "<td>".$data['kill']."</td>";
echo "<td>".$data['death']."</td>";
$kd = $data['kill'] / $data['death'];
echo "<td>".$kd."</td></tr>";
}
?>
Is it something to do with this or is something wrong? I am really confused.
Here you have to use bind_result() and in that you have to pass the number of parameters which is equal to your number of field from your player table.
Because here you are fetching data using select * query.

Displaying Data into html table

I am a neophyte programmer in php and I am always seeking for the solution of this problem. If anyone has an idea pls. post your answer and I am thankful for your great ideas to solve this stuff.
In my database table I have data like this:
In my php page I want to present in this way using html table.
Could anyone help me doing this stuff? Thank you very much…
I think you are searching for the functionality of GROUP_CONCAT in mysql (docs). You would get something like:
SELECT name, GROUP_CONCAT( week ), GROUP_CONCAT( there )
FROM presence
GROUP BY name;
which will return the following results you can parse using explode() in php (docs):
Andrew 4th,1st,3rd,2nd Present,Present,Present,Present
John 1st,4th,3rd,2nd Absent,Present,Present,Present
Mark 2nd,3rd,1st,4th Present,Present,Present,Present
Micheal 2nd,3rd,4th,1st Absent,Absent,Absent,Present
On a side note: If you haven't settled on a database scheme yet, it might be better to use an int for the week number column, as it is more reliable when sorting and easier to manipulate.
Sqlfiddle: http://sqlfiddle.com/#!2/fc785/1
Try this:
$SQL = "select NAME, WEEK, STATUS from tblattendance order by NAME, SUBSTRING(WEEK,1,LENGTH(WEEK) - 2) ASC";
$data = $db->query($SQL);
$last_name = "";
echo '<table><tr><th>NAME</th><th>1st WEEK</th><th>2nd WEEK</th><th>3rd WEEK</th><th>4th WEEK</th>';
while($row = $data->fetch_assoc()){
if($last_name != $row["NAME"]){
$last_name = $row["NAME"];
echo '</tr>';
echo '<tr>';
echo '<td>'.$row["NAME"].'</td>';
}
echo '<td>'.$row["STATUS"].'</td>';
}
echo '</tr></table>';
Try this :
$sql="select distinct name from tblattendance;";
$res=$mysqli->query($sql);
if($res){
while($row=$res->fetch_assoc()){
$name=$row['name'];
$sql="select week, status from tblattendance where name='$name';";
$res1=$mysqli->query($sql);
}
}

variable result from sql query not showing

Is there anything wrong in this scenario? the result isn't showing. maybe i'm getting the syntax wrong?
// fetch the latest tweet
$lastTweet = '';
$q2 = "SELECT tweet FROM timel ORDER BY id DESC LIMIT 1";
list($lastTweet) = $conn->get_results($q2);
if(!$lastTweet){
$lastTweet = "You don't have any tweets yet!";
}
var_dump($lastTweet);
and this on the html body:
<span class="latest"><strong>Latest: </strong><span id="lastTweet"><?.=$lastTweet?></span></span>
If you changed your first line to
$lastTweet = array();
And your third line to
$lastTweet = $conn->get_results($q2);
Then you would be able to use
<?php var_dump($lastTweet); ?>
But in your case lastTweet contains a string of text, hence you need to use echo to print it's content.
<?php echo $lastTweet ?>
Nb: i'm not sure about your project details but i'm guessing you'd need to also get the date, maybe id and user_id of the tweet, if that's the case, you might want to make lastTweet an empty array()
Use this:
<span class="latest"><strong>Latest: </strong><span id="lastTweet">
<?php echo $lastTweet; ?></span> </span>
Saludos ;)

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'];

Categories