2017-08-12 25 views
3

我在PHP OOP和CMS中構建了一個網站CMS,基本上有一個管理站點的選項來更新他們的導航菜單項。因此,它基本上是這樣的:如何在PHP中通過GET請求傳遞輸入的名稱

print screen

而對於這個代碼是這樣的:

<?php 
    if (isset($_GET['id'])){ 
     $id = $_GET['id']; 
     $menuSet = new Menus(); 
     $menuSet->SelectMenuById($id); 
     echo " 
     <div class='content-wrapper'> 
      <section class='content-header'> 
       <h1> 
        Menu Settings 
        <small>".$menuSet->GetMenuName()."</small> 
       </h1> 
       <ol class='breadcrumb'> 
        <li class='active'>menusettings.php</li> 
       </ol> 
      </section> 
      <section class='content'> 
       <div class='row'> 
        <div class='col-xs-12'> 
         <div class='box'> 
         <div class='box-header'> 
          <h3 class='box-title'></h3> 

          <div class='box-tools'> 
          <div class='input-group input-group-sm' style='width: 150px;'> 
           <input type='text' name='table_search' class='form-control pull-right' placeholder='Search'> 

           <div class='input-group-btn'> 
           <button type='submit' class='btn btn-default'><i class='fa fa-search'></i></button> 
           </div> 
          </div> 
          </div> 
         </div> 
         <!-- /.box-header --> 
         <div class='box-body table-responsive no-padding'> 
          <table class='table table-hover'> 
          <tr> 
           <th>Number</th> 
           <th>Link</th> 
           <th></th> 
          </tr> 
          "; 
          for($i=1;$i<=$menuSet->GetMenuItems();$i++){ 
           $func = "GetMenuLink".$i; 
           /* $menuItemValue = $menuSet->{$func}(); 
           if (!$menuItemValue) { 
            continue; 
           } */ 
           echo " 
            <tr> 
            <td>$i</td> 
            <td><input type='text' name='' value='".$menuSet->$func()."'/></td> 
            <td> 
             <a title='Edit' href='itemedit.php?i=$i'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp; 
             <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a> 
            </td> 
            </tr> 
           "; 
          } 
          echo " 
          </table> 
         </div> 
         <!-- /.box-body --> 
         </div> 
         <!-- /.box --> 
        </div> 
        </div> 
      </section> 
     </div> 
    "; 
    }else{ 
     echo " 
     <div class='content-wrapper'> 
      <section class='content-header'> 
       <h1> 
        Menu Settings 
        <small></small> 
       </h1> 
       <ol class='breadcrumb'> 
        <li class='active'>menusettings.php</li> 
       </ol> 
      </section> 
      <section class='content' style='text-align:center;'> 
       <h5><strong> 
        You need to adjust the <span style='color:red'>menu_name</span> in the url first. So go back to <a href='dashboard.php'>dashboard</a> page.</br> 
       </strong></h5> 
      </section> 
     </div> 
     "; 
    } 
    ?> 

所以你可以看到我用TA for循環來檢索從MySQL表中的數據: (起點爲第43行)

for($i=1;$i<=$menuSet->GetMenuItems();$i++){ 
           $func = "GetMenuLink".$i; 
           /* $menuItemValue = $menuSet->{$func}(); 
           if (!$menuItemValue) { 
            continue; 
           } */ 
           echo " 
            <tr> 
            <td>$i</td> 
            <td><input type='text' name='' value='".$menuSet->$func()."'/></td> 
            <td> 
             <a title='Edit' href='itemedit.php?i=$i'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp; 
             <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a> 
            </td> 
            </tr> 
           "; 
          } 

這裏是表,其CONTA INS項目名稱:

print screen 2

這裏是類(Menu.class.php),我已經使用這個:

