can get my "add friends" script to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
runnerjp
Forum Newbie
Posts: 3
Joined: Sun May 11, 2008 5:51 am

can get my "add friends" script to work

Post by runnerjp »

ok so in my table i currenty have
________________________
| username | by_user |
|------------------------- |
| Emma | Bob |

but im getting the error

QUERY ERROR:
SELECT * FROM `friend_requests` WHERE `by_user` = Bob
Unknown column 'Bob' in 'where clause'

but bob is right there lol... and surly if it cant find bob it should display the oage still

PHP Code:

Code: Select all

<?php
session_start(); // starts sessions
include "../settings.php"; // inlcudes config
 
switch ($_GET[friends]) { //allows multiple pages
default:
 $id = $_SESSION['user_id'];
 $get_username_value = get_username($id);
$get = "SELECT * FROM `friend_requests` WHERE `by_user` = $get_username_value "; 
$result= mysql_query($get)     or die("QUERY ERROR:<br />{$get}<br />" . mysql_error() ); //gets requests
while ($reqs = mysql_fetch_array($get))
{
 
echo ( "Friend Requests
$reqs[by_user] wants to be friends with you.
<a href='newfriends.php?friends=accept&user=$reqs[by_user]'>Accept</a><br/>
<a href='newfriends.php?friends=delete&user=$reqs[by_user]'>Delete</a>" ); //displays requests and shows accept delete links
}
break;
 
case 'accept': //accept page
if ($_GET[user]) { //get username
 $get_username_value = get_username($id);
$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$get_username_value') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user] has been added as a friend and the request has been deleted" ); // echos the completion
}
break; //ends accept page
 
case 'delete': // delete page
if ($_GET[user]) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user]'s request has been deleted" ); // echos completion
}
break; //ends delete page
} // ends switch
 
?>
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: can get my "add friends" script to work

Post by EverLearning »

Put quotes around the variables in you query:

Code: Select all

$get = "SELECT * FROM `friend_requests` WHERE `by_user` = '$get_username_value'";
because unquoted strings are considered to be field names. That's why you got the error
Unknown column 'Bob' in 'where clause'
runnerjp
Forum Newbie
Posts: 3
Joined: Sun May 11, 2008 5:51 am

Re: can get my "add friends" script to work

Post by runnerjp »

Code: Select all

<?php
session_start(); // starts sessions
include "../settings.php"; // inlcudes config
 
switch ($_GET[friends]) { //allows multiple pages
default:
 $id = $_SESSION['user_id'];
 $get_username_value = get_username($id);
 $get = "SELECT * FROM `friend_requests` WHERE `by_user` = '$get_username_value'";
$result= mysql_query($get)     or die("QUERY ERROR:<br />{$get}<br />" . mysql_error() ); //gets requests
 
{
 
echo ( "Friend Requests
$reqs[username] wants to be friends with you.
<a href='newfriends.php?friends=accept&user=$reqs[username]'>Accept</a><br/>
<a href='newfriends.php?friends=delete&user=$reqs[namername]'>Delete</a>" ); //displays requests and shows accept delete links
}
break;
 
case 'accept': //accept page
if ($_GET[user]) { //get username
 $get_username_value = get_username($id);
$add = mysql_query( "INSERT INTO `friends` (`friendname` , `username`) VALUES ('$_GET[user]' , '$get_username_value') "); // add to your friends list
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user] has been added as a friend and the request has been deleted" ); // echos the completion
}
break; //ends accept page
 
case 'delete': // delete page
if ($_GET[user]) { //gets username
$delete = mysql_query( "DELETE FROM `friend_requests` WHERE `by_user` = '$_GET[user]' "); // deletes friend request
echo ( "$_GET[user]'s request has been deleted" ); // echos completion
}
break; //ends delete page
} // ends switch
 
?>

now i im stuck,,, need some advie lol. ok the code doents pull the username of the person that wants to be accepted as a friend

i just get /members/newfriends.php?friends=accept&user=??
how would i pull the user name out so i can have

Name wants to be friends with you. Accept or Delete

and how would you go about making it so that a message is displayed when there is a user waiting to become a friend?

sorry i forgot to think this part through
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: can get my "add friends" script to work

Post by califdon »

runnerjp wrote:

Code: Select all

sorry i forgot to think this part through[/quote]
Well, this is what programming is all about: thinking a project through and developing the logic that you need to follow. It's pretty hard to help you with something so fundamental.
Post Reply