wall updates can be seen only by friends - php

i'm creating a wall system where you can post updates on the index page and i wanted the posts to be seen only by friends of the user, i almost made my code work but i still have a problem ,but before i mention my problem if there is a better way to do it please share it with me , i will show you my code
first i have two tables for the code
'my_friends' : 'id' ,'username','friend'
'statues' : 'p_id','post','f_name','userip','date_created'
this code is in post.php then it will be included in the index page
//Check for user's friends
$check = mysql_query("select * from `my_friends` where `username` = '".$username."' order by `id` desc ");
//Get users friends
while($get_users_friends = mysql_fetch_array($check))
{
$show_more_button = 1;
$result = mysql_query("SELECT *,
UNIX_TIMESTAMP() - date_created AS TimeSpent FROM `statues` WHERE `f_name` = '".mysql_real_escape_string(strip_tags($get_users_friends["friend"]))."' ");
}
while($row = mysql_fetch_array($result))
{
$comments = mysql_query("SELECT *,
UNIX_TIMESTAMP() - date_created AS CommentTimeSpent FROM comments where post_id = ".$row['p_id']." order by c_id asc"); ?>
<div class="friends_area" id="record-<?php echo $row['p_id']?>">
<label style="float:left" class="name">
<b><?php echo $row['f_name']; ?></b><br>
<span>
</span><br>
<em><?php echo $row['post'];?></em>
<br clear="all" />
the code doesn't work right, it show the post feed to only one friend, the first added friend of the user ,in other words , the sql $result = mysql_query("SELECT *,
UNIX_TIMESTAMP() - date_created AS TimeSpent FROMstatuesWHEREf_name= '".mysql_real_escape_string(strip_tags($get_users_friends["friend"]))."' "); select all the statues of only one friend ,when i write echo mysql_real_escape_string(strip_tags($get_users_friends["friend"])),'<br>'; before the sql it shows all the friends but when i use it in the sql it doesn't, please help

Here are some thoughts that maybe can help you
Dont use mysql_* functions they are deprecated use PDO class instead
Dont query for * if you dont need whole the information from the table
Table name is statues
After completing first while your variable $result will hold only the last friend data query cause it gets overritten by the next step of iteration(use $result[] = )
Aggregate info from $get_users_friends to rewrite your query into SELECT p_id, post, f_name, UNIX_TIMESTAMP() - date_created AS TimeSpent FROM statues WHERE f_name IN '.implode(',', $firends_name).
Dont see if you use $comments variable at all
$show_more_button = 1; dont see if it is used at all. and ofc it gets overritten by next iteration step;
your code should look something like:
$check = mysql_query("select friend from `my_friends` where `username` = '".$username."' order by `id` desc ");
while($get_users_friends = mysql_fetch_array($check, MYSQL_ASSOC))
{
$result[] = $get_users_friends['friend'];
}
$friends_data_query = mysql_query("SELECT p_id, f_name, post,UNIX_TIMESTAMP() - date_created AS TimeSpent FROM `statues` WHERE `f_name` IN'".implode(',',$result)."'");
while($friends_data = mysql_fetch_array($friends_data_query,MYSQL_ASSOC)){
But make sure that it is rewritten into PDO. Cause now it is very vulnurable to injections.

Related

PHP, SQL - getting fetch where table id = user id and count other table where row is = user id

Thanks for helping, first I will show code:
$dotaz = "Select * from customers JOIN contracts where customers.user_id ='".$_SESSION['user_id']."' and contracts.customer_contract = ".$_SESSION['user_id']." order by COUNT(contracts.customer_contract) DESC limit $limit, $pocetZaznamu ";
I need to get the lists of users (customers table) ordered by count of contracts(contracts table)
I tried to solve this by searching over there, but I can't... if you help me please and explain how it works, thank you! :) $pocetZanamu is Number of records.
I need get users (name, surname etc...) from table customers, ordered by number of contracts in contracts table, where is contract_id, customer_contract (user id)..
This should do it where is the column name you are counting.
$id = $_SESSION['user_id'] ;
$dotaz = "Select COUNT(`customer_contract`) AS CNT, `customer_contract` FROM `contracts` WHERE `user_id`=$id GROUP BY `customer_contract` ORDER BY `CNT` DESC";
Depending on what you are doing you may want to store the results in an array, then process each element in the array separately.
while ($row = mysqli_fetch_array($results, MYSQL_NUM)){
$contracts[$row[1]] = $row[0];
}
foreach ($contracts AS $customer_contract => $count){
Process each user id code here
}
Not sure what you are counting. The above counts the customer_contract for a table with multiple records containing the same value in the customer_contract column.
If you just want the total number of records with the same user_id then you'd use:
$dotaz = "Select 1 FROM `contracts` WHERE `user_id`=$id";
$results = $mysqli->query($dotaz);
$count = mysql_num_rows($results);

how can i delete rows id from the database

can somebody help me?
here is my php script...
$req = mysql_query('SELECT id, email, start_date, end_date, time_event, time_submitted, payment_method, status FROM booking_members WHERE status="Canceled"');
while($dnn = mysql_fetch_array($req))
$req1 = mysql_query('DELETE FROM booking_members WHERE id='$id');
while($dnn1 = mysql_fetch_array($req1))
{
<td class="left"><center>
<img src="images/cross.png"></img>
</td></center>
how can i get the id from $dnn['id'] in order to delete rows?
Why not just run the delete directly?
DELETE FROM booking_members WHERE status='Cancelled'
There is absolutely no reason to first select all the id's and then loop through them all deleting rows one at a time.
Mike Brant is exactly right; You should delete them without doing a lookup first. But if you wanted to access the id, you'd use:
// ... PREVIOUS
while ($dnn = mysql_fetch_array($req)) {
$delete_id = $dnn['id'];
$req1 = mysql_query("DELETE FROM booking_members WHERE id='".$delete_id."'");
// ... CONTINUE
while($dnn = mysql_fetch_array($req))
Where is the { ?
And here:
$req1 = mysql_query('DELETE FROM booking_members WHERE id='$id');
If you want the id, of last query, you must do: $dnn['id'] and not $id ( or you set $id before)...
And, for include a HTML script, in PHP script, you must use echo or print or must close the tags php ( ?> )
If you want to do a look up, and im assuming you are using dot net nuke, download the free reports module. After installing it edit it and type in the following select statement:
Select u.id,
u.username,
u.email,
b.start_date,
b.end_date,
b.time_event,
b.time_submitted,
b.payment_method,
b.status
FROM users as u
Join
booking_members as b on u.userid = b.userid
Where status="Canceled"'
this will show you the username, email, start_date, End_date, Time_event, Time_submitted, Payment_method and status of users where there status is canceled...

SELECT * FROM table_name ORDER BY column_name?

ok so i coded in a news section and everytime i insert new news,its shows below the old one.
i want to make it ORDER BY id but make it start like backwards.i dont know how to explain but yeh
i want them to be ordered by the newest added by the id so if the 1st row that was inserted's id is 1 then i want it to show below the next id.
so row with id = 2 will be here
so row with id = 1 will be here
thats how i want it to be, instead of it being like this
so row with id = 1 will be here
so row with id = 2 will be here
.
Sorry for my bad explanation i hope this is understandable
heres my code so far
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>
add DESC in your ORDER BY clause
SELECT * FROM news ORDER BY id DESC
by default, it is in ASC mode.
SELECT * FROM news ORDER BY id DESC
DESC is the descending keyword ASC is ascending
If you specify neither then default behaviour is ascending
Just use DESC keyword in your sql query.
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
However, it isn't really such a good idea to use id, because semantically, there is nothing preventing somebody from changing the sequence which counts up automatically assigning the ID.
Therefore, you should add a column created_at. Everytime you insert a row, you can use the SQL function NOW().
The advantage is that you can say:
SELECT * FROM news WHERE created_at <= NOW() ORDER BY created_at DESC
This means that you can schedule news items ahead of time, and it will automatically display when the date/time arrives!
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
try this:
just have to add
order by id DESC
Just replace your code by this code:
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>

Two results to one mysql statement

I've successfully managed to output the posts of a users friends...
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) ORDER BY `time` DESC");
This outputs all of the friends posts of the user. However I want to get the posts of the user itself AS WELL as the friends posts...this is what I have tried...
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) AND `user_id`=$session_user_id ORDER BY `time` DESC");
I even tried making another mysql statement just to load the users posts however when I did that, the users posts were being outputted first in the order of the time that they were outputted and then after the users posts were the friends posts shown...
Thanks :)
You actually want an OR condition:
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) OR `user_id`=$session_user_id ORDER BY `time` DESC");
Try:
$query = mysql_query("SELECT * FROM `users_posts` WHERE user_id IN ($friend) OR `user_id`=$session_user_id ORDER BY `time` DESC");
That will return anything where user_id is in your array; as well as the user's own ID.
If I understand correctly chwhat you are trying to do all you need do is change
And 'user_id = $session_iser_id
To
Or 'user_id = $session_iser_id

How to get a session value from a second table?

I have two tables for the users; a login table and the user profile table.
I want to compare a value from 'userprofiletable' to another value from another table called posts. If the value is equal, it shows a list.
I have the following code. The problem is that it is not comparing the value in the posts table with the value of the session from user profile table.
Could someone help me please?
<?php
$limit = '5';
$dbreq = 'SELECT * FROM `posts` ORDER BY `pos` DESC';
$dbdata = mysql_query($dbreq);
while($dbval = mysql_fetch_array($dbdata))
{
if (($dbval['city'] == $_SESSION['student_city'])) { //checks for last 4 accomodation
if ($limit >= '1') {
echo '<tr><td>'.$dbval['title'].'</td></tr>';
$limit = $limit -'1';
}
}
}
?>
I also want to get the value of userprofiletable and post it in the posts table. For example, when somebody make a new post.
Your post is a bit unclear, but I think this is what you want:
<?php
$userid = 11542;//Sample uid. You will have to figure this out and set it.
$limit = 5;
$dbreq = "SELECT * FROM `posts` WHERE `userid`=".$userid." ORDER BY `pos` DESC LIMIT=".$limit.";";
$dbdata = mysql_query($dbreq);
while($dbval = mysql_fetch_array($dbdata))
{
if (($dbval['city'] == $_SESSION['student_city'])) { //checks for last 4 accomodation
echo '<tr><td>'.$dbval['title'].'</td></tr>';
}
}
?>
The question is not clear, but there could be two answers:
To reproduce your code, you can do in ONE sql query:
$dbreq = 'SELECT *
FROM `posts`
WHERE city="'.mysql_real_escape_string($_SESSION['student_city']).'"
ORDER BY `pos` DESC
LIMIT 4';
If, however, there are two tables, then you need "LEFT JOIN" linking the posts table to the userprofile table
$dbreq = 'SELECT p.*, u.*
FROM posts p
LEFT JOIN userprofiletable up ON p.UserID=up.UserID
WHERE up.city="'.mysql_real_escape_string($_SESSION['student_city']).'"
ORDER BY p.pos DESC
LIMIT 4';
(UserID in the table above is the name of the field in the posts table and userprofiletable that links the two.)

Categories