Help with code please

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1663 Views - Last Post: 30 November 2010 - 07:36 PM Rate Topic: -----

#1 indysolo621   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Help with code please

Posted 29 November 2010 - 03:40 PM

I need help with this code it needs to get stalker,and stalking from friend in database. But only needs to do this where stalker=$_SESSION[username] . and then needs to show messages by all stalking . Message data is message,at,username from message . I have a code so far but cant seem to get it to work completley. It is only showing the first user the person is stalking instead of all of them. (stalking is like following on twitter). Help is much appreciated and I will post more info if I didnt state everything I needed to. Here is the code I have so far.
Thanks in advance :)


<?php
session_start();
require "header.php";
require_once("connect.php");
{
// Check his status.
{
print "<center><a href='members.php'>Find Members to Stalk</a></center>";
echo "		<form action=\"stalksuccess.php?\" method=\"post\">\n"; 
echo "			\n"; 
echo "				<center>Stalk</center><br/>\n"; 
echo "				\n"; 
echo "				<center><label>Who to Stalk? <input type=\"text\" name=\"stalk\" size=\"25\" value=\"$_POST[stalk]\"><input type=\"submit\" value=\"Stalk!\"></center><br/>\n";
echo "		</form>\n"; 
if (!empty($_SESSION[username])) // he got it.
{
{
print "<center><table width=75% border=0><th>Who I'm Stalking</th>";
}
$Members = mysql_query("SELECT stalking FROM friend WHERE stalker='$_SESSION[username]'") or die(mysql_error());
$name = mysql_fetch_array($Members);
{
echo'<tr><td><center><a href="member_profile.php?username=' . $name['stalking'] . '">'.$name['stalking'].' </a></center></td></tr>';
print "</table></center>";
}
print "<center><table width=75% border=0><th>Who Is Stalking Me?</th>";
$Members1 = mysql_query("SELECT stalking FROM friend WHERE stalker='$username'") or die(mysql_error());
$name1 = mysql_fetch_array($Members1);
{
echo'<tr><td><center><a href="member_profile.php?username=' . $name1['stalker'] . '">'.$name1['stalker'].' </a></center></td></tr>';
}
{
print "</table></center>";
}
}

if (!($_SESSION[username])) // bad info.
{
	echo "<center>You are currently <b>NOT</b> logged in. Please login. </center>";
	echo "<meta http-equiv=\"REFRESH\" content=\"0;url=login.php\">\n";
	}
require"footer.php";
}
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Help with code please

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Help with code please

Posted 29 November 2010 - 03:55 PM

You're only getting one result because that's all you're actually fetching. Please read this tutorial for more information.
Was This Post Helpful? 0
  • +
  • -

#3 indysolo621   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with code please

Posted 29 November 2010 - 04:09 PM

hmmmm, thanks for the help. I tried adding what would do it while()? and it didnt work but I have the wrong code at the beggening of the topic so I updated it, some here it is.

<?php

session_start();
require "header.php";
require_once("connect.php");
$stalking=$name['stalking'];
{
// Check his status.
{
print "<center><a href='members.php'>Find Members to Stalk</a></center>";
echo "		<form action=\"stalksuccess.php?\" method=\"post\">\n"; 
echo "			\n"; 
echo "				<center>Stalk</center><br/>\n"; 
echo "				\n"; 
echo "				<center><label>Who to Stalk? <input type=\"text\" name=\"stalk\" size=\"25\" value=\"$_POST[stalk]\"><input type=\"submit\" value=\"Stalk!\"></center><br/>\n";
echo "		</form>\n"; 
if (!empty($_SESSION[username])) // he got it.
{
print "<center><table width=75% border=0><th>Who I'm Stalking</th>";
{
$Members = mysql_query("SELECT stalking FROM friend WHERE stalker='$_SESSION[username]'") or die(mysql_error());
$name = mysql_fetch_array($Members);
}
echo'<tr><td><center><a href="member_profile.php?username=' . $name['stalking'] . '">'.$name['stalking'].' </a></center></td></tr>';
}
print "</table></center>";
{
print "<center><table width=75% border=0><th>Who Is Stalking Me?</th>";
{
$Members1 = mysql_query("SELECT stalking FROM friend WHERE stalker='$_SESSION[username]'") or die(mysql_error());
$name1 = mysql_fetch_array($Members1);
}
echo'<tr><td><center><a href="member_profile.php?username=' . $name1['stalker'] . '">'.$name1['stalker'].' </a></center></td></tr>';
}
print "</table></center>";
print "<center><table width=75% border=0><th>Username</th> <th>Message</th></center>";
$Data2= mysql_query("SELECT `at`,`message` FROM `message` where `username`='$stalking' ORDER BY `id` DESC LIMIT 10") or die(mysql_error());
while ($Data3 = mysql_fetch_array($Data2))
{
echo'<center><tr><td><center><a href="member_profile.php?username=' . $Data3['username'] . '">'.$Data3['username'].' </a></center></td>';
print "<td><font color=\"#afd2e6\"><center>{$Data3['message']}</center></font></td></tr></center>";
}
print "</table>";
}

if (!($_SESSION[username])) // bad info.
{
	echo "<center>You are currently <b>NOT</b> logged in. Please login. </center>";
	echo "<meta http-equiv=\"REFRESH\" content=\"0;url=login.php\">\n";
	}
require"footer.php";
}
?>

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with code please

Posted 29 November 2010 - 04:21 PM

You don't say what the error is, but this line looks wrong to me:

$Members1 = mysql_query("SELECT stalking FROM friend WHERE stalker='$_SESSION[username]'") or die(mysql_error());



Your indenting and use of braces is painful to see. I've cleaned it up a bit and fixed that line:
<?php
session_start();
require "header.php";
require_once("connect.php");
$stalking=$name['stalking'];
// Check his status.
print "<center><a href='members.php'>Find Members to Stalk</a></center>";
echo "		<form action=\"stalksuccess.php?\" method=\"post\">\n"; 
echo "			\n"; 
echo "				<center>Stalk</center><br/>\n"; 
echo "				\n"; 
echo "				<center><label>Who to Stalk? <input type=\"text\" name=\"stalk\" size=\"25\" value=\"$_POST[stalk]\"><input type=\"submit\" value=\"Stalk!\"></center><br/>\n";
echo "		</form>\n"; 
if (!empty($_SESSION['username'])) // he got it.
{
	print "<center><table width=75% border=0><th>Who I'm Stalking</th>";
	$Members = mysql_query("SELECT stalking FROM friend WHERE stalker='$_SESSION[username]'") or die(mysql_error());
	$name = mysql_fetch_array($Members);
}
echo'<tr><td><center><a href="member_profile.php?username=' . $name['stalking'] . '">'.$name['stalking'].' </a></center></td></tr>';
print "</table></center>";

print "<center><table width=75% border=0><th>Who Is Stalking Me?</th>";
$query = "SELECT stalking FROM friend WHERE stalker='".$_SESSION['username']."'";
$Members1 = mysql_query($query) or die(mysql_error());
$name1 = mysql_fetch_array($Members1);
echo'<tr><td><center><a href="member_profile.php?username=' . $name1['stalker'] . '">'.$name1['stalker'].' </a></center></td></tr>';

print "</table></center>";
print "<center><table width=75% border=0><th>Username</th> <th>Message</th></center>";
$Data2= mysql_query("SELECT `at`,`message` FROM `message` where `username`='$stalking' ORDER BY `id` DESC LIMIT 10") or die(mysql_error());
while ($Data3 = mysql_fetch_array($Data2))
{
	echo'<center><tr><td><center><a href="member_profile.php?username=' . $Data3['username'] . '">'.$Data3['username'].' </a></center></td>';
	print "<td><font color=\"#afd2e6\"><center>{$Data3['message']}</center></font></td></tr></center>";
}
print "</table>";

if (!($_SESSION['username'])) // bad info.
{
	echo "<center>You are currently <b>NOT</b> logged in. Please login. </center>";
	echo "<meta http-equiv=\"REFRESH\" content=\"0;url=login.php\">\n";
}
require"footer.php";
?>

Was This Post Helpful? 0
  • +
  • -

#5 indysolo621   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with code please

Posted 29 November 2010 - 04:35 PM

thank you for that :) it got everything working in the code, the only problem is it is only showing 1 of each thing. Like only shows 1 person you are stalking and 1 person who is stalking you and 1 message. how do I fix that?
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with code please

