Display element in search result page, then the details in another page

The result of the search comes from the database, on the result page when I click on the name of the searched element, I want to be redirected to another page viewing more information about that element from the database!

Here is the insert company code:

<?php

    $db = new mysqli('localhost', 'root', '', 'search');

    if(isset($_POST["submit"]))
    {
        $comName = addslashes($_POST["comName"]);
        $comCountry = addslashes($_POST["comCountry"]);
        $comKey = addslashes($_POST["comKey"]);
        $comDes = addslashes($_POST["comDes"]);
        $comLink = addslashes($_POST["comLink"]);
        $comLogo = $_FILES["comLogo"]["name"];
        $comLogo_tmp = $_FILES['comLogo']['tmp_name'];

        move_uploaded_file($comLogo_tmp, "images/$comLogo");
        {
            $sql = "insert into tshirt (comName, comCountry, comKey, comDes, comLink, comLogo) values ('$comName', '$comCountry', '$comKey', '$comDes', '$comLink', '$comLogo')";

            $rs = mysqli_query($db, $sql);

            if($rs)
            {
                echo "<script> alert('Company uploaded successfully') </script>";
            }

            else

            {
                echo "<script> alert('Uploading failed, please try again.') </script>";
            } 
        }
    }
?>

The result page code :

<?php

                        $mysqli = new mysqli("localhost", "root", "", "search");
                        if(isset($_GET['search_button'])) 
                        {
                            $search = $_GET['search'];
                            if($search=="") 
                            {
                            echo "<center> <b> Please write something in Search Box </b> </center>";
                            exit();
                            }
                            $sql = "select * from tshirt where comKey like '%$search%' limit 0, 5";
                            $rs =  mysqli_query($mysqli, $sql);
                            if (mysqli_num_rows($rs) < 1) 
                            {
                                echo "<center> <h4> <b> Oops !!! Sorry, there is no result found, try again. </b> </h4> </center>";
                                exit();
                            }

                        echo "<font size='+1' color='#1a1aff'> images for $search</font>";

                        while ($row = mysqli_fetch_array($rs)) {
                            echo   "<td>
                                        <table style='margin-top: 10px'>
                                            <tr>
                                                <td>
                                                    <a target='_blank' href='images.php?id=$search'><img src='img/$row[5]' height='100px'></a>
                                                </td>
                                            </tr>
                                        </table>
                                    </td>";
                            }
                        }
                    ?>
                </tr>
            </table>
            <?php
            echo "<a target='_blank' href='images.php?id=$search'><font size='+1' color='#1a1aff'> More Images for $search </font></a>";

            echo "<hr>";
                $sql1 = "select * from tshirt where comKey like '%$search%' limit 0, 10";
                $rs1 =  mysqli_query($mysqli, $sql1);
                while ($row1 = mysqli_fetch_array($rs1))
                {  
                    echo "<a href='$row1[2]'><font size='4' color='#0000cc'> $row1[1] </font></a><br>";
                    echo "<font size='3' color='#006400'> $row1[2] </font><br>";
                    echo "<font size='3' color='#666666'> $row1[4] </font><br><br>";
                }
    ?>

Help me please to create the code.

All answers are welcome. Thanks!

It’s a bit hard to tell as you use column numbers rather than names to display the results. But it looks as if you’ve already got a link from each of the rows to another piece of code, so all you need to do is to write that script to recover the relevant row-id from the $_GET variable that you pass in.

This seems a complicated way to do it:

echo "<a href='$row1[2]'><font size='4' color='#0000cc'> $row1[1] </font></a><br>";

The code above means that you will need a script called $row1[2], so presuming that’s a unique identifier, that means you’ll need an individually-named script for each of the items in your database, or perhaps do some messing around with a .htaccess rewrite rule. I’d normally do something more like

echo "<a href='showdetails.php?id=$row1[2]'><font size='4' color='#0000cc'> $row1[1] </font></a><br>";

so I’d then have a single script called showdetails.php, and in that script I’d recover the $_GET['id'] variable, validate it, then use that to recover the details of the item.

1 Like

Thank you for your help.
What do you think if I use the names rather than column numbers ?
Can you give me the new code if I use that?
Thank you

Using column names just makes the code easier to read, in my opinion. Right now, to figure out what $row1[1] contains, you’ve got to look back at the query and, in your case (because you “select *” rather than only selecting the columns you need) you also need to know the table structure. If you use the column names, anyone reading the code (including yourself in ten years time when you want to change something) can tell at a glance what each variable is.

No, but I (and everyone else here) will try to help you debug the code you write.

There’s already enough in your results page code to show you how to retrieve the $_GET variables, build a query using them, and output the results. You’re just doing the same thing here, but with a different query. You should look at prepared statements later on, and consider only retrieving the rows you actually use, but they’re niceties for once you get it all working.

ETA - why do you run the search twice in your results page? First you run it to retrieve five results, and then a bit later you run it again to get ten results.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.