I am working on a simple inbox/pm system, and I can't figure out why but I can get the display working for sent messages, I can display the list of sent items, view pms from the inbox, but not sure what I am doing wrong, any tips appreciated..
here is my code:
<table>
<?php
$id = $_GET['id'];
$from_user = $_SESSION['user_id'];
$sql = "SELECT users.user_id, users.username, users.profile, messages.id, messages.to_user, messages.from_user,
messages.subject, messages.message, messages.has_read, messages.deleted, messages.date_sent
FROM `messages`
JOIN `users` ON messages.to_user = users.user_id
WHERE messages.from_user = '$from_user' AND messages.id = '$id' ORDER BY messages.date_sent DESC";
$result = mysql_query($sql);
$rows = mysql_fetch_array($result);
$from_user = $rows['from_user'];
$subject = $rows['subject'];
?><tr>
<td width="50px" align="center">
<img src="<?php echo $rows['profile']; ?>" width="40px"><br><?php echo $rows['username']; ?>
</td>
<td valign="top" width="350px">
<b><?php echo $rows['subject']; ?></b><br>
<?php echo $rows['message']; ?>
</td><td><?php echo $rows['date_sent']; ?></td>
</tr>
<tr>
<td colspan="3"><hr></td>
</tr>
</table>
Your have $from_user specified twice, try removing the second and see how you go.
Related
I recently made a leaderboard and I would like to make some changes.
This is the code :
<table>
<tr>
<td></td>
<td>Name</td>
<td>Likes</td>
</tr>
<?php
$con=mysqli_connect("localhost", "user", "pass", "database");
$sql = ("SELECT username, likes FROM users ORDER BY likes DESC LIMIT 5");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$rank}</td>
<td>{$row['username']}</td>
<td>{$row['likes']}</td>
</tr>";
$rank++;
}
}
?>
</table>
Now, what I would like to do is add instead of 'username' the avatar of each user with 'alt' for username. E.g :
<img src="<?php echo $example->avater;?>" alt="<?php echo $example->username;?>" />
This is the location of avatar in mysql !
I also have a css code created for the avatar to appear in a round circle.That means we will use
<div class="pro_usrs_container">
I tried but I don't understand how to make the codes so that I can integrate them in <td> instead of <td> $ row ['username'] </td>
Thank you very much !
Select avater in your Query and use it in your table.
<table>
<tr>
<td></td>
<td>Name</td>
<td>Likes</td>
</tr>
<?php
$con=mysqli_connect("localhost", "user", "pass", "database");
$sql = ("SELECT username, avater, likes FROM users ORDER BY likes DESC LIMIT 5");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) :
while ($row = mysqli_fetch_assoc($result)) : ?>
<tr>
<td><?php echo $rank; ?></td>
<td><img src="<?php echo $row['avater'];?>" alt="<?php echo $row['avater'];?>" /></td>
<td><?php echo $row['likes']; ?></td>
</tr>
<?php $rank++;
endwhile;
endif;
?>
</table>
Based on the requirement above:
<table>
<tr>
<td></td>
<td>Name</td>
<td>Likes</td>
</tr>
<?php
$con=mysqli_connect("localhost", "user", "pass", "database");
$sql = ("SELECT avater, username, likes FROM users ORDER BY likes DESC LIMIT 5");
$result = mysqli_query($con, $sql);
$rank = 1;
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>{$rank}</td>
<td><img src="{$row['avater']}" alt="{$row['username']}" /></td>
<td>{$row['likes']}</td>
</tr>";
$rank++;
}
}
?>
I'm still in the process of learning SQL queries. I've tried searching around online, but I don't even know what terms I should be searching for, so I'm looking for someone to point me in the right direction. I have two tables -- appointments and logins. I'm trying to use the concat function to get the first and last name for both "apptFor" and "addedBy." Do I need to have separate queries? It seems like it should be able to get done in a single query.
<table border="1">
<tbody>
<tr>
<td width="140" align="center"><strong>Appt Date/Time</strong></td>
<td width="100" align="center"><strong>Appt For</strong></td>
<td width="300" align="center"><strong>Appointment</strong></td>
<td width="100" align="center"><strong>Added By</strong></td>
<td width="100" align="center"><strong>Date Added</strong></td>
</tr>
<?php
$query = "SELECT a.appID,a.appDate,a.appNote,a.apptFor,a.addedBy,a.added,concat(u.firstName,' ',u.lastName) as appAddedBy FROM appointments a ";
$query .=" INNER JOIN logins u on a.addedBy=u.userid WHERE a.userID='$uid' order by appDate asc";
$result = mysqli_query($con, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['appDate']; ?></td>
<td><?php echo $row['apptFor']; ?></td>
<td><?php echo $row['appNote']; ?></td>
<td><?php echo $row['appAddedBy']; ?></td>
<td><?php echo $row['added']; ?></td>
</tr>
<?php }} ?>
</tbody>
</table>
If you need tow name you should join two time the logins
$query = "SELECT a.appID,a.appDate,a.appNote,a.apptFor,a.addedBy
,a.added,concat(u1.firstName,' ',u1.lastName) as appAddedBy
,concat(u2.firstName,' ',u2.lastName) as appApptFor
FROM appointments a ";
$query .=" INNER JOIN logins u1 on a.addedBy=u1.userid";
$query .=" INNER JOIN logins u2 on a.apptFor=u2.userid
WHERE a.userID='$uid' ";
order by appDate asc";
$result = mysqli_query($con, $query);
Currently, When I type 'M' in the search engine I do not get any results inspite of having a 'Math' value in my database. (I am using Match Against function of mysql on my website). So I decided to make a code that first runs a Match Against function and if no output is obtained, it again runs the search but this time using 'LIKE' function of my sql and if still no result is obtained it shows alert 'no values found'.
the gist of code is somewhat like this..
mysql(Match against function)
If (mysqli_num_rows > 0)
{output}
elseif (mysqli_num_rows < 1)
{ mysqli (LIKE function)
then show output}
else {show alert'no results'}
Will this code give the result 'Math' when I enter 'M' in the search engine.
The actual code.
<?php
if (isset($_POST['go']))
{ $search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search))
{
if(isset($_POST['colleges']) )
{
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
}
else
{
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
$searchresults = mysqli_num_rows($query_run1);
?><p class = "totalresults"> <br />
<br />     <font size= "4" >Total Results :
<?php
echo $searchresults ;
?>
</font> </p>
<?php
if(mysqli_num_rows($query_run1)>0)
{
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
elseif(mysqli_num_rows($query_run1) < 1)
{
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?> <table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
}
?>
</table>
<?php
}
else
{
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
else
{ echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
}
You have some syntax issues in your code, your else statements were not lined up properly, so your page was not working. (For example, you had an } else { lined up with another } else {).
I went through all of the code and formatted it so I could see what the issue was, remember, Some sensible code indentation would be a good idea.
It helps us read the code and more importantly it will help you debug your code.
Take a quick look at a coding standard for your own benefit.
You may be asked to amend this code in a few weeks/months and you will thank me in the end.
<?php
if (isset($_POST['go'])) {
$search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search)) {
if(isset($_POST['colleges']) ) {
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
} else {
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
$searchresults = mysqli_num_rows($query_run1);
?>
<p class = "totalresults">
<br />
<br />
    <font size= "4" >Total Results: <?php echo $searchresults; ?></font>
</p>
<?php
if(mysqli_num_rows($query_run1)>0) {
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
<?php
} //end while loop
?>
</table>
<?php
} else if(mysqli_num_rows($query_run1) < 1) {
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip ;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
</table>
<?php
} //end while loop
} else { //num rows < 1
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
} else { //no searches returned
echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
I also suggest, for really complex, nested structures, to comment what each ending bracket goes to like I did in your code. I put comments so that I could see exactly what the } was ending.
WARNING: This code may be vulnerable to SQL Injection Attacks. You should Learn about Prepared Statements for MySQLi or PDO. I recommend PDO, which I wrote a function for to make it extremely easy, very clean, and way more secure than using non-parameterized queries.
If you could access the mysql.ini of your host, you could simply change the min word length for full text search.
[mysqld]
ft_min_word_len=N
Otherwise you should decide which search function you want to use. It's not very intuitive for users if one search is a full text search and the other is a simple LIKE.
One way around could also be to only use full text, if the user types more than 2 chars.
// ...
if (strlen($query) < 4) {
$where = "MATCH (...) AGAINST ...";
} else {
$where = "...LIKE ... ";
}
$sql .= $where;
Btw. MATCH AGAINST should normaly return way more results than like, so doing a LIKE search after MATCH AGAINST returns 0 rows, is kinda useless.
I am done with a CMS school system which i created from scratch for practice in php. My question is for example I have Accounting 101, Computer Science 101, however there must multiple times for Accounting 101. For example: Ticket 1035, 1036 are both Accounting 101 and they should appear in the same table, but in my code it shows them in different classes. Here is my code.
if(isset($_GET['id']))
{
$category = $_GET['id'];
$sql = "SELECT * FROM classes WHERE category_id = " . $category;
$query2 = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($query2))
{
?>
<center><h3><?php echo $row->class_name . '-' . $row->units; ?> </h3></center>
<table border ="0" wdith="100%">
<tr>
<td>
<strong>Description: </strong>
<?php echo $row->class_description; ?>
</tr>
</td>
</table>
<br/>
<table border="1" width="44%">
<tr>
<td width="60"><b>Ticket</b> </td>
<td width="123"><b>Days</b></td>
<td width="120"><b>Hours</b></td>
<td width="64"><b>Room</b></td>
<td><b>Instructor</b></td>
</tr>
<tr>
<td width="60"> <?php echo $row->ticket; ?> </td>
<td width="123"><?php echo $row->days; ?></td>
<td width="120"><?php echo $row->start_hours . $row->time_format . '-' . $row->end_hours . $row->time_format2 ; ?> </td>
<td width="64"> <?php echo $row->room_number; ?></td>
<td><?php echo $row->instructor_name; ?></td>
</tr>
}//end while
}//end if
Its showing Accounting 101 with different tickets in different tables, but it should be all in 1 table. Thanks.
You need a double loop if you're trying to get records inside of records. For example:
while ($row1 = mysql_fetch_object($query1))
{
echo $row1->ParentName;
$query2 = 'select * from `mytable` where `myForeignKey` = ' . $row1->ParentId;
while ($row2 = mysql_fetch_object($query2))
{
echo $row2->ChildName;
}
}
You could also do a left join. Let me know if you need a sample of that.
Edit:
The left join would be done like this:
$sql = "select * from `classes` as a where category_id = '{$category}' left join `tickets` as b on a.id = b.class_id"
Ref. http://www.w3schools.com/sql/sql_join_left.asp
I'm trying to make a facebook wall-alike php script.
It loads the posts stuff, photo url and user stuff like username and user id from my MySQL database. It also checks if the post is just a comment to another post (parentof). I can present all the original posts with pictures with a simple do-while using tags .
But how could I present the comments under every post? I guess with another do-loop, but how?
Here is my code:
<?php
require_once "config.php";
$result = mysql_query('SET NAMES utf8');
$result = mysql_query('SET CHARACTER SET utf8');
$wallhaku = mysql_query("SELECT `wall`.`post_id`, `wall`.`parentof`, `wall`.`sentby`, `wall`.`text`, `wall`.`image_url`, `users`.`username`, `users`.`photopath`, `users`.`name`, `users`.`member_id` FROM `wall` LEFT JOIN `users` ON `wall`.`sentby` = `users`.`member_id` WHERE parentof=0 ORDER BY post_id DESC") or die (mysql_error());
$row_wallhaku = mysql_fetch_array($wallhaku);
<table width="800" height="120" border="0" cellpadding="10">
<?php $i=0; $numberpage=1;
do {
$wallid = $row_wallhaku['post_id'];
$parenthaku = mysql_query("SELECT post_id, parentof FROM wall WHERE parentof=$wallid") or die (mysql_error());
$row_parenthaku = mysql_num_rows($parenthaku);
<td width="120" align="left">
<img src=<?php echo $row_wallhaku['photopath']; ?> height= "100" width="100">
</td>
<td width="600" align="left">
<?php echo $row_wallhaku['name']?><br /><p><?php echo $row_wallhaku['text'];
if($row_wallhaku['image_url']=="0") {
<p align="right"><?php echo $row_parenthaku ?> kommenttia. Lue, kommentoi <?php } ?></td>
<?php $i++; if($i%$numberpage==0) echo "</tr>";
if($row_wallhaku['image_url']!="0")
{
<tr>
<td width='800' colspan='2' align='center'>
<img src=<?php echo $row_wallhaku['image_url']; ?> height="180">
<p align="right"><?php echo $row_parenthaku ?> kommenttia. Lue, kommentoi
</td>
</tr>
<?php
}
</table>
In case that the relation between posts is one to many , You need to use 2 queries.
The first query will get the data of the topic (the post).
The other query will get the data of all the comments which "parentof" equals the topic's id.
$id = $_GET['id'];
$getTopic = mysql_query("SELECT * FROM posts WHERE id='$id'");
$topic = mysql_fetch_array($getTopic);
$getComments = mysql_query("SELECT * FROM posts WHERE parentOf='{$topic['id']}'");
while($comment = mysql_fetch_array($getComments))
{
echo $comment['title']; //You should use a "view" functin which handles the HTML and staff.
}