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>
Related
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);
}
}
}
}
}
?>
I tried applying sorting with paging but it is only sorting first page because the page number is one which is fine by me but the records of second and third page are not sorted.
Here is the FIRST PAGE CODE
include_once("session.php");
include_once("functions.php");
include_once("connection.php");
$username = $_SESSION["username"];
$orderby = "job_name";
$order = "asc";
//$conn2 = mysqli_connect("localhost","root","","jobportal");
//$conn3 = mysqli_connect("localhost","root","","jobportal");
//$conn4 = mysqli_connect("localhost","root","","jobportal");
$sql3 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=1";
$sql4 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=0";
$stmt3 = $conn->query($sql3);
$stmt4 = $conn->query($sql4);
$approved = $stmt3->fetch_array();
$pending = $stmt4->fetch_array();
$total_rows_approved = $approved[0];
$total_rows_pending = $pending[0];
$limit = 2;
$total_pages_apporved = ceil($total_rows_approved/$limit);
$total_pages_pending = ceil($total_rows_pending/$limit);
$page_approved = 0;
$page_pending = 0;
if(isset($_GET["page_approved"])){
$page_approved = $_GET["page_approved"];
}
if($page_approved){
$start_approved = ($page_approved-1)*$limit;
}
else{
$start_approved = 0;
}
if(isset($_GET["page_pending"])){
$page_pending = $_GET["page_pending"];
}
if($page_pending){
$start_pending = ($page_pending-1)*$limit;
}
else{
$start_pending = 0;
}
$sql1 = "select * from job_info where username='$username' and approved=1 order by ".$orderby." ".$order." limit $start_approved,$limit";
$stmt1 = $conn->query($sql1);
$sql2 = "select * from job_info where username='$username' and approved=0 order by ".$orderby." ".$order." limit $start_pending,$limit";
$stmt2 = $conn->query($sql2);
<script>
var page_no_approved=1;
var page_no_pending=1;
function order_column1(column_name,column_order){
$.ajax({
url: "get_job_list.php",
data: "orderby1="+column_name+"&order1="+column_order+"&page_no_approved="+page_no_approved,
type: "POST",
success: function(data){
$("#showresults").html(data);
}
});
}
function order_column2(column_name,column_order){
$.ajax({
url: "get_job_list.php",
data: "orderby2="+column_name+"&order2="+column_order+"&page_no_pending="+page_no_pending,
type: "POST",
success: function(data){
$("#showresults").html(data);
}
});
}
</script>
and here is the SECOND PAGE CODE(ajax)
include_once("session.php");
include_once("functions.php");
include_once("connection.php");
$username = $_SESSION["username"];
$sql3 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=1";
$sql4 = "SELECT COUNT(*) FROM job_info WHERE username='$username' AND approved=0";
$stmt3 = $conn->query($sql3);
$stmt4 = $conn->query($sql4);
$approved = $stmt3->fetch_array();
$pending = $stmt4->fetch_array();
$total_rows_approved = $approved[0];
$total_rows_pending = $pending[0];
$limit = 2;
$total_pages_apporved = ceil($total_rows_approved/$limit);
$total_pages_pending = ceil($total_rows_pending/$limit);
$page_approved = 0;
$page_pending = 0;
if(isset($_POST["page_no_approved"])){
$page_approved = $_POST["page_no_approved"];
}
if($page_approved){
$start_approved = ($page_approved-1)*$limit;
}
else{
$start_approved = 0;
}
if(isset($_POST["page_no_pending"])){
$page_pending = $_POST["page_no_pending"];
}
if($page_pending){
$start_pending = ($page_pending-1)*$limit;
}
else{
$start_pending = 0;
}
$orderby1 = "job_name";
$order1 = "asc";
$orderby2 = "job_name";
$order2 = "asc";
if(!empty($_POST["orderby1"])){
$orderby1 = $_POST["orderby1"];
}
if(!empty($_POST["order1"])){
$order1 = $_POST["order1"];
}
if(!empty($_POST["orderby2"])){
$orderby2 = $_POST["orderby2"];
}
if(!empty($_POST["order2"])){
$order2 = $_POST["order2"];
}
$date_order1 = "asc";
$job_name_order1 = "asc";
$industry_order1 = "asc";
$date_order2 = "asc";
$job_name_order2 = "asc";
$industry_order2 = "asc";
if($orderby1 == "date" && $order1=="asc"){
$date_order1 = "desc";
}
if($orderby1 == "job_name" && $order1=="asc"){
$job_name_order1 = "desc";
}
if($orderby1 == "industry" && $order1 == "asc"){
$industry_order1 = "desc";
}
if($orderby2 == "date" && $order2=="asc"){
$date_order2 = "desc";
}
if($orderby2 == "job_name" && $order2=="asc"){
$job_name_order2 = "desc";
}
if($orderby2 == "industry" && $order2 == "asc"){
$industry_order2 = "desc";
}
$sql1 = "select * from job_info where username='$username' and approved=1 order by ".$orderby1." ".$order1." limit $start_approved,$limit";
$result1 = $conn->query($sql1);
$sql2 = "select * from job_info where username='$username' and approved=0 order by ".$orderby2." ".$order2." limit $start_pending,$limit";
$result2 = $conn->query($sql2);
EDIT: I didn't included the code to display data because it will make the already large code to be more longer and possibly unreadable. It would help if someone could point me to the place where the concept of sorting with pagination is applied or if someone could explain the concept with small example.
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..
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 />";
}
I need help with this code, i need it to add an array in Gangs
under members and seperate them with -
I need every member in the Gang to be listed under Members and separated with -
so i can explode them below.
This is the line that adds the members to gangs but its without - and i think it erases members that are already there.
$result = mysql_query("UPDATE Gangs SET members='".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());
full code
if(isset($_POST['creategang'])){
if(empty($_POST['gang_name'])){
echo "Enter a Gang Name.";
} else {
if (strlen($_POST['gang_name']) > "20"){
echo "The username may not consist out of more then 20 characters.";
}else{
if (ereg('[^A-Za-z0-9]', $_POST['gang_name'])) {
echo "Invalid Name only A-Z,a-z and 0-9 is allowed.";
}else{
$sql = "SELECT name FROM Gangs WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$m_count = mysql_num_rows($query);
if($m_count >= "1"){
echo 'This name has already been used.!';
}else{
$sql = "INSERT INTO Gangs SET name = '".$_POST['gang_name']."' , owner= '$name'";
$res = mysql_query($sql);
$result = mysql_query("UPDATE users SET gang='".mysql_real_escape_string($_POST['gang_name'])."' WHERE id='" .mysql_real_escape_string($_SESSION['user_id']). "'")
or die(mysql_error());
$result = mysql_query("UPDATE Gangs SET members='".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());
echo 'Gang successfully created!';
}
}
}
}
}
?>
this is the code i will use to separate the array
$Gang_array = explode("-", $Gang_members);
Thanks for viewing my question and thanks in advance for helping me
new code to add to gang
<? include_once("connect.php"); ?>
<?
if(isset($_SESSION['user_id'])) {
// Login OK, update last active
$sql = "UPDATE users SET lastactive=NOW() WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
mysql_query($sql);
}else{
header("Location: index.php");
exit();
}
$sql = "SELECT * FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$userip = htmlspecialchars($row->userip);
$name = htmlspecialchars($row->name);
$sitestate = htmlspecialchars($row->sitestate);
$password = htmlspecialchars($row->password);
$mail = htmlspecialchars($row->mail);
$money = htmlspecialchars($row->money);
$exp = htmlspecialchars($row->exp);
$rank = htmlspecialchars($row->rank);
$health = htmlspecialchars($row->health);
$points = htmlspecialchars($row->points);
$profile = htmlspecialchars($row->profile);
$gang = htmlspecialchars($row->gang);
?>
<?php
$sql = "SELECT * FROM Gangs WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$Gang_name = htmlspecialchars($row->name);
$Gang_owner = htmlspecialchars($row->owner);
$Gang_money = htmlspecialchars($row->money);
$Gang_exp = htmlspecialchars($row->exp);
$Gang_level = htmlspecialchars($row->level);
$Gang_members = htmlspecialchars($row->members);
$Gang_array = explode("-", $Gang_members);
$Gang_profile = htmlspecialchars($row->profile);
?>
<div id="content" class="profile">
<h2>Gang Profile</h2>
<form method="post" >
<input type="submit" name="Petition" id="Petition" value="Petition">
</form>
<center>
<h1><?php echo $Gang_name; ?></h1>
Owner: <?php echo $Gang_owner; ?><br>
Gang Cash: $<?php echo $Gang_money; ?><br>
Gang Exp: <?php echo $Gang_exp; ?><br>
Gang Level: <?php echo $Gang_level; ?><br>
Gang Members: <?php echo $Gang_array; ?><br>
</center><br>
<p>Gang Quote</p>
<div id="UserText">
<?php
$Gang_profile = htmlentities($Gang_profile);
$Gang_profile = nl2br($Gang_profile);
$Gang_profile = stripslashes($Gang_profile);
echo $Gang_profile; ?>
</div>
</div>
<?
if (isset($_POST['Petition'])) {
$result = mysql_query("SELECT members FROM Gangs
WHERE name='".$Gang_name."'");
if ($result) {
while($row = mysql_fetch_assoc($result)) {
$members = $row['members'];
}
}
if ($members != '') $members .= '-'.$name;
else $members = $name;
$result = mysql_query("UPDATE Gangs SET members='".$members."' WHERE name='".$Gang_name."'");
}
If I understand your question correctly. You can try this :
Get the members first :
$result = mysql_query("SELECT members FROM Gangs
WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'");
if ($result) {
while($row = mysql_fetch_assoc($result)) {
$members = $row['members'];
}
}
Then add new member and do update :
//This is to check whether $name is already in the gangs
if (strpos($members,$name) !== false) {
if ($members != '') $members .= '-'.$name;
else $members = $name;
//Update to gangs
$result = mysql_query("UPDATE Gangs SET members='".$members."' WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'");
}
else sprintf("%s is in the gangs already",$name);
Hope it helps.
Pseudo code, but this should probably work:
$result = mysql_query("UPDATE Gangs SET members = members + '-' + '".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());