Posted 29 November 2010 - 04:51 PM

You've got the while loop on the wrong query.
Was This Post Helpful? 0
  • +
  • -

#7 indysolo621   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with code please

Posted 29 November 2010 - 04:53 PM

where should it be on each one?
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with code please

Posted 29 November 2010 - 05:12 PM

Did you write this code? This forum is for people who want to improve their programming skills.
Was This Post Helpful? 0
  • +
  • -

#9 indysolo621   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with code please

Posted 29 November 2010 - 05:42 PM

yes I did, sorry if I am asking to much. Just trying to get my code to work, I tried switching the while but when I do it takes all the content out. Sorry, not trying to upset you. Ah nevermind I found it I had the while right on the edited version just accidently added a ';' to the end. Thanks for all the help :)

This post has been edited by indysolo621: 29 November 2010 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with code please

Posted 29 November 2010 - 05:46 PM

Well, I see three queries. Two of them are really only looking for one item/row, so they don't need a loop. Which query do you think should be looking for more than one row?
Was This Post Helpful? 0
  • +
  • -

#11 indysolo621   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with code please

Posted 29 November 2010 - 05:47 PM

I found what I had wrong, just accidently added a ; after the while() which made it not show and of the content from the mysql_query. Thank you :)
Was This Post Helpful? 0
  • +
  • -

