php home posts system using Mysql - php

This is my code to get there posts on their profile
<?php
$getposts = mysql_query("SELECT * FROM videos_content WHERE username ='$username' ORDER BY id DESC LIMIT 10") or die(mysql_error());
while ($row = mysql_fetch_assoc($getposts)) {
$id = $row['id'];
$video_title = $row['video_title'];
$video_url = $row['video_url'];
$date_posted = $row['date_posted'];
$added_by = $row['username'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$added_by'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./image/default_pic.jpg";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
?>
My sql user table is like this
[id,username,followers,following,account_active]
-> My following and followers columns are arrays with comma separated values.
My sql posts table is like this
[id,username,video_title,video_url,date_posted,removed]
I want to display posts of people whom a user(i mean a logged in user) is following on his/her home page.
can anyone suggest me some solution ?
Thanks.
I tried this code
<?php
$followingArray = "";
$countFollowing = "";
$followingArray12 = "";
$selectFollowingQuery = mysql_query("SELECT following FROM users WHERE username = '$user' ");
$followingRow = mysql_fetch_assoc($selectFollowingQuery);
$followingArray = $followingRow['following'];
if ($followingArray != "") {
$followingArray = explode(",","$followingArray");
$countFollowing = count($followingArray);
$followingArray12 = array_slice($followingArray, 0, 12);
$getposts = mysql_query("SELECT * FROM videos_content WHERE username ='$username' ORDER BY id DESC LIMIT 10") or die(mysql_error());
while ($row = mysql_fetch_assoc($getposts)) {
$id = $row['id'];
$video_title = $row['video_title'];
$video_url = $row['video_url'];
$date_posted = $row['date_posted'];
$added_by = $row['username'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$added_by'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./image/default_pic.jpg";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
}
if (in_array($username, $followingArray) {
echo '(posts)'; /*html to echo those posts, its a very long chain of divs, that is the reason i didn't include that code.*/
}
else{
echo ''; /*nothing*/
}
}
?>

Firstly I would suggest splitting your users table into 3 tables.
users : [id,username,account_active]
followers : [id,followerId]
following : [id,followingId]
This will prevent columns with comma-separated values, and make your problems much simpler.
From there its easy:
SELECT *
FROM posts
WHERE id IN (
SELECT followerId
FROM followers
WHERE id = $userID
);
etc..

Related

prevent insert same id if the user/student not put timeout

i have two button on my homepage one is time-in and the other is time-out,
i want to prevent the user/student to time-in using same id if he did not put time-out on his last time-in to create valid entry. Hope you can help me.
here is my php code:
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn, "SELECT * FROM stud WHERE rfid_num = '$rfid'");
$count = mysqli_num_rows($sql);
if ($count == 0 ) {
header("location:notexist.php");
} elseif (empty($row['timeout'])) {
header("location:page say the user/student need to put timeout first before time-in again");
} else {
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
?>
this is my answer just wanna share it, i just add select student_att table
to fetch the data and check if timeout column is empty.
<?php
include_once('connection.php');
if(isset($_POST['submit0'])){
$rfid = $_POST['rfid'];
$time=date("H:i:s");
$sql = mysqli_query($conn,"select * from stud where rfid_num ='$rfid' ");
$count = mysqli_num_rows($sql);
if ($count == 0) {
header("location:notexist.php");
}else{
while( $row = mysqli_fetch_array($sql)) {
$rfid=$row['rfid_num'];
$id=$row['id'];
$name0 = $row['name'];
$course0 = $row['course'];
$image = $row['image'];
$sql1 = mysqli_query($conn,"select * from student_att where rfid_num ='$rfid' order by number DESC limit 1 ");
while( $row = mysqli_fetch_array($sql1)) {
if(empty($row['timeout'])){
header("location:logout.php");
}else{
$InsertSql = "INSERT INTO student_att(rfid_num,id,name,course,image,timein) VALUES ('$rfid','$id','$name0','$course0','$image','$time')";
$res = mysqli_query($conn, $InsertSql);
}
}
}
}
}
?>

PHP MYSQL Multiple If Statements for Multiple UPDATES

Can someone tell me what is wrong with these IF statements?
if(isset($_POST['submit']))
{
$dropship = $unitid['id'];
$jumpship = $_POST['jumpship'];
$dsdest = $_POST['planet'];
$dslz = $_POST['landingzone'];
$dsmission = $_POST['mission'];
$ds1 = mysql_query("SELECT id, ds1 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$ds2 = mysql_query("SELECT id, ds2 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$ds3 = mysql_query("SELECT id, ds3 FROM gc3025_game_jumpships WHERE `id`='$jumpship'");
$dist_loc_get2 = mysql_query("SELECT * FROM gc3025_dist_game WHERE `planet`='$dsdest' AND `districtid`='$dslz'");
$distloc2 = mysql_fetch_assoc($dist_loc_get2);
$newdist = $distloc2['g_district'];
$ds_name_get = mysql_query("SELECT * FROM gc3025_game_dropships WHERE `id`='$dropship'");
$ds_name = mysql_fetch_assoc($ds_name_get);
$dsname = $ds_name['unit_name'];
$dest_name_get = mysql_query("SELECT gc3025_planets_game.Game, gc3025_planets_game.owners, gc3025_planets_game.g_planet, gc3025_planets_game.Planet_id, gc3025_planets_id.planet_name FROM gc3025_planets_id JOIN gc3025_planets_game ON gc3025_planets_id.id = gc3025_planets_game.Planet_id WHERE `g_planet`='$dsdest'");
$dest_name = mysql_fetch_assoc($dest_name_get);
$destname = $dest_name['planet_name'];
$dsdz_name_get = mysql_query("Select gc3025_dist_game.districtid, gc3025_dist_game.g_district, gc3025_dist_labels.id, gc3025_dist_labels.dist_name FROM gc3025_dist_game JOIN gc3025_dist_labels ON gc3025_dist_game.districtid = gc3025_dist_labels.id WHERE `g_district`='$newdist'");
$dsdz_name = mysql_fetch_assoc($dsdz_name_get);
$dsdzname = $dsdz_name['dist_name'];
$dsmission_name_get = mysql_query("SELECT * FROM gc3025_movement_dropdowns WHERE `id`='$dsmission'");
$dsmission_name = mysql_fetch_assoc($dsmission_name_get);
$dsmissionname = $dsmission_name['mission_type'];
if ($ds1 == 0){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds1` = '$dsname', `ds1dest` = '$destname', `ds1dz` = '$dsdzname', `ds1mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds1 == 1){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds2` = '$dsname', `ds2dest` = '$destname', `ds2dz` = '$dsdzname', `ds2mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds2 == 1){
mysql_query ("UPDATE `gc3025_game_jumpships` SET `ds3` = '$dsname', `ds3dest` = '$destname', `ds3dz` = '$dsdzname', `ds3mission` = '$dsmissionname' WHERE `id`='$jumpship'");
}
if ($ds3 == 1){
echo "This Jumpship is Full!";
}
echo "<p>$dsname Loaded on $jumpship going to $destname and to complete $dsmissionname In District $dsdzname!</p>";
Hope this is enough.
Basically the table is for a jumpship that carries 3 dropships. I need the if statement to basically If ds1 has a dropship in it then the dropship will be entered to ds2 column. if there is a dropship in ds1 and ds2 then it will update ds3 column. if all three have dropships in them then the echo "this jumpship is full" will post.
Function mysql_query returns resource, and you must use mysql_fetch_assoc for data extraction. Besides, data inside the query should be properly escaped.
$ds_query = mysql_query(sprintf(
"SELECT ds1, ds2, ds3 FROM gc3025_game_jumpships WHERE `id`='%s'",
mysql_real_escape_string($jumpship)
));
$ds_result = mysql_fetch_assoc($ds_query);
if (!$ds_result['ds1'])
{
mysql_query(sprintf(
"UPDATE `gc3025_game_jumpships` " .
"SET `ds1` = '%s', `ds1dest` = '%s', `ds1dz` = '%s', `ds1mission` = '%s' " .
"WHERE `id`='%s'",
mysql_real_escape_string($dsname),
mysql_real_escape_string($destname),
mysql_real_escape_string($dsdzname),
mysql_real_escape_string($dsmissionname),
mysql_real_escape_string($jumpship)
));
}
elseif ($ds_result['ds1'])
{
// and so on
}
elseif ($ds_result['ds2'])
{
// and so on
}

Problems updating correct row in databse with php

I'm trying to create a voting system for artists played on my radio station. I'm using the source code from: http://dl.howcode.org/download/97ff383c7d4dc9939c65c9e6fab2a5dc
The problem I have found is that the votes update using the number from the first row in the database no matter which option is selected, thus if for instance the first row has 3 votes in and the user tries to vote on someone with 0 votes, it will change the votes for the correct artist to 4 instead of 1... I hope that makes sense?
The code I have is:
[EDIT] I have changed the queries to fetch assoc to make it easier to understand.
<?php
$voteID = $_GET['voteID'];
$connect = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
$query = "SELECT * FROM listenervotes WHERE voteID='$voteID'" ;
$q = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($q)){
$id = $row["id"];
$voteTitle = $row["voteTitle"];
$voteID = $row["voteID"];
$ipaddress = $row["ipAddress"];
echo "<h3>$voteTitle</h3>";
?>
<table>
<form action="" method="POST">
<?php
$artists = "SELECT * FROM artists WHERE voteID='$voteID'" ;
$q2 = mysqli_query($connect, $artists);
while($r = mysqli_fetch_assoc($q2)){
$artist = $r["artistName"];
$votes = $r["votes"];
$genre = $r["genre"];
$ip = $_SERVER['REMOTE_ADDR'];
$newIpAddress = $ipaddress."$ip, ";
$newVotes = $votes + 1;
if (isset($_POST['vote'])) {
$voteOption = $_POST['voteOption'];
if ($voteOption == ""){
die("You haven't selected anyone!");
}else{
$ipaddressE = explode(",", $ipaddress);
if(in_array($ip, $ipaddressE)){
die("You have already voted!");
}else{
mysqli_query($connect, "UPDATE artists SET votes='$newVotes' WHERE voteID='$voteID' AND artistName='$voteOption'");
mysqli_query($connect, "UPDATE listenervotes SET ipaddress='$newIpAddress' WHERE voteID='$voteID'");
die('You voted successfully!<br><tr><td>'.$artist.'</td><td>'.$genre.'</td><td>'.$votes.' Votes</td></tr>');
}
}
}
echo '<tr><td>'.$artist.'</td><td>'.$genre.'</td><td><input type="radio" name="voteOption" value="'.$artist.'"</td></tr>';
}
}
?>
I could be missing something obvious, in my mind I'm thinking that I somehow need to iterate through the rows before setting the new value, if so, how and where?
It looks like you are always looping over all rows and updating the relevant row with the first value found. Adding a check on the ID should do:
<?php
$voteID = $_GET['voteID'];
$connect = mysqli_connect('xxx', 'xxx', 'xxx', 'xxx');
$query = "SELECT * FROM listenervotes WHERE voteID='$voteID'" ;
$q = mysqli_query($connect, $query);
while($row = mysqli_fetch_assoc($q)){
$id = $row["id"];
$voteTitle = $row["voteTitle"];
$voteID = $row["voteID"];
$ipaddress = $row["ipAddress"];
echo "<h3>$voteTitle</h3>";
?>
<table>
<form action="" method="POST">
<?php
$artists = "SELECT * FROM artists WHERE voteID='$voteID'" ;
$q2 = mysqli_query($connect, $artists);
while($r = mysqli_fetch_assoc($q2)){
$artist = $r["artistName"];
$votes = $r["votes"];
$genre = $r["genre"];
$ip = $_SERVER['REMOTE_ADDR'];
$newIpAddress = $ipaddress."$ip, ";
$newVotes = $votes + 1;
if (isset($_POST['vote'])) {
$voteOption = $_POST['voteOption'];
if ($voteOption == ""){
die("You haven't selected anyone!");
}else{
$ipaddressE = explode(",", $ipaddress);
if(in_array($ip, $ipaddressE)){
die("You have already voted!");
}elseif ($voteOption === $artist) { // Don't run UPDATE when we're on the wrong row.
mysqli_query($connect, "UPDATE artists SET votes='$newVotes' WHERE voteID='$voteID' AND artistName='$voteOption'");
mysqli_query($connect, "UPDATE listenervotes SET ipaddress='$newIpAddress' WHERE voteID='$voteID'");
die('You voted successfully!<br><tr><td>'.$artist.'</td><td>'.$genre.'</td><td>'.$votes.' Votes</td></tr>');
}
}
}
echo '<tr><td>'.$artist.'</td><td>'.$genre.'</td><td><input type="radio" name="voteOption" value="'.$artist.'"</td></tr>';
}
}
?>

How to check for 2 variables in a single mysql_query

Need some help in php mysql
suppose i am logged in as $active_username and I want to see all the messages between me($active_username) and my friend($username) from a table pvt_message, where i have columns(pid,user_from,user_to,body).
I tried using below code:
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE user_from='$active_username' && user_to='$username' ORDER BY id ASC ") or die(mysql_error());
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$active_username'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
rest are the code to show data fetched on table, there is no error in that,so i didnt post.
this above code fetched me only those message i had sent to my friend.
again i used below code instead of above one:
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE user_from='$active_username' && user_to='$username' ORDER BY id ASC ") or die(mysql_error());
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$active_username'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
n now it fetched me all those message he/she sent me.
I want to view message we both sent each other, in asending order of the primary key of the table.
so i tried using:
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE (user_from='$active_username' || user_from='$username') && (user_to='$username' || user_to='$active_username') ORDER BY id ASC ") or die(mysql_error());
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$get_user_info = mysql_query("SELECT * FROM users WHERE username='$active_username' || username ='$username'");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
how to get those data in table??? where user_from can be both me($active_username) or he($username) and user_to can be both me($active_username) or he ($username).
Thankyou in advance :)
Try it
SELECT * FROM pvt_messages WHERE user_from IN ('$active_username', '$username') AND user_to IN ('$active_username', '$username')
It will work if not have messages in DB from active_username to active_username
just change your query like below
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE ((user_from='$active_username' && user_to='$username') || (user_from='$username' && user_to='$active_username')) ORDER BY id ASC ") or die(mysql_error());
Put the below code before fetching from the user table and pass the $qry variable in the mysql_query function.
if($['user_from'] == $active_username)
{
$qry = "SELECT * FROM users WHERE username ='$active_username'";
}
else
{
$qry ="SELECT * FROM users WHERE username ='$username'";
}
$get_user_info = mysql_query($qry);
I was able to get messages sequentially between my friend and me.... on basis of pid of the table pvt message using
$getmessages= mysql_query("SELECT * FROM pvt_messages WHERE (user_from='$active_username' && user_to='$username') || (user_from='$username' && user_to='$active_username') ORDER BY id ASC ") or die(mysql_error());
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$get_user_info = mysql_query("SELECT * FROM users WHERE (username='$active_username' || username='$username')");
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
echo "
<div style='float: left;padding-top:5px;'>
<img src='$profilepic_info' height='20' width='20'>
</div>
<div style='padding-left:20px;padding-top:5px;'>
<a href='$active_username'>Me:</a> $body
</div></br>
<div style=' margin-left:80px;'>
<br />
</div>
<hr />
";
............. but now while i am fetching the user datas , to show name of user and profile pic too, only my pic is beling displayed in both my messages and my friend messages.
there is something wrong in line:
$get_user_info = mysql_query("SELECT * FROM users WHERE (username='$active_username' || username='$username')");
Thankyou everyone, for suggestion. I had to change a little bit, to get result I wanted.
while ($row = mysql_fetch_assoc($getmessages ))
{
$body = $row['msg_body'];
$date_send = $row['date'];
$opened = $row['opened'];
$whosent= $row['user_from'];
$whoreceived= $row['user_to'];
$seen=$row['opened'];
/////////////////////////////////////////////////////
$get_user_info = mysql_query("SELECT * FROM users WHERE (username='$whosent')");
////////////////////////////////////////////////////
$get_info = mysql_fetch_assoc($get_user_info);
$profilepic_info = $get_info['profile_pic'];
if ($profilepic_info == "") {
$profilepic_info = "./images/default_pic.jpg";
}
else
{
$profilepic_info = "./userdata/profile_pics/".$profilepic_info;
}
echo "
<div style='float: left;padding-top:5px; padding-left:20px;'>
<img src='$profilepic_info' height='50' width='40'>
</div>
<div style='margin-left:50px;padding-top:5px;'>
<a href='$active_username' style='color:black'> $whosent</a> $body
</div></br>
<div style=' margin-left:30px;'>
<font size='2px'>sent on: $date_send</font>

Why does my code only show one post?

I have been developing a social network, and a key function is to be able to post on other users' profiles. However, my current code will only show one post. Also, it seems to be the first post that is shown. I have tested this by creating new accounts, and writing a test post. The code does show this first post, but if I try it again, only the first post is visible. The code is as follows:
The code to send post to database:
$person = "profile.php?id={$id}";
$post = $_POST['post'];
if($post != "")
{
$data_added = date("Y-m-d");
$added_by = $session_username;
$user_posted_to = $id;
$post = preg_replace("#[^0-9a-z]#i", "", $post);
$sqlCommand = "INSERT INTO posts VALUES ('',
'$post',
'$data_added',
'$added_by',
'$user_posted_to')";
$commandQuery = mysql_query($sqlCommand) or die ("Couldn't send post");
}
else
{
echo "You have to fill in the post form...";
}
The code to retrieve it (and display it):
$getPosts = mysql_query("SELECT *
FROM posts
WHERE user_posted_to='$id'
ORDER BY id DESC LIMIT 15") or die("Couldn't find any posts");
while($row = mysql_fetch_array($getPosts))
{
$id = $row['id'];
$body = $row['body'];
$date_added = $row['date_added'];
$added_by = $row['added_by'];
$user_posted_to = $row['user_posted_to'];
$querya = mysql_query("SELECT *
FROM members
WHERE username='$added_by' LIMIT 1");
while($row = mysql_fetch_array($querya))
{
$user_added = $row['id'];
}
$user_added = "profile.php?id={$user_added}";
}
echo "
<div>
<h3><a href='$user_added'>$added_by</a> - $date_added </h3>
<p> $body</p>
</div><br />
";
If anyone needs some more of the code, like my database connection, just comment.
In your while cicle you fill in some variables but you do not use them.
You use echo only outside the cycle, so just once, and thus you print only the values of the last istance of the while cycle.
Try
$getPosts = mysql_query("SELECT * FROM posts WHERE user_posted_to='$id' ORDER BY id DESC LIMIT 15") or die("Couldn't find any posts");
while($row = mysql_fetch_array($getPosts)){
$id = $row['id'];
$body = $row['body'];
$date_added = $row['date_added'];
$added_by = $row['added_by'];
$user_posted_to = $row['user_posted_to'];
$querya = mysql_query("SELECT * FROM members WHERE username='$added_by' LIMIT 1");
while($row = mysql_fetch_array($querya)){
$user_added = $row['id'];
}
$user_added = "profile.php?id={$user_added}";
echo "
<div><h3><a href='$user_added'>$added_by</a> - $date_added </h3><p> $body</p></div><br />";
}

Categories