简体   繁体   中英

display more than one search result with php

Currently my search script works but its not doing what I would like it to do. At the moment it only returns one search result even though there is about 20 that should shown for a searched item. I am trying to figure out how to get the script to return those missing results that should be shown. So maybe the the experts here could help :)

include('data.php');
    //Database Connection
$con=@mysql_connect("$ip", "$user", "$pass")
            or die(mysql_error());

//Select Database
$dbcon=@mysql_select_db($db, $con)
            or die(mysql_error());


$search = $_POST['search'];

$sql = mysql_query("select guid, name, staffmember, dateposted, id, admin_note from $whitelist where guid like '%$search%' or name like '%$search%' or staffmember like '%$search%' or dateposted like '%$search%' or id like '%$search%' or admin_note like '%$search%'");


while ($row = mysql_fetch_array($sql)) {
    //Username
    $name = $row['name'];
    //GUID
    $guid = $row['guid'];
    //Staff Member Who Submitted
    $staff = $row['staffmember'];
    //ID
    $id = $row['id'];
    //Date Posted
    $date = $row['dateposted'];
    //Note From Admin
    $admin = $row['admin_note'];
    }
    ?>
    <html>
    <body>
    <table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
                <tr>
                    <!--<th class="table-header-check"><a id="toggle-all" ></a> </th>-->
                    <th class="table-header-repeat line-left minwidth-1"><a href="">ID</a>  </th>
                    <th class="table-header-repeat line-left minwidth-1"><a href="">GUID</a></th>
                    <th class="table-header-repeat line-left"><a href="">Username</a></th>
                    <th class="table-header-repeat line-left"><a href="">Admin Note</a></th>
                    <th class="table-header-repeat line-left"><a href="">Staff Member</a></th>
                    <th class="table-header-repeat line-left"><a href="">Date Posted</a></th>
                    <th class="table-header-options line-left"><a href="">Options</a></th>

                </tr>
                <tr>

                    <td><?php echo "$id"; ?></td>
                    <td><?php echo "$guid"; ?></td>
                    <td><?php echo "$name"; ?></td>
                    <td><?php echo "$admin"; ?></td>
                    <td><?php echo "$staff"; ?></td>
                    <td><?php echo "$date"; ?></td>
                    <td class="options-width">
                    <?php
                 echo '<a href="edit.php?id=' . $row['id'] . '" title="Edit" class="icon-1 info-tooltip"></a>';
                echo '<a href="delete.php?id=' . $row['id'] . '" title="Edit" class="icon-2 info-tooltip"></a>'; 
                ?>

                    </td>
                </tr>

                </table>
                </body>
                </html>
while ($row = mysql_fetch_array($sql)) {
    //Username
    $name = $row['name'];
    //GUID
    $guid = $row['guid'];
    //Staff Member Who Submitted
    $staff = $row['staffmember'];
    //ID
    $id = $row['id'];
    //Date Posted
    $date = $row['dateposted'];
    //Note From Admin
    $admin = $row['admin_note'];
    }

What you are doing here is wrong. In every iteration of the loop, the variables are being re-defined, their old values are getting deleted and overrided by the new values.

You need to output the data within the "while ($row = mysql_fetch_array($sql))" loop. As it is by now, your old values are overwritten for each new row that it fetched.

In this case your code has to be structured like

 print out table header
 while ($row = mysql_fetch_array($sql)) {
    print out table row
 }
 print out what comes after table

ie

include('data.php');
//Database Connection
$con=@mysql_connect("$ip", "$user", "$pass")
        or die(mysql_error());

//Select Database
$dbcon=@mysql_select_db($db, $con)
        or die(mysql_error());


$search = $_POST['search'];

$sql = mysql_query("select guid, name, staffmember, dateposted, id, admin_note from $whitelist where guid like '%$search%' or name like '%$search%' or staffmember like '%$search%' or dateposted like '%$search%' or id like '%$search%' or admin_note like '%$search%'");?>


<html>
<body>
<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
            <tr>
                <!--<th class="table-header-check"><a id="toggle-all" ></a> </th>-->
                <th class="table-header-repeat line-left minwidth-1"><a href="">ID</a>  </th>
                <th class="table-header-repeat line-left minwidth-1"><a href="">GUID</a></th>
                <th class="table-header-repeat line-left"><a href="">Username</a></th>
                <th class="table-header-repeat line-left"><a href="">Admin Note</a></th>
                <th class="table-header-repeat line-left"><a href="">Staff Member</a></th>
                <th class="table-header-repeat line-left"><a href="">Date Posted</a></th>
                <th class="table-header-options line-left"><a href="">Options</a></th>

            </tr>


<?php
while ($row = mysql_fetch_array($sql)) {
//Username
$name = $row['name'];
//GUID
$guid = $row['guid'];
//Staff Member Who Submitted
$staff = $row['staffmember'];
//ID
$id = $row['id'];
//Date Posted
$date = $row['dateposted'];
//Note From Admin
$admin = $row['admin_note'];
?>
            <tr>

                <td><?php echo "$id"; ?></td>
                <td><?php echo "$guid"; ?></td>
                <td><?php echo "$name"; ?></td>
                <td><?php echo "$admin"; ?></td>
                <td><?php echo "$staff"; ?></td>
                <td><?php echo "$date"; ?></td>
                <td class="options-width">
                <?php
 }

             echo '<a href="edit.php?id=' . $row['id'] . '" title="Edit" class="icon-1 info-tooltip"></a>';
            echo '<a href="delete.php?id=' . $row['id'] . '" title="Edit" class="icon-2 info-tooltip"></a>'; 
            ?>

                </td>
            </tr>

            </table>
            </body>
            </html>

(To make good and safe code, a few things should be changed, but to get you started, this should work)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM