Hafiz Abdullah
Hafiz Abdullah

Reputation: 248

-- undefined index -- in php

In my page, I have 2 forms of textbox.

The first one I have 5 textbox and 2 submit buttons (Save & Update) in it.

The second one I have 1 textbox and 2 submit buttons (Delete & Search).

The problem is whenever I try to click the Update button, there's an error occur (Undefined index: simpan in C:\Apache\htdocs\standby\new_testing.php on line 37) but it still execute the function. It appears that I have a problem in this line ( if ($_GET['padam'] == 'Delete') ). Can anyone help me with this. This is my code:

        <!-- form for inserting and updating data into database-->
        <form action="new_testing.php" method="GET">                                
        <input type="varchar" size="15" name="textbox"><br>
        <input type="varchar" size="15" name="textbox2"><br>
        <input type="varchar" size="15" name="textbox3"><br>
        <input type="varchar" size="15" name="textbox4"><br>
        <input type="number" size="15" name="textbox5"><br><br>
        <input type="submit" name="simpan" value="Save">   <input type="submit" name="alter" value="Update">
        </form>


        <?php



        if ( isset($_GET['textbox']) and isset($_GET['textbox2']) and isset($_GET['textbox3']) and isset($_GET['textbox4']) 
        and isset($_GET['textbox5']) )
        {

        $box1 = $_GET['textbox'];
        $box2 = $_GET['textbox2'];
        $box3 = $_GET['textbox3'];
        $box4 = $_GET['textbox4'];
        $box5 = $_GET['textbox5'];




        $db=mysql_connect  ("localhost", "root",  "") or die ('I cannot connect to the database  because: ' . mysql_error());

        $mydb=mysql_select_db("inventory");


            if ($_GET['simpan'] == 'Save')
            {
            $simpan = $_GET['simpan'];
            $sql="insert into alatan values( '".$box1."','".$box2."','".$box3."','".$box4."','".$box5."' ) ";
            $result=mysql_query($sql) or die (mysql_error());
            }

            elseif ($_GET['alter'] == 'Update')
            {
            $alter = $_GET['alter'];
            $sql="update alatan set Fakulti=('".$box2."'), NamaAlat=('".$box3."'), Lokasi=('".$box4."'), TahunDibeli=('".$box5."') where NoSiri=('".$box1."')";
            $result=mysql_query($sql) or die (mysql_error());
            }



        }
        ?>


        <!-- form for deleting and seacrhing data from database-->
        <form action="new_testing.php" method="GET">
        <input type="varchar" size="15" name="drop"><br>
        <input type="submit" name="padam" value="Delete"> <input type="submit" name="cari" value="Search">

        </form>



        <?php

        if ( isset($_GET['drop']) )
        {

        $del = $_GET['drop'];


        $db=mysql_connect  ("localhost", "root",  "") or die ('I cannot connect to the database  because: ' . mysql_error());

        $mydb=mysql_select_db("inventory");


            if ($_GET['padam'] == 'Delete')

            {
                $padam = $_GET['padam'];

            $sql="delete from alatan where NoSiri=('".$del."')";

            $result=mysql_query($sql) or die (mysql_error());


            }
                elseif ($_GET['cari'] == 'Search')

                {
                    $cari = $_GET['cari'];

                    $sql= "select * from alatan where NoSiri=('". $del ."')";

                    $result=mysql_query($sql) or die (mysql_error());

                    while($row=mysql_fetch_array($result)){

                        $box1  =$row['NoSiri'];
                        $box2 =$row['Fakulti'];
                        $box3 =$row['NamaAlat'];
                        $box4 =$row['Lokasi'];
                        $box5 =$row['TahunDibeli'];


        }

                }



        }

        ?>


        <!-- table for displaying table "alatan-->
        <table border="1">
            <tr>

            <th>No Siri</th>
            <th>Fakulti</th>
            <th>Nama Alat</th>
            <th>Lokasi</th>
            <th>Tahun Dibeli</th>
          </tr>

          <?php

        $db=mysql_connect  ("localhost", "root",  "") or die ('I cannot connect to the database  because: ' . mysql_error());

        $mydb=mysql_select_db("inventory");

        $sql="select * from alatan";

        $result=mysql_query($sql);

        $NoSiri  = array();
        $Fakulti = array();
        $NamaAlat = array();
        $Lokasi = array();
        $Tahun = array();

        while($row=mysql_fetch_array($result)){

            $NoSiri  =$row['NoSiri'];
            $Fakulti =$row['Fakulti'];
            $NamaAlat =$row['NamaAlat'];
            $Lokasi =$row['Lokasi'];
            $Tahun =$row['TahunDibeli'];


            echo "<tr>

          <td> $NoSiri </td>
          <td>$Fakulti </td>
            <td>$NamaAlat</td>
            <td>$Lokasi</td>
            <td>$Tahun</td>

            </tr>";

            }

        ?>

Upvotes: 0

Views: 914

Answers (1)

Menztrual
Menztrual

Reputation: 41597

Because its doing a check on $_GET when the index 'padam' hasn't been set. IE: page.php?padam=...

The following will fix that problem:

if( isset($_GET['padam']) && $_GET['padam'] == 'Delete' )
{
    ...
}

Upvotes: 1

Related Questions