Page 1 of 1

seraching record from table code

Posted: Fri Jun 12, 2009 6:37 am
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

Re: seraching record from table code

Posted: Fri Jun 12, 2009 7:14 am
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.

Re: seraching record from table code

Posted: Sat Jun 13, 2009 6:01 pm
by Christopher
Please not duplicate posting. A solution has been provided above. Ask more question in this thread.