Jump to content

creating a link when i extract from the database


hashkash

Recommended Posts

Hello!
Im new to php.My problem is this.
I have some user info stored in the db.Now i would like to display only the names from the userinfo table.
(this i can do).I would like the clients to be able click on the names displayed so that they can view
that users information.
Pls help me!!
Thanks!
Kashyap
Link to comment
Share on other sites

The showinfo.php that vbnullchar posted is just an example. The actual page is going to be the page that you want the info to show on.

replace that with the page name you want the data to show on


The ?id=$row[id]  part is how you pass the record id through the url

you retrieve that on your showinfo page by using the get superglobal

e.g.

$user_id=$_GET['id'];

you then have a variable called $user_id with which to add into your mysql query for that users id

hope this helps
Link to comment
Share on other sites

Im getting a warning
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\showinfo.php on line 7.
here is the showinfo.php code
[code]
<?php
    $con=mysql_connect("localhost","root","")
    or die("Couldnt Connect :" . mysql_error());
    mysql_select_db("joomla");
    $uname=$_GET['name'];
    $query="select * from j_userdetails where name= '$uname' ";
    while ($line = mysql_fetch_array($query, MYSQL_ASSOC))
          {
            echo "\t<tr>\n";
            foreach ($line as $col_value)
            {
            echo "\t\t<td>$col_value</td>\n";
            }
            echo "\t</tr>\n";
          }
          echo "</table>";
?>
[/code]
Link to comment
Share on other sites

You need to change the select statement to get the record that matches the id passed in the URL

instead of [code]

<?php
    $con=mysql_connect("localhost","root","")
    or die("Couldnt Connect :" . mysql_error());
    mysql_select_db("joomla");
    $uname=$row[name];  // this is not valid, because you have no query above it to deliver this data into this variable
    $query="select * from j_userdetails where name='$uname'";
    while ($line = mysql_fetch_array($query, MYSQL_ASSOC))
          {
            echo "\t<tr>\n";
            foreach ($line as $col_value)
            {
            echo "\t\t<td>$col_value</td>\n";
            }
            echo "\t</tr>\n";
          }
          echo "</table>";
?>
[/code]

try
[code]
<?php
    $con=mysql_connect("localhost","root","")
    or die("Couldnt Connect :" . mysql_error());
    mysql_select_db("joomla");
$user_id=$_GET['id']; //added this line to get the var passed through the url
  $query="select * from j_userdetails where uid='$user_id'"; // changed here to retrieve user id

    while ($line = mysql_fetch_assoc($query)) //shortened this line to be a little cleaner
          {
            echo "\t<tr>\n";
            foreach ($line as $col_value)
            {
            echo "\t\t<td>$col_value</td>\n";
            }
            echo "\t</tr>\n";
          }
          echo "</table>";
?>

[/code]

you may need to change uid to whatever your actual field name for user id is.
Link to comment
Share on other sites

AHHHH I cannot believe I missed this..

[code]
$query="select * from j_userdetails where uid='$user_id'"; // changed here to retrieve user id

$result=mysql_query($query);  // your not actually running the query until you add this

    while ($line = mysql_fetch_assoc($result)) //shortened this line to be a little cleaner
       
[/code]

once you set up your query variable, you have to actually run that query using the mysql_query($query) method. then you have to reference the $result variable in your mysql_fetch_assoc() method like above.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.