Jump to content

Invite friend code


Greenbox

Recommended Posts

Hi, im trying to make a code where you can send a friend invite to other members and be stored in a page called friends.php,  i'm not the best coder but i think im getting alittle hang on it :P of anyone could help me out alittle then it would been great :) just ask if you need any information about the db, Thanks in advance ;)

 

<?php
session_start();
include "includes/db_connect.php";
include "includes/functions.php";

$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM users WHERE username='$username'");


$fetch=mysql_fetch_object($query);
$inviter = mysql_fetch_object(mysql_query("SELECT * FROM users WHERE username='$username'"));

if (strip_tags($_POST['invite_button']) && strip_tags($_POST['invite_person'])){
$invite_person=strip_tags($_POST['invite_person']);
if (strtolower($invite_person) == strtolower($username)){
echo "You cannot invite yourself";
}elseif (strtolower($invite_person) != strtolower($username)){

$invite_text="<form name=invite_friend method=post action=?accept=friends&id=$inviter->id>
  <div align=center>You have been invited to be $username s friend!<br>
    <input name=Decline type=submit id=Decline value=Decline>
    | 
    <input name=Accept type=submit id=Accept value=Accept>
  </div>
</form>";

mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` ) 
VALUES (
'', '$invite_person', '$invite_person', '$invite_text', '$date', '0'
)");
mysql_query("UPDATE friend SET person ='$invite_person' WHERE id='$inviter->id'");

echo "Invite has been sent to <a href='profile.php?viewuser=$invite_person'><b>$invite_person</b></a>";
}}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<div align="center">
                <input name="invite_person" type="text" id="invite_person" />
                <input name="invite_button" type="submit"  id="invite_button" value="Invite" />
                </div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/202745-invite-friend-code/
Share on other sites

<?php
session_start();
include "includes/db_connect.php";
include "includes/functions.php";

$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM users WHERE username='$username'");

$fetch=mysql_fetch_object($query);
$inviter = mysql_fetch_object(mysql_query("SELECT * FROM users WHERE username='$username'"));

// if (strip_tags($_POST['invite_button']) && strip_tags($_POST['invite_person'])){
if (isset($_POST['invite_button']) && isset($_POST['invite_person)) {
$invite_person=strip_tags($_POST['invite_person']);

if (strtolower($invite_person) == strtolower($username)){
	echo "You cannot invite yourself";
}elseif (strtolower($invite_person) != strtolower($username)){
	$invite_text="<form name=invite_friend method=post action=?accept=friends&id=$inviter->id>
		<div align=center>You have been invited to be $username s friend!<br>
			<input name=Decline type=submit id=Decline value=Decline>
			| 
			<input name=Accept type=submit id=Accept value=Accept>
		</div>
	</form>";

	mysql_query("INSERT INTO `inbox` ( `id` , `to` , `from` , `message` , `date` , `read` ) 
	VALUES ('', '$invite_person', '$invite_person', '$invite_text', '$date', '0')") or trigger_error("Mysql Update Inbox Failed: " . mysql_error());
	mysql_query("UPDATE friend SET person ='$invite_person' WHERE id='$inviter->id'") or trigger_error("Mysql Update Failed Friend: " . mysql_error());
	echo "Invite has been sent to <a href='profile.php?viewuser=$invite_person'><b>$invite_person</b></a>";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<div align="center">
                <input name="invite_person" type="text" id="invite_person" />
                <input name="invite_button" type="submit"  id="invite_button" value="Invite" />
                </div>
</body>
</html>

 

A. Use proper indentiation. Makes code easier to read.

B. checking for "strip_tags" of a field is not correct, as all that has to do is strip the tags, which will normaly always return true. Better to check isset to see if the variable has been set.

C. Added error checking so you can see if the queries are failing for some reason.

 

The above "should work" or at least return a helpful error.

Link to comment
https://forums.phpfreaks.com/topic/202745-invite-friend-code/#findComment-1062788
Share on other sites

Ok, the addfriend page is finished i think.. but how i can make it so when the user that have been invited goes into his inbox and click the accept button it updates the db field and add id, username, person? anyone who could tech me how i can do it? thanks :thumb-up:

 

<?php
session_start();
include "includes/db_connect.php";
include "includes/functions.php";

$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM users WHERE username='$username'");

$fetch=mysql_fetch_object($query);
$inviter = mysql_fetch_object(mysql_query("SELECT * FROM users WHERE username='$username'"));

// if (strip_tags($_POST['invite_button']) && strip_tags($_POST['invite_person'])){
if (isset($_POST['invite_button']) && isset($_POST['invite_person'])) {
  $invite_person=strip_tags($_POST['invite_person']);
											   
   if (strtolower($invite_person) == strtolower($username)){
      echo "You cannot invite yourself";
   }elseif (strtolower($invite_person) != strtolower($username)){
   $invite_title="You have a friend invitation";
      $invite_text="<form name=invite_friend method=post action=?accept=friends&friends_id=$inviter->id>
         <div align=center>You have been invited to be $username s friend!<br>
            <input name='Decline' type='submit' id='Decline' value='Decline'>
            | 
            <input name='Accept_friend' type='submit' id='Accept' value='Accept'>
         </div>
      </form>";

      mysql_query("INSERT INTO `inbox` ( `id` , `to` , `title` , `from` , `message` , `date` , `read` ) 
      VALUES ('', '$invite_person', '$invite_title', '$invite_person', '$invite_text', '$date', '0')") or trigger_error("Mysql Update Inbox Failed: " . mysql_error());
      mysql_query("UPDATE friends SET person ='$invite_person' WHERE id='$inviter->id'") or trigger_error("Mysql Update Failed Friend: " . mysql_error());
      echo "Invite has been sent to <a href='profile.php?viewuser=$invite_person'><b>$invite_person</b></a>";
   }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>add friend</title>
</head>

<body>
<div align="center">
<form name="form1" method="post" action="">
                <input name="invite_person" type="text" id="invite_person" />
                <input name="invite_button" type="submit"  id="invite_button" value="Invite" />
                </form>
                </div>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/202745-invite-friend-code/#findComment-1063000
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.