Page 1 of 1

Pull value from address and cookie

Posted: Wed Nov 16, 2005 11:28 pm
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.

Posted: Thu Nov 17, 2005 12:38 am
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; ?>">

Posted: Thu Nov 17, 2005 11:39 am
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.

Posted: Thu Nov 17, 2005 11:46 am
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.

Posted: Thu Nov 17, 2005 2:02 pm
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.