开发者

How to add a $userid onto an Html Link

Is i开发者_开发知识库t possible once I have retrieved a userid from a MySql database, for example, $userID, to then use that as a parameter in a HTML link, for example;

<href="http://www.site.com/?110938">

Thanks.


In PHP, anything you echo is sent to the browser. So you can simply echo that variable.

echo $userID;

Or to put it in the link:

<href="http://www.site.com/?<?php echo $userID ?>">

Of course that means you're trusting that $userID will contain valid data (and not some malformed HTML attempting to break your site and ruin your user's lives).

If it should always be a number, you can keep out potential bad data by forcing it to be an integer:

<href="http://www.site.com/?<?php echo (int) $userID ?>">


I believe what you are looking to do is use the GET method of retrieving data.

The GET method (as opposed to post) pulls all of the parameters out of the URL. For example, lets take a google maps search url:

http://maps.google.com/maps?q=Empire+State+Building,+NY&hl=en

Let us break down this URL:

  1. http://maps.google.com/ - this is the location of the script
  2. maps? - Maps is the name of the file that contains the script (if they are using PHP); notice, there is a ? in the URL, indicating that the GET method is being used. All information after the ? pertains to the GET data.
  3. q=Empire+State+Building,+NY& - this portion is the first GET variable. They have decided to use the letter q as the name of their GET variable, and the value is Empire+State+Building,+NY. The & indicates that another variable will be specified.
  4. hl=en - this is the second variable in their query.

If they were to use PHP to read these parameters, they would use the following code:

$var1 = $_GET['q']; //Has value Empire+State+Building,+NY as string
$vars = $_GET['hl']; //Has value en as string

If you wanted to have your user's ID be in the URL, and use that to interact with the database, the easiest way would be to use GET, such that your URL would look like this:

www.example.com/example.php?id=45828

Then in your script, you would use the following code to access that data and query the database with it:

$userid = $_GET['id'];
$query = "SELECT * FROM users WHERE userid=$userid;";
$query = mysql_escape_string( $query );
mysql_query( $query );


just pass the variable like <href="http://www.site.com/?var_name=<?php echo $userID;?>">

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