2016-06-16 201 views
2

我正在創建用於管理數據庫的用戶界面。每個條目都有兩個單選按鈕,一個用於驗證(並隨後通過電子郵件發送給相關機構),另一個用於刪除條目。代碼如下所示,粗體部分是涉及此功能的部分。 有幾個問題: 1)有沒有可能這樣做沒有表單屬性?如果是這樣,我該怎麼做?傳遞php變量

2)使用form屬性,我使用'handle.php'來刪除每個條目。在「handle.php」的語法包括這條線現在

$sql="DELETE FROM rti WHERE ID=x"; //x here is the ID number to delete. 

,如何傳遞ID的值,以及從我的接口,這樣的代碼上面的行刪除對應於該按鈕的條目那被壓了?

<html> 
    <head> 
     <style> 
     * { 
      margin: 0px; 
      padding: 0px; 
     } 

     div.topbar { 
      position: relative; 
      background-color: black; 
      height: 45px; 
      text-align: center; 
      font-family: Calibri; 
      font-size: 30px; 
      padding-top: 10px; 
     } 

     div.topbar img { 
      position: absolute; 
      left: 10px; 
      top: 0px; 
     } 

     div.topbar span { 
      color: white; 
     } 

     div.container { 
      background-color: #a5a5a5; 
      width: 100%; 
      height: 100%; 
      color: white; 
     } 

     div.data { 
      padding: 20px; 
     } 

     table { 
      background-color: #b1b1b1; 
      border-collapse: collapse; 
     } 


     th, td { 
      text-align: center; 
      border: solid 1px black; 
      margin: 0px; 
     } 

     th.one { 
      width: 50px; 
     } 

     th.two{ 
      width: 150px; 
     } 

     th.three { 
      width: 75px; 
     } 

     th.four { 
      width: 200px; 
     } 

     div.validation { 
      background-color: #b5b5b5; 
      width: 200px; 
      height: 100%; 
      position: relative; 
      left: 1100px; 
      bottom: 300%; 
      text-align: center; 
      border-left: solid 1px black; 
     } 
     </style> 
    </head> 
    <body> 
     <div class = "topbar"><img src="logo.png"><span>RTI DATABASE</span></div> 
      <div class = "container"> 
       <div class ="data"> 
        <?php 
        $servername="localhost"; 
        $username="root"; 
        $password=''; 
        $conn=mysql_connect($servername, $username, $password) or die ("Error connecting to mysql server: ".mysql_error()); 
        $dbname = 'bsp'; 
        mysql_select_db($dbname, $conn) or die ("Error selecting specified database on mysql server: ".mysql_error()); 
        $sql="SELECT * FROM rti"; 
        $result=mysql_query($sql) or die ("Query to get data from firsttable failed: ".mysql_error()); 
        echo "<table>"; 
        echo "<tr>"; 
        echo '<th class="one">ID</th> 
         <th class="two">Name</th> 
         <th class="three">Board</th> 
         <th class="four">Query</th> 
         <th class="five">Validate</th> 
         <th class="six">Delete</th> 
         <th class="seven">Submit</th>'; 

        echo "</tr>"; 
        while ($row=mysql_fetch_array($result)) { 
         $id=$row["ID"]; 
         $name=$row["name"]; 
         $board=$row["board"]; 
         $query=$row["query"]; 
         echo "<tr>"; 
         echo "<td>$id</td> 
          <td>$name</td> 
          <td>$board</td> 
          <td>$query</td> 
          **<form action='handle.php' method='POST'> 
           <td><input type='radio' name='option' value='validate'></td> 
           <td><input type='radio' name='option' value='delete'></td> 
           <td><input type='submit' value='Submit' name='submit'> 
          </form></td>"; 
         echo "</tr>";** 
        } 
       echo "</table><br>"; 
       ?> 
      </div> 
     </div> 
    </body> 
</html> 

回答

1
**<form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td> 
    <td><input type='radio' name='option' value='delete'> 
    <input type='hidden' name="id" value='".$id."'> 
    </td> 
    <td><input type='submit' value='Submit' name='submit'></form></td>"; 

在handle.php:

$id = $_POST['id']; 
+0

我試過你的解決方案,但是handle.php給了我錯誤Undefined index:id,對應於你建議的行。有什麼建議麼? –

+0

確保'id'在您的發佈請求中? 'print_r($ _ POST);' –

+0

另外,你可能錯過了我的代碼中的一件事。這也造成了我的困惑。我在echo語句中使用了雙引號(「」),而在內部,對於方法,名稱和值等html屬性,我使用了單引號。我想這可能會造成問題。 –

0

使用像<input type='radio' name='option[your_id]' value='validate'>,以確定該項目。 Like:<input type='radio' name='option[$id]' value='validate'>

1

在你的表單中添加一個隱藏類型input與每個項目的id值,所以每次用戶點擊按鈕的id發送。就像這樣:

<form action='handle.php' method='POST'><td><input type='radio' name='option' value='validate'></td> 

<input type = 'hidden' name="id" value='".$row['id']."'><td> 
      <input type='radio' name='option' value='delete'></td> 
     <td><input type='submit' value='Submit' name='submit'></form></td>";