0

I am new to php and js and I am working on a simply on one webpage but i get stuck. What I am trying to do is next:

  1. I am logging on the web

  2. I am using the user id to retrieve data associated with that id from table in db. I am retrieving just the "name" column and using it as a hyperlink.

Here is my code:

$query = "SELECT * FROM cases WHERE id='".$_SESSION['id']."'";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
while($rows=mysql_fetch_array($result))
{
    echo '<tr>
            <td><ul><li><a href="casedetails.php">'.$rows['Name'].'</li></ul></td>
          </tr>';
} 
  1. It is retrieving data that is in the row named "name" in the table, and I want when i click on the hyperlink to save the name of the hyperlink (for ex. Murder(link to cases.php) ). because i want to use it in a query on the page of the hyperlink. I will appreciate if someone can help me!
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Dragan
  • 1
  • Please, [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide.[Prevent SQL Injection!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Nov 07 '14 at 15:52
  • you are looking for `$_GET`, your url would be formatted like `casedetails.php?case_id=1` where `1` would be the id of the case you are looking for information on. – cmorrissey Nov 07 '14 at 15:54
  • 1
    @JayBlanchard Just for the heckuvit, I'm going to use `mysql_` today and go against the grain ;) Tomorrow, I'll use `@echo off del.` - `delete Y/N?` - How I miss DOS 5.0 – Funk Forty Niner Nov 07 '14 at 15:55
  • Thank you all for the replies! – Dragan Nov 07 '14 at 17:06

1 Answers1

0

If you want to click the link and it takes you a different page while remembering what ID (or name) was clicked, you would write your code like this:

$query = "SELECT * FROM cases WHERE id='".$_SESSION['id']."'";
$result=mysql_query($query) or die("Query Failed : ".mysql_error());
while($rows=mysql_fetch_array($result))
{
   $name - $rows['Name']; 
   echo '<tr>
   <td><ul><li><a href="casedetails.php?name='.$name.'">'.$name.'</li></ul></td>
   </tr>';
} 

So, whenever you go to casedetails.php, the variable $name will be in a GET that you can retrieve and use for queries. For example, on casedetails.php you would have $newName = $_GET['name']; and that would retrieve the name.

aaronToner
  • 66
  • 3