2

I'm able to display what I have in my table with the code below, but as you can see in the code I'm linking the rows to a new page, and on that page I'm trying to display the rest of the rows, which I have in the same table.

I mean, I have cols ID, photo, Firstname, Lastname, Age, StreetAdd, PhoneNum, EmailAdd in the table. I'm displaying only rows photo, Firstname, Lastname on the first page.

So what I'm trying to do is when the user clicks on the First name , which I displayed from the database, he will be redirected to the new page and see the rest of the info. How do I do it?

This is the PHP page which displays the three cols. I can display the rest of the cols on a new page but it's displaying all the info in the row. I want to display the individual info for each user, not the whole list. A possible example would be eBay. When you search for items, you won't see the full description until you click on the picture or the title.

<?php
  $con = mysql_connect("localhost","root","");
  if (!$con) {
    die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("simple_login", $con);
  $result = mysql_query("SELECT * FROM test ");
  echo "<table align='center' bgcolor='#F9F0F0' border='0' cellspacing='0'>
    <tr>
      <th><font color='red'>Firstname</font></th>
    </tr>";
  while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td><a href='send.php'><img src='".$row['photo']."' \" width=\"150px\" height=\"150px\" /></a><br><br><br>";
    echo "<a href='send.php'><td align='center' style='vertical-align:text-top' width='200px'>" . $row['Firstname'] . "</td>";
    echo "<td align='center' style='vertical-align:text-top' width='200px'>" . $row['Lastname'] . "</td>";
    echo "</tr>";
  }
  echo "</table>";
  mysql_close($con);
?>
Magna
  • 598
  • 3
  • 13
  • 23
  • [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://stackoverflow.com/a/14110189/1723893). – NullPoiиteя Jan 06 '13 at 06:10

3 Answers3

3

On you have put a text level element a inside a block level element td the cell where first name is shown. Also you didn't close a tag there. correct form is this.

echo "<td align='center' style='vertical-align:text-top' width='200px'>";
echo "<a href='send.php'>" . $row['Firstname'] . "</a></td>";

To get the same user bio on the send.php you need to pass the primary key for this row. For examle if the primary key is id you pass it send.php in query string.

echo "<a href='send.php?id=".$row['id']."'>" . $row['Firstname'] . "</a></td>";

Now in the send.php use $_GET['id'] to get the primary key and use it to retrieve the user bio from db.

But make sure you escape parameters you pass to sql database. Dont use those variables directly! See Nullpointer's answer


Update 1:

When you get the primary key of a row just invoke a SELECT * with LIMIT 1

$pkey = mysql_real_escape_string($_GET['id']);
$sql = "SELECT * FROM test where id='$pkey' LIMIT 1";
/* Run this sql */
Community
  • 1
  • 1
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • thanks for the correction..could you show me how do i use $_GET['id'] to get single user info..I'd be glad...that's where I'm Stuck... – Magna Jan 06 '13 at 06:25
  • tried what you pointed but I'm getting error...please show me the full code...txs – Magna Jan 06 '13 at 06:38
  • @Magna there is no full code. It depends completely on your code. What error you are getting? – Shiplu Mokaddim Jan 06 '13 at 06:52
  • Notice: Undefined index: id in C:\xampp\htdocs\send.php on line 10 Notice: Undefined variable: result in C:\xampp\htdocs\send.php on line 18 Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\xampp\htdocs\send.php on line 18 – Magna Jan 06 '13 at 06:57
  • `mysql_fetch_array() expects parameter 1 to be resource, null given` means your previous `mysql_query` didn't succeeded. – Shiplu Mokaddim Jan 06 '13 at 09:25
1

to display individual info for each user you can use where close in query like

SELECT * FROM test WHERE user = bla

Warning

your code is vulnerable to sql injection you need to escape all get and post and the better approach will be using Prepared statement

Good Read

  1. How to prevent SQL injection in PHP?
  2. Are PDO prepared statements sufficient to prevent SQL injection?

Note

  1. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So use either PDO or MySQLi

Good read

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
  2. Pdo Tutorial For Beginners
Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • I will fix the problem with using Mysql..also WHERE user= blah works but how I'm gonna know the user name if new user registers..isn't there a way to do it automatically? – Magna Jan 06 '13 at 06:18
  • you can either do this by redirecting user to that page after registered or you can use ajax for that – NullPoiиteя Jan 06 '13 at 06:20
0

This should be your first page

<?php
  $con = mysql_connect("localhost","root","");
  if (!$con) {
    die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("simple_login", $con);
  $result = mysql_query("SELECT * FROM test ");
  echo "<table align='center' bgcolor='#F9F0F0' border='0' cellspacing='0'>
    <tr>
      <th><font color='red'>Firstname</font></th>
    </tr>";
  while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td><a href='send.php'><img src='".$row['photo']."' \" width=\"150px\" height=\"150px\" /></a><br><br><br>";
    echo "<a href='send.php?".$row['id']."'><td align='center' style='vertical-align:text-top' width='200px'>" . $row['Firstname'] . "</td>";
    echo "<td align='center' style='vertical-align:text-top' width='200px'>" . $row['Lastname'] . "</td>";
    echo "</tr>";
  }
  echo "</table>";
  mysql_close($con);
?>

Now send.php should be

<?php
$con = mysql_connect("localhost","root","");
      if (!$con) {
        die('Could not connect: ' . mysql_error());
      }
      mysql_select_db("simple_login", $con);
      $sql = "SELECT * FROM test where id = " . $_Get['id'] ;
      $result = mysql_query($sql);

//then display the result here

?>

hope this helps

Utsav
  • 1
  • 2