#12 indysolo621   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with code please

Posted 29 November 2010 - 06:02 PM

just 1 more question. on line 30 I need to change $_SESSION[username] with $name['stalking'] But everytime I try to add $name['stalking'] it comes up with several difrent errors. Any idea on how to do this?

<?php
session_start();
require "header.php";
require_once("connect.php");
$stalking=$name['stalking'];
// Check his status.
print "<center><a href='members.php'>Find Members to Stalk</a></center>";
echo "		<form action=\"stalksuccess.php?\" method=\"post\">\n"; 
echo "			\n"; 
echo "				<center>Stalk</center><br/>\n"; 
echo "				\n"; 
echo "				<center><label>Who to Stalk? <input type=\"text\" name=\"stalk\" size=\"25\" value=\"$_POST[stalk]\"><input type=\"submit\" value=\"Stalk!\"></center><br/>\n";
echo "		</form>\n"; 
if (!empty($_SESSION['username'])) // he got it.
{
	print "<center><table width=75% border=0><th>Who I'm Stalking</th>";
	$Members = mysql_query("SELECT stalking FROM friend WHERE stalker='$_SESSION[username]'") or die(mysql_error());
	while($name = mysql_fetch_array($Members))
echo'<tr><td><center><a href="member_profile.php?username=' . $name['stalking'] . '">'.$name['stalking'].' </a></center></td></tr>';
print "</table></center>";

print "<center><table width=75% border=0><th>Who Is Stalking Me?</th>";
$Members1 = mysql_query("SELECT stalker FROM friend WHERE stalking='$_SESSION[username]'") or die(mysql_error());
while($name1 = mysql_fetch_array($Members1))
{
echo'<tr><td><center><a href="member_profile.php?username=' . $name1['stalker'] . '">'.$name1['stalker'].' </a></center></td></tr>';
}
print "</table></center>";
print "<center><table width=75% border=0><th>Username</th><th>@</th> <th>Message</th></center>";
$query2="SELECT `at`,`message`,`username`,`id` FROM `message` where `username`='".$_SESSION['username']."' ORDER BY `id` DESC LIMIT 10";
$Data2= mysql_query("$query2") or die(mysql_error());
while ($Data3 = mysql_fetch_array ($Data2))
{
	echo'<center><tr><td><center><a href="member_profile.php?username=' . $Data3['username'] . '">'.$Data3['username'].' </a></center></td>';
	echo'<center><td><center><a href="member_profile.php?username=' . $Data3['at'] . '">'.$Data3['at'].' </a></center></td>';
	print "<td><font color=\"#afd2e6\"><center>{$Data3['message']}</center></font></td></tr></center>";
}
print "</table>";
}
if (!($_SESSION['username'])) // bad info.
{
	echo "<center>You are currently <b>NOT</b> logged in. Please login. </center>";
	echo "<meta http-equiv=\"REFRESH\" content=\"0;url=login.php\">\n";
}
require"footer.php";
?>

This post has been edited by indysolo621: 29 November 2010 - 06:03 PM

Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with code please

Posted 29 November 2010 - 06:59 PM

The errors matter. You should always post errors. That's why they're mentioned in the rules.
The reason for using a variable inside a query is so that you can check it to see that it is being built the way you intended:

$query2="SELECT `at`,`message`,`username`,`id` FROM `message` where `username`='".$_SESSION['username']."' ORDER BY `id` DESC LIMIT 10";
echo $query2;
exit;
$Data2= mysql_query($query2) or die(mysql_error());


Was This Post Helpful? 0
  • +
  • -

#14 indysolo621   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with code please

Posted 29 November 2010 - 07:05 PM

well right now the error doesnt come up but I switch $_SESSION[username] with $name['stalking'] and it just doesnt show any messages when my username is stalking someone who has posted messages. So why wouldnt it show up?
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Help with code please

Posted 29 November 2010 - 07:18 PM

Did you try echoing the query? Was it built as you expected?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2