Pull value from address and cookie

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
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Pull value from address and cookie

Post by waradmin »

So i want to put data into a database that is 1/2 from a cookie and 1/2 from the address.

What i want is to have the database table (which has 2 fields, uname and friend_uname) to be filled in first with the uname from the cookie:

Code: Select all

if($_SESSION['Uname'] == '' || $_SESSION['lp'] == '')
And the friend_uname to be filled in from the address that will look like:
user.php?user=usernamehere

I cant figure out how to get the value from the address to be put into a database.

It would be ideal to have a submit button with 2 hidden fields, one being uname and the other being friend_uname so when the submit button is pressed it puts the data in to a database but once again, i cant seem to figure it out. Any ideas would be great.

Note, i have the first mysql query as:

Code: Select all

$result = mysql_query("SELECT * FROM loginphp
WHERE Uname='{$_SESSION['Uname']}'") or die(mysql_error());

$row = mysql_fetch_array( $result );
Thanks for the help.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

getting the user from the URL would look like this

Code: Select all

$friend_uname = $_GET['user']
To put both of those fields in a form you would do this:

Code: Select all

<?
$username = $_COOKIE['username'];
$friend_uname = $_GET['user'];
?>
<input type="hidden" name="username" value="<? echo $username; ?>">
<input type="hidden" name="friend_uname" value="<? echo $friend_uname; ?>">
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

For the cookie would it be:

Code: Select all

$username = $_SESSION['Uname'];
or what you suggested:

Code: Select all

$username = $_COOKIE['Uname'];
Also for the code to put in the values would this work:

Code: Select all

{
  #connect to MySQL
  $conn = mysql_connect("$DBHOST", "$DBUSER","$DBPASS") 
	or die("Count not connect to database");
  
  #select the database
  $rs = mysql_select_db("$DBNAME",$conn) 
	or die ("Could not select database");  

  #create the SQL query
  if($Uname and $friend_username)
  {
     $sql = "insert into friends (Uname, friend_uname) 
			values (\"$User\",\"$friend_username\")"; 
     $rs = mysql_query($sql,$conn) 
	or die ("Could not execute SQL query");
  }

  #confirm the entry and display a link to view
  if($rs)
  {
    $msg = "<h3>Success - your entry has been saved.</h3>";
    $msg.= "<h3><a href = \"../index.php\">";
    $msg.= "View</a></h3>";
  }
}
Thanks.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

sessions and cookies are different.

you asked about cookies and scrotaye's answer was dead on for cookies.

it would appear though from your first post that you're using sessions in which case you'd need to change from the $_COOKIE[] array to the $_SESSION[] array.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

My mistake, sorry about that.

I was able to get that working thanks for the help on that.

Can someone lead me in the direction of database checking for an existing value. Because i want to avoid duplicate entries into the database so if someone can tell me or show me a link where i can have the database get checked for an existing value, example: the session uname is already associated with the user.php?user=username

Thanks.

If that doesnt make sense ill clear it up.
Post Reply