<?php 
    class Menus 
    { 
     public $id,$mname,$menui,$menul1,$menul2,$menul3,$menul4,$menul5,$menul6,$menul7,$menul8,$menul9,$menul10,$menul11,$menul12,$menul13; 
     public function __construct() 
     { 
      $this->db = new Connection(); 
      $this->db = $this->db->dbConnect(); 
     } 
     public function GetMenus() 
     { 
      if(empty($name)) 
      { 
       $menu = $this->db->prepare("select * from menu_nav"); 
       $menu->execute(); 
       $menu_array = array(); 
       while($row = $menu->fetch()) 
       { 
        $menu_array[] = $row; 
       } 
       return $menu_array; 
      } 
      else 
      { 
       header("Location: php/includes/errors/008.php"); 
       exit(); 
      } 
     } 
     public function SelectMenuById($id) 
     { 
      if(!empty($id)) 
      { 
       $mnu = $this->db->prepare("select * from menu_nav where id = ?"); 
       $mnu->bindParam(1,$id); 
       $mnu->execute(); 
       while($row = $mnu->fetch()) 
       { 
        $this->id   = $row['id']; 
        $this->mname  = $row['menu_name']; 
        $this->menui  = $row['menu_items']; 
        $this->menul1  = $row['menu_link_1']; 
        $this->menul2  = $row['menu_link_2']; 
        $this->menul3  = $row['menu_link_3']; 
        $this->menul4  = $row['menu_link_4']; 
        $this->menul5  = $row['menu_link_5']; 
        $this->menul6  = $row['menu_link_6']; 
        $this->menul7  = $row['menu_link_7']; 
        $this->menul8  = $row['menu_link_8']; 
        $this->menul9  = $row['menu_link_9']; 
        $this->menul10  = $row['menu_link_10']; 
        $this->menul11  = $row['menu_link_11']; 
        $this->menul12  = $row['menu_link_12']; 
        $this->menul13  = $row['menu_link_13']; 
       } 
      } 
      else 
      { 
       header("Location: php/includes/errors/009.php"); 
       exit(); 
      } 
     } 
     public function DeleteMenu($id) 
     { 
      if(!empty($id)) 
      { 
       $adm = $this->db->prepare("delete from menu_nav where id = ?"); 
       $adm->bindParam(1,$id); 
       $adm->execute(); 
      } 
      else 
      { 
       header("Location: php/includes/errors/010.php"); 
       exit(); 
      } 
     } 
     public function DeleteMenuItem($i) 
     { 
      if(!empty($i)) 
      { 
       $adm = $this->db->prepare("UPDATE menu_nav SET menu_link_$i = NULL"); 
       $adm->bindParam(1,$i); 
       $adm->execute(); 
      } 
      else 
      { 
       header("Location: php/includes/errors/011.php"); 
       exit(); 
      } 
     } 
     public function EditMenuItem($i,$e) 
     { 
      if(!empty($i)) 
      { 
       $adm = $this->db->prepare("UPDATE menu_nav SET menu_link_$i = $e"); 
       $adm->bindParam(1,$i); 
       $adm->execute(); 
      } 
      else 
      { 
       header("Location: php/includes/errors/012.php"); 
       exit(); 
      } 
     } 
     public function GetId() 
     { 
      return $this->id; 
     } 
     public function GetMenuName() 
     { 
      return $this->mname; 
     } 
     public function GetMenuItems() 
     { 
      return $this->menui; 
     } 
     public function GetMenuLink1() 
     { 
      return $this->menul1; 
     } 
     public function GetMenuLink2() 
     { 
      return $this->menul2; 
     } 
     public function GetMenuLink3() 
     { 
      return $this->menul3; 
     } 
     public function GetMenuLink4() 
     { 
      return $this->menul4; 
     } 
     public function GetMenuLink5() 
     { 
      return $this->menul5; 
     } 
     public function GetMenuLink6() 
     { 
      return $this->menul6; 
     } 
     public function GetMenuLink7() 
     { 
      return $this->menul7; 
     } 
     public function GetMenuLink8() 
     { 
      return $this->menul8; 
     } 
     public function GetMenuLink9() 
     { 
      return $this->menul9; 
     } 
     public function GetMenuLink10() 
     { 
      return $this->menul10; 
     } 
     public function GetMenuLink11() 
     { 
      return $this->menul11; 
     } 
     public function GetMenuLink12() 
     { 
      return $this->menul12; 
     } 
     public function GetMenuLink13() 
     { 
      return $this->menul13; 
     } 
    } 
?> 

所以一切工作正常,除了一兩件事。 ..

問題是我無法更新和更改菜單項,因爲我提供了一個輸入字段,我不知道如何通過itemedit.php頁面傳遞該字段的數據n它的菜單導航ID:

<tr> 
           <td>$i</td> 
           <td><input type='text' name='' value='".$menuSet->$func()."'/></td> 
           <td> 
            <a title='Edit' href='itemedit.php?i=$i'><span class='glyphicon glyphicon-pencil'></span></a>&nbsp; 
            <a title='Remove' href='itemdelete.php?i=$i'><span class='glyphicon glyphicon-remove'></span></a> 
           </td> 
           </tr> 

所以請任何想法,建議或解決方案呢?

而且itemedit.php是這樣也:

if (isset($_GET['i'])){ 
     $i = $_GET['i']; 
     $e = // value of the updated input 
     $editItem = new Menus(); 

     if(isset($_POST['yes'])){ 
      $editItem->EditMenuItem($i,$e); 
      echo '<META HTTP-EQUIV="Refresh" Content="0; URL=menus.php">'; 
      exit; 
     } 
     if(isset($_POST['no'])){ 
      echo '<META HTTP-EQUIV="Refresh" Content="0; URL=menus.php">';  
      exit; 
     } 
echo ... 

回答

3

有沒有<form>封裝輸入元素。另外,您還可以使用<input type="submit" ... >元素代替錨標記,將用戶輸入的數據發送到itemedit.php頁面。因此,改變你的for循環通過以下方式,

for($i=1;$i<=$menuSet->GetMenuItems();$i++){ 
    $func = "GetMenuLink".$i; 
    ?> 
     <tr> 
      <form action="itemedit.php?i=<?php echo $i; ?>" method="post"> 
     <td>$i</td> 
     <td><input type='text' name='e' value='<?php echo $menuSet->$func(); ?>'/></td> 
     <td> 
      <input type='submit' value='Edit' class='glyphicon glyphicon-pencil' />&nbsp; 
      </form> 
      <a title='Remove' href='itemdelete.php?i=<?php echo $i; ?>'><span class='glyphicon glyphicon-remove'></span></a> 
     </td> 
     </tr> 
    <?php 
} 

而且在itemedit.php頁面,流程表單的通過以下方式提交的數據,

if (isset($_GET['i'])){ 
    $i = $_GET['i']; 
    $e = $_POST['e']; 
    $editItem = new Menus(); 
    // update the menu items 
    $editItem->EditMenuItem($i,$e); 
    ... 

更新(1):

您準備好的聲明和bindParam()方法調用也是錯誤的。這些應該是這樣的:

$adm = $this->db->prepare("UPDATE menu_nav SET menu_link_$i = ?"); 
$adm->bindParam(1, $e, PDO::PARAM_STR); 
+0

然後會出現這樣的錯誤:未定義指數:上@wrephebiajou是否更換*文本輸入*元素爲'e'了'name'屬性行4 itemedit.php – wrephebiajou

+0

è? –

+0

Oop,我犯了錯誤,很好的答案的人,非常感謝很多很多:) – wrephebiajou

相關問題