-3

When I delete it works fine. When I cancel also it works fine. But when I edit and click on save after edit I get the message "Undefined index: search in ". Data gets saved though. How do I fix this.

Code is :

<html>
    <head>
    </head>
<body>  
<?php
    $page='search.php';
    mysql_connect("localhost","root","") or die (mysql_error());
    mysql_select_db("list") or die (mysql_error());

    if(empty($_POST) === false)
    {

        if ($_POST['search'] == 'search')
        {

            $data=$_POST['criteria']; 
            if (empty($data) === true)
            {
                echo 'Please enter some text!!<br/>';
            } 
            else
            {
                $get=mysql_query("SELECT SRNO, fname, lname, phone, email, address, comments from names where fname='" . mysql_real_escape_string($data) . "'");
                if (mysql_num_rows($get)==0) 
                    {
                        echo 'There are no search results!!';
                    }
                        else
                    {
                        echo '<table border=0 cellspacing=25 cellpadding=1>';
                        echo'<tr><th>Sr. No</th><th>First Name</th><th>Last Name</th><th>Phone No</th><th>E-mail</th><th>Address</th><th>Comments!!</th><th>Modify</th><th>Delete!</th></tr>';      
                        while($get_row=mysql_fetch_assoc($get))
                            {
                                echo '<tr><td>'.$get_row['SRNO'].'</td><td>'.$get_row['fname'].'</td><td>'.$get_row['lname'].'</td><td>'.$get_row['phone'].'</td><td>'.$get_row['email'].'</td><td>'.$get_row['address'].'</td><td>'.$get_row['comments'].'</td><td><a href="search.php?edit='.$get_row['SRNO'].'">Edit</a></td><td><a href="search.php?delete='.$get_row['SRNO'].'">Delete</a></td></tr>';

                            }
                        echo '</table>';

                    }
            }
        }

        else if ($_POST['save']== 'save')
        {

            $count=0;
            $fname= $_POST['fname'];
            $lname= $_POST['lname'];
            $srno=  $_POST['SRNO'];
            $address=$_POST['address'];
            $comments=$_POST['comments'];
            $email=$_POST['email'];
            $phone=$_POST['phone'];

                if (empty($lname) === true || empty($fname) === true || empty($address) === true || empty($comments) === true || empty($email) === true || empty($phone) === true) 
                {
                    echo '<h3>All fields are mandatory</h3>';

                }
                else
                {
                        if (filter_var($email,FILTER_VALIDATE_EMAIL) === false)
                        {
                            echo '<h3>This is not a valid e-mail address.</h3><br />';
                            $count=$count+1;
                        }
                        if (ctype_alpha($fname) === false || ctype_alpha($lname) === false)
                        {
                            echo '<h3>Name should contain character only!</h3><br />';
                            $count=$count+1;
                        }
                        if( !is_numeric($phone) ) 
                        {
                                echo '<h3>Please enter a valid phone number</h3><br />';
                                $count=$count+1;
                        }
                        if ($count==0)
                        {
                            if(isset($_GET['edit']))
                                {
                                    mysql_query('update names set fname="'.$fname.'", lname="'.$lname.'", address="'.$address.'", comments="'.$comments.'", email="'.$email.'", phone="'.$phone.'"  where SRNO="'.$srno.'"');   

                                } 
                                else if(isset($_GET['add']))
                                    {
                                        mysql_query("INSERT INTO names (fname,lname,phone,email,comments,address) VALUES ('$fname', '$lname','$phone','$email','$comments','$address')");
                                    }

                            //header('Location:'.$page);
                        }

                }
        }
    }   
            //else if
        /*
        if(mysql_num_rows($getf) == 0)
        {
            $getel=mysql_query('SELECT SRNO, fname, lname, phone, email, address, comments from names where lname='.$_GET['$data']));

        }*/



        if(isset($_GET['delete']))
        {
            mysql_query('DELETE from names where SRNO='.mysql_real_escape_string((int)$_GET['delete']));

        }

        if(isset($_GET['edit']))
        {
            $getedit=mysql_query('SELECT SRNO, fname, lname, phone, email, address, comments from names where SRNO='.mysql_real_escape_string((int)$_GET['edit']));

            echo '<table border=0>';
            while ($get_row=mysql_fetch_assoc($getedit))
                {
                    echo '<form method="POST" action="">';

                    echo '<tr><td>Sr.No:</td><td><input type="text" value='.$get_row['SRNO'].' name="SRNO" readonly="readonly"></td></tr>';
                    echo '<tr><td>First Name:</td><td><input type="text" value='.$get_row['fname'].' name="fname"></td></tr>';
                    echo '<tr><td>Last Name:</td><td><input type="text" value='.$get_row['lname'].' name="lname"></td></tr>';
                    echo '<tr><td>Phone No:</td><td><input type="text" value='.$get_row['phone'].' name="phone"></td></tr>';
                    echo '<tr><td>E-mail address:</td><td><input type="text" value='.$get_row['email'].' name="email"</td></tr>';
                    echo '<tr><td>Address:</td><td><textarea name="address" rows=4>'.$get_row['address'].'</textarea></td></tr>';
                    echo '<tr><td>Comments:</td><td><textarea name="comments" rows=4>'.$get_row['comments'].'</textarea></td></tr>';
                    echo '<tr><td><input type="submit" name="save" value="save"></td><td><a href="search.php">Cancel</a></td></tr>';
                    echo '</form>';                                 
                }
            echo '</table>';

        }

            echo '<form action="" method="post">';  
            echo '<input type="text" name="criteria">';
            //echo '<input type="hidden" name="form" value="search">';
            echo '<input type="submit" value="search" name="search">';
            echo '</form>';

 echo '<br /><a href="index.php">Home</a>';
?>
</body>
</html>
  • 1
    Look into **related**, this question gets asked multiple times a day. – Peon Mar 13 '13 at 10:04
  • Note that the best/correct way to check if it was a POST request (instead of GET); is by checking it this way: if($_SERVER['REQUEST_METHOD'] == 'POST') – Niek van der Steen Mar 13 '13 at 10:09
  • possible duplicate of [Undefined index error PHP](http://stackoverflow.com/questions/10613570/undefined-index-error-php) – bummi Sep 15 '14 at 09:30

3 Answers3

4

It is because the search variable is not being sent in the form this time. If you check first if it is set, then you won't get that warning.

Change this line

if ($_POST['search'] == 'search')

For this one:

if (isset($_POST['search']) && $_POST['search'] == 'search')
Alvaro
  • 40,778
  • 30
  • 164
  • 336
0

I think you have to check your condition this like

if (isset($_POST['search']) && $_POST['search'] == 'search')
{
   // code here
}
else if(isset($_POST['save']) && $_POST['save']== 'save')
{
   // code here
}
Nitin
  • 1
  • 4
  • Actually in the case of edit. Your form does not having field named search. So, as per your condition code check for search but it does't exist. Try this. – Nitin Mar 13 '13 at 10:11
0

Yes everything is working fine first it checks you $_POST['search'] when you post save form. Which dont contain any search form field.

jst replace all $_POST['save'] , $_POST['search'] with (isset($_POST['save']) && $_POST['save']=='save') and (isset($_POST['search']) && $_POST['search']=='search')

Things that you need to remember is

  1. If your query returning single result then dont use while loop there. simply fecth resutl.
  2. always try to put form tag outside / before table.
  3. In above your structure you are repeating same name in form inside while loop.
  4. check your variable in php with isset.

try above suggestion and then try.

mukund
  • 2,253
  • 1
  • 18
  • 31