0

我的代码引发了意外警告。除搜索部分外,代码一切正常。

当我搜索我的数据时,我从未搜索过我收到此消息

 Warning: mysql_num_rows() expects parameter 1 to be resource

这是我的html:

<html>
    <head>
        <title>
            Employee Details 
        </title>
    </head>
    <body>
        <?php

        $page='index.php';
            mysql_connect('localhost','root','') or die (mysql_error());
            mysql_select_db('employee') or die (mysql_error());

            /* {DELETE AN ENTRY FROM THE LIST AND DATABASE -START*/
            if(isset($_GET['delete']))
            {
                mysql_query('DELETE FROM details where empid='.mysql_real_escape_string((int)$_GET['delete']));
            }
            /* DELETE AN ENTRY FROM THE LIST AND DATABASE -END}*/



            /*Form displayed after clicking on EDIT - START*/
            if(isset($_GET['edit']))
            {
                $query=mysql_query('SELECT empid, fname, lname, address, phone, email, designation FROM details where empid='.mysql_real_escape_string((int)$_GET['edit']));

                $get_row=mysql_fetch_assoc($query);
                $empid      =   $get_row['empid'];
                $fname      =   $get_row['fname'];
                $lname      =   $get_row['lname'];
                $address    =   $get_row['address'];
                $phone      =   $get_row['phone'];
                $email      =   $get_row['email'];
                $designation=   $get_row['designation'];

                echo '<p><form method="post" action="">
                        <table border=1 align="center" cellspacing=.3>
                        <caption>Edit details for '.$fname.' '.$lname.'</caption>
                        <tr><td>Employee ID:</td><td><input type="text" name="empid" value='.$empid.' readonly="readonly" size=25></td></tr>
                        <tr><td>FirstName:</td><td><input type="text" name="fname" value='.$fname.' size=25></td></tr>
                        <tr><td>Last Name:</td><td><input type="text" name="lname" value='.$lname.' size=25></td></tr>
                        <tr><td>Postal Address:</td><td><textarea name="address" rows=4 size=25>'.$address.'</textarea></td></tr>
                        <tr><td>Phone number:</td><td><input type="text" name="phone" value='.$phone.' size=25></td></tr>
                        <tr><td>E-mail address</td><td><input type="text" name="email" value='.$email.' size=25></td></tr>
                        <tr><td>Designation</td><td><textarea name="designation" rows=4 size=25>'.$designation.'</textarea></td></tr>
                        <tr><td><input type="submit" value="Save to database" name="submit"></td><td><a href="index.php">Cancel all changes</a></td></tr>
                        </table>
                        </form></p>';
            }
            /* FORM to add a new value start*/
            if(isset($_GET['add']))
                {
                    echo '<p><form method="post" action="">
                            <table border=1 align="center" cellspacing=.3>
                            <caption>Add details of a new Employee</caption>
                            <tr><td>FirstName:</td><td><input type="text" name="fname" size=25></td></tr>
                            <tr><td>Last Name:</td><td><input type="text" name="lname"  size=25></td></tr>
                            <tr><td>Postal Address:</td><td><textarea name="address" rows=4 size=25></textarea></td></tr>
                            <tr><td>Phone number:</td><td><input type="text" name="phone"  size=25></td></tr>
                            <tr><td>E-mail address</td><td><input type="text" name="email"  size=25></td></tr>
                            <tr><td>Designation</td><td><textarea name="designation" rows=4 size=25></textarea></td></tr>
                            <tr><td><input type="submit" value="Add to database" name="add"></td><td><a href="index.php">Cancel all changes</a></td></tr>
                            </table>
                            </form></p>';
            }
            /*FORM to add a new value end*/

            /*Form displayed after clicking on EDIT - END*/


            if(empty($_POST) === false)
            {
                if(isset($_POST['submit']) && $_POST['submit']='Save to database')
                    {
                        $err = 'No';
                        $empid      =   $_POST['empid'];
                        $fname      =   $_POST['fname'];
                        $lname      =   $_POST['lname'];
                        $address    =   $_POST['address'];
                        $phone      =   $_POST['phone'];
                        $email      =   $_POST['email'];
                        $designation=   $_POST['designation'];  

                        if(empty($empid)=== true || empty($fname)=== true || empty($lname)=== true || empty($address)=== true || empty($phone)=== true || empty($email)=== true || empty($designation)=== true)
                            {
                                echo '<p align="center">* All fields are mandatory</p>';
                                $err='Yes';
                            }

                                else 
                                    {
                                        if (ctype_alpha($fname) === false || ctype_alpha($lname) === false)
                                            {
                                                echo '<p align="center">* Please enter a valid name.</p>';
                                                $err='Yes';
                                            }
                                        if(!is_numeric($phone))
                                            {
                                                echo '<p align="center">* Please enter a valid phone number.</p>';
                                                $err='Yes';
                                            }
                                        if(filter_var($email,FILTER_VALIDATE_EMAIL) === false)
                                            {   
                                                echo '<p align="center">* Please enter a valid E-mail address</p>';
                                                $err='Yes';
                                            }
                                        if ($err==='No')
                                                {
                                                    mysql_query('UPDATE details set fname="'.mysql_real_escape_string($fname).'", lname="'.mysql_real_escape_string($lname).'", address="'.mysql_real_escape_string($address).'", phone="'.mysql_real_escape_string($phone).'", email="'.mysql_real_escape_string($email).'", designation="'.mysql_real_escape_string($designation).'" where empid='.mysql_real_escape_string((int)$empid));
                                                    header('Location:'.$page);
                                                }
                                    }

                    }


                    if(isset($_POST['add']) && $_POST['submit']='Add to database')
                    {
                        $err = 'No';

                        $fname      =   $_POST['fname'];
                        $lname      =   $_POST['lname'];
                        $address    =   $_POST['address'];
                        $phone      =   $_POST['phone'];
                        $email      =   $_POST['email'];
                        $designation=   $_POST['designation'];  

                        if(empty($fname)=== true || empty($lname)=== true || empty($address)=== true || empty($phone)=== true || empty($email)=== true || empty($designation)=== true)
                            {
                                echo '<p align="center">* All fields are mandatory</p>';
                                $err='Yes';
                            }

                                else 
                                    {
                                        if (ctype_alpha($fname) === false || ctype_alpha($lname) === false)
                                            {
                                                echo '<p align="center">* Please enter a valid name.</p>';
                                                $err='Yes';
                                            }
                                        if(!is_numeric($phone))
                                            {
                                                echo '<p align="center">* Please enter a valid phone number.</p>';
                                                $err='Yes';
                                            }
                                        if(filter_var($email,FILTER_VALIDATE_EMAIL) === false)
                                            {   
                                                echo '<p align="center">* Please enter a valid E-mail address</p>';
                                                $err='Yes';
                                            }
                                        if ($err==='No')
                                                {
                                                    mysql_query("INSERT INTO details (fname, lname, address, phone, email, designation) VALUES('$fname','$lname','$address','$phone','$email','$designation')");
                                                    header('Location:'.$page);
                                                }
                                    }

                    }

                if(isset($_POST['search']) && $_POST['search']='search')
                    {
                        $name   =   $_POST['name'];
                        $query = mysql_query('SELECT empid, fname, lname, address, phone, email, designation FROM details where lname='.$name."'");
                        echo $name;
                        if(mysql_num_rows($query) == 0)
                        {
                            echo 'There are no records in the database matching your search';
                        }
                            else
                            {   

                                echo '<table border=1 cellpadding=10 cellspacing=.5 bgcolor="33CCCC">
                                    <caption>Search Results for '.$name.'.</caption>
                                    <tr><th>Employee ID</th><th>First Name</th><th>Last Name</th><th>Phone</th><th>Postal Address</th><th>E-Mail</th><th>Designation</th><th>Edit</th><th>Delete</th></tr>';

                                while($get_row = mysql_fetch_assoc($query))
                                    {
                                        echo '<tr><td>'.$get_row['empid'].'</td><td>'.$get_row['fname'].'</td><td>'.$get_row['lname'].'</td><td>'.$get_row['phone'].'</td><td>'.$get_row['address'].'</td><td>'.$get_row['email'].'</td><td>'.$get_row['designation'].'</td><td><a href="index.php?edit='.$get_row['empid'].'">Modify Details</a></td><td><a href="index.php?delete='.$get_row['empid'].'">Delete entry</a></td></tr>';
                                    }
                                echo '</table>';

                            }                   
                    }

            }


            echo '<p align="right"><a href="index.php?add">Add a new value</a></p>';
            /* TO DISPLAY DATA STORED IN THE DATABASE -START */

            echo '<form action="" method="post">    
                        <p align="right"><input type="text" name="name" size=25>
                        <input type="submit" name="search" value="search"></p>
                    </form>';

            $query=mysql_query('SELECT empid, fname, lname, address, phone, email, designation FROM details');
            if(mysql_num_rows($query) == 0)
            {
                echo 'There are no records in the database';
            }
                else
                {   

                    echo '<table border=1 cellpadding=10 cellspacing=.5 bgcolor="33CCCC">
                        <tr><th>Employee ID</th><th>First Name</th><th>Last Name</th><th>Phone</th><th>Postal Address</th><th>E-Mail</th><th>Designation</th><th>Edit</th><th>Delete</th></tr>';

                    while($get_row = mysql_fetch_assoc($query))
                        {
                            echo '<tr><td>'.$get_row['empid'].'</td><td>'.$get_row['fname'].'</td><td>'.$get_row['lname'].'</td><td>'.$get_row['phone'].'</td><td>'.$get_row['address'].'</td><td>'.$get_row['email'].'</td><td>'.$get_row['designation'].'</td><td><a href="index.php?edit='.$get_row['empid'].'">Modify Details</a></td><td><a href="index.php?delete='.$get_row['empid'].'">Delete entry</a></td></tr>';
                        }
                    echo '</table>';

                }   

            /* TO DISPLAY DATA STORED IN THE DATABASE -END */



        ?>

    </body>
</html>
4

2 回答 2

0

改变

$query = mysql_query('SELECT empid, fname, lname, address, phone, email, designation FROM details where lname='.$name."'");

$query = mysql_query("SELECT empid, fname, lname, address, phone, email, designation FROM details where lname='".$name."'");

(您忘记了字符串的开头 ')

于 2013-03-17T16:56:18.903 回答
0

只需替换此行 mysql_query('SELECT empid, fname, lname, address, phone, email, designation FROM details where empid='.mysql_real_escape_string((int)$_GET['edit']));

对此

mysql_query("SELECT empid, fname, lname, address, phone, email, designation FROM details where empid='.mysql_real_escape_string((int)$_GET['edit'])") or die(mysql_error());

你会发现问题所在。很可能,您正在引用一个不存在的行。另外,正如其他回复所说,您'在查询结束时忘记了,

于 2013-03-17T16:56:57.803 回答