2012-03-19 79 views
0

在我的页面中,我有两种形式的文本框。- undefined index - in php

第一个我有5个文本框和2个提交按钮(保存&更新)。

第二个我有1个文本框和2个提交按钮(删除&搜索)。

问题是,无论何时我尝试点击更新按钮,都会发生错误(未定义的索引:第37行中的C:\ Apache \ htdocs \ standby \ new_testing.php中的simpan),但它仍然执行该函数。看来我在这一行有一个问题(if($ _GET ['padam'] =='Delete'))。谁能帮我这个。这是我的代码:

 <!-- 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>"; 

      } 

     ?> 

回答

1

因为它在索引'padam'尚未设置时执行$ _GET检查。 IE:page.php?padam=...

下面将解决这个问题:

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

谢谢!有用.. – 2012-03-19 04:08:05