seraching record from table code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
goraya
Forum Newbie
Posts: 3
Joined: Fri Jun 12, 2009 5:46 am

seraching record from table code

Post by goraya »

i have table student where i have fields
id ,name ,marks,remarks
i wana make a form where i have one fild enter Id and submit then
show me result another php page results of student
like
id name marks remarks
pls any one help me in this code of php and my sql
ben.artiss
Forum Contributor
Posts: 116
Joined: Fri Jan 23, 2009 3:04 pm

Re: seraching record from table code

Post by ben.artiss »

Hi,

It's up to you how you'd like to do this but if it's this small usually I'd keep it all in the same page:

Code: Select all

<html>
    <head>...</head>
    <body>
        <?php if (!isset($_POST['search_id'])) { ?>
            <form method="post" action="">
                <input type="text" name="search_id" />
                <input type="submit" value="Search" />
            </form>
        <?php } else { ?>
            <table>
                <tr>
                    <td>ID</td>
                    <td>Name</td>
                    <td>Marks</td>
                    <td>Remarks</td>
                </tr>
                <?php
                $sql = sprintf("SELECT * FROM `student` WHERE `id`='%s' LIMIT 1",mysql_real_escape_string($_POST['search_id']));
                if ($res = mysql_query($sql)) {
                    $rows = mysql_num_rows($res);
                    if (!$rows) {
                        echo '<tr><td colspan="4">No result for '.htmlspecialchars($_POST['search_id']).'</td></tr>';
                    } else {
                        $row = mysql_fetch_assoc($res);
                        echo '<tr>';
                        echo '<td>'.$row['id'].'</td>';
                        echo '<td>'.$row['name'].'</td>';
                        echo '<td>'.$row['marks'].'</td>';
                        echo '<td>'.$row['remarks'].'</td>';
                        echo '</tr>';
                    }
                }
                ?>
            </table>
        <?php } ?>
    </body>
</html>
This is just a quick example and you may want a bit more checking to make sure it's a valid number etc. but hopefully this should point you in the right direction.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: seraching record from table code

Post by Christopher »

Please not duplicate posting. A solution has been provided above. Ask more question in this thread.
(#10850)
Post Reply