Need to make links from search results!

Is it possible to make my MySQL search results into a link? meaning if I do a search for lets say I’m searching my inventory and I do a search by manufacture and my results return 50 items in stock by that manufacture, is it possible to make each row into a link that I can click on and be sent to another page that pulls up a detailed view of that row? To be more specific lets say I searched for the manufacture Sony and the return results show 50 items by Sony and I select an item that’s lets say an LED TV, now I already have a form made that display’s that item in detail, so it will display all the information already listed in the original search but the link will produce a page with other details such as an image of the product, cost, warranty dates, etc. Is this possible using PHP or do I need to use something like Java?

It is certainly possible, depending on your table structure. If you have a primary key on your table, this is where you can use it for devastatingly good results.

A PRIMARY key is a unique, auto-incrementing integer count on your table. Each new row gets a new unique ID. They’re unique, so you can use them in WHERE clauses. Not just that, but they’re super-fast to search through thanks to the PRIMARY index type.

What you have to do if you have one is to simply pass it to another page as a GET parameter (?id=the ID of your row). From there, your new page will create the view. You can get the data for just that row by filtering through MySQL with a WHERE clause, for example:

[#]SELECT * FROM MyTable WHERE id = $myID[/#]

NOTE: SANITIZE $myID before using it. Something like $myID = (int)$_GET['id]; will effectively typecast to integer and get rid of anything that is not a number.

I do have a primary key table for all my db tables all of them set to auto increment, so am I understanding that I need to set the get parameter in the returned search page?

…In the links you generate. And then pick them back up on the next page to discriminate between what the user clicked and the rest.

Ok then, thanks for your help sebrenauld. Off to find a tutorial I go!

ok so I figured out how to make a link on each row of my search results and I now how to get the data to display in the linked form but my new problem is this, when you click on the id number (which is the link to the itemized view of that item) the new view_item.php page comes up and display’s the content for that row but only because I have the code in the view_item.php set to view that id number. How do I pass the id number I click on to the new page and have it query the db for that row? you can see what I mean here:
http://www.jemtechnv.com/portal/inventory/inventory_search.php when you click on ID 1 it will take you to a page that displays the correct info for ID 1 if you return and click on ID 2 or any ID other than 1 you will see the page still displays the info for ID 1.
Here is my code for the inventory_search.php page:

[php]

".$rows[‘id’]."".$rows[‘part_number’]."".$rows[‘model_number’]."".$rows[‘serial_number’]."".$rows[‘part_manufacture’]."".$rows[‘part_available’]."".$rows[‘part_quantity’]."".$rows[‘part_condition’]."<img src=’".$rows[‘part_image’]."'height=‘50’ width=‘50’/>
[/php]

And the code for the view_item.php page:

[php]// Connect to server and select databse
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);
$data = “select * from $tbl_name where ID = 1”;

$query = mysql_query($data);

$data2 = mysql_fetch_array($query);

?>

Part Number Model Number Serial Number

[/php]

I know I need to use $_GET[id] but I’m not sure how I need to use it in my view_item.php page and how I pass it in my link from the inventory_search.php page.
I’ve tried using “.$rows[‘id’].” in the inventory_search.php page

and variations of the code below in my view_item.php page

[code]$ID=$_GET[‘ID’];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query=“SELECT * FROM inventory WHERE ID=’$ID’”;
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();

<input type=“hidden” name=“ID” value="<? echo "$ID"; ?>">
[/code]

but I don’t have any experience using the GET function so I’m at a loss here!

holy crap I figured it out. so for the next poor soul that’s fighting with this and can’t get anybody to answer here is what I did.

On my search results page I did this
[php]<a href=view_item.php?id=$rows[id]>".$rows[‘id’]."[/php]

so the above creates the hyper link to the desired page and the ?id=$rows[id] grabs the specific id for the hyperlink you click on and send it to the desired page via the browser url. you should see ?id=1 or whatever number you click on at the end of the url in your browser.

secondly in the link page, which is my view_item.php I did this.

[code]

// Connect to server and select databse
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);
$id=$_REQUEST[‘id’];
$data = “select * from $tbl_name where ID = $id”;

$query = mysql_query($data);

$data2 = mysql_fetch_array($query);[/code]
and it works, so when I click on an id in my inventory_search.php page it will pull up a new page, view_item.php and display in greater detail about the item on the first page.

That’s very kind of you. My last post, dating from four days ago, had this:

What you have to do if you have one is to simply pass it to another page as a GET parameter (?id=the ID of your row). From there, your new page will create the view. You can get the data for just that row by filtering through MySQL with a WHERE clause, for example:

SELECT * FROM MyTable WHERE id = $myID

NOTE: SANITIZE $myID before using it. Something like $myID = (int)$_GET['id]; will effectively typecast to integer and get rid of anything that is not a number.

The gratitude is heart-warming.

By the way, right now, you’ve got a gaping SQL injection vulnerability on the page. Proof that you did not even read that post. So if I were you, I’d stop whining about “lack of help” and actually read what people say.

If I were you I’d shut your sissy girl mouth pal, you’re cryptic answer wasn’t worth my time that’s why I said I would find a tutorial and figured it out on my own. Secondly I could care less about any sql vulnerabilities since it’s not being used on a server with internet access. I put it on a test site for visual purposes only, the final version will be run within my office with no outside access so what does it matter. Get off your high horse there buddy, if anybody is crying about anything it’s you crying because you didn’t get acknowledged for supposedly helping me solve my problem, which by the way you didn’t! So sulk about it some more and post some more of your intelligent witty remarks because I honestly could care less what you think, you’re not the only solution for PHP help, for that matter you’re not even a solution for help at all!

awww he got all butt hurt and cried to the moderator. lol ha ha ha ha talk about whining!

Sponsor our Newsletter | Privacy Policy | Terms of Service