2011-04-13 42 views
3

這裏的前端設計師不是PHP開發人員,我真的需要幫助。需要建立一個系統,允許客戶登錄到自己的區域來更新表格。該表格將包含約20條記錄,每行將用單選按鈕編輯(4個選項)。只有2列,一個用於ID,另一個稱爲狀態(可編輯數據)。使用單選按鈕更新數據庫的簡單PHP任務

查看器將只能看到客戶端所做的更改以及根據狀態對該行進行的背景顏色更改,這意味着可編輯部分必須保存2個數據(整數值才能更改顏色和名稱的狀態,例如單選按鈕改變顏色的值,以及用於將文本回顯到單元中的按鈕的標籤)。將處理大約登錄系統後...

數據庫設置:

Database: test 
Table: fire_alert 
Structure: 
    id (INT, PRIMARY); color(INT); warning(VACHAR); 

connect.php

<?php 
// Database Variables (edit with your own server information) 
$server = 'localhost'; 
$user = 'root'; 
$pass = 'root'; 
$db = 'test'; 

// Connect to Database 
$connection = mysql_connect($server, $user, $pass) 
    or die("Could not connect to server ... \n" . mysql_error()); 
mysql_select_db($db) 
    or die("Could not connect to database ... \n" . mysql_error()); 
?> 

view.php

<div id="container"> 
<?php 
// connect to the database 
include('connect.php'); 
include("header.php"); 

// get results from database 
$result = mysql_query("SELECT * FROM fire_alert") 
    or die(mysql_error()); 
echo "Current date - " . date("Y/m/d") . "<br /><br />"; 

// display data in table 
echo "<table>"; 
echo "<tr> <th>Area</th> <th>Status</th></tr>"; 

// loop through results of database query, displaying them in the table 
while($row = mysql_fetch_array($result)) { 
    // echo out the contents of each row into a table 
    echo "<tr>"; 
    echo '<td>' . $row['id'] . '</td>'; 
    echo '<td>' . $row['warning'] . '</td>'; 
    echo "</tr>"; 
} 

// close table> 
echo "</table>"; 

echo '<td><a href="edit.php' . $row['id'] . '">Change Area Status</a></td>'; 
?> 
</div> 
<body> 
</body> 
</html> 

edit.php(客戶端訪問僅)沒有動態da TA改變的是,硬編碼的HTML至今:

<?php include("header.php"); ?> 
<?php include("connect.php"); ?> 

<div id="container" class="edit"> 
<div id="radio1"> 

<? 
$result = mysql_query("SELECT * FROM fire_alert") 
    or die(mysql_error()); 
echo "Current date - " . date("Y/m/d") . "<br /><br />"; 
?> 

<table class="edit"> 
    <tr><th>Area</th><th>Status</th></tr> 
    <tr> 
     <td>1</td> 
     <td> 
      <form method="post" action="edit.php"> 
       <input type="radio" id="radio1" name="radio" value="1" /><label for="radio1">Safe</label> 
       <input type="radio" id="radio2" name="radio" value="2" /><label for="radio2">Caution L1</label> 
       <input type="radio" id="radio3" name="radio" value="3" /><label for="radio3">Caution L2</label> 
       <input type="radio" id="radio4" name="radio" value="4" /><label for="radio4">Closed</label> 
      </form> 
     </td> 
    </tr> 
</table> 

</div> 
<?php echo '<td><a href="view.php' . $row['id'] . '">Update</a></td>'; ?> 
</div> 

回答

1

運行此更新(或我使用反正方式)最簡單的方法是通過利用JavaScript的。讓每一部分無線電塊以自己的形式自動提交onChange,幷包含隱藏變量以指示頁面提交。然後,只需編寫一個函數來更新數據庫,根據所輸入的用戶的ID(或不過你確認)及其選擇的選擇...

喜歡的東西:

if($_POST["submit"] == 1) 
{ 
    $id = $_POST["id"]; 
    $radio = $_POST["radio"]; 
    $query = "UPDATE fire_alert SET warning = '$radio' WHERE id = '$id'"; 
    $result = mysql_query($query) or die(mysql_error()); 
} 

你有一個問題你如何格式化你的view.php鏈接,該行應該如下:

'Update'; ?>」

雖然爲你打算,因爲它沒有提交表單,所以單選按鈕值不會通過上面不會有相當的工作...相反你應該做的:

<form method="post" action="edit.php"> 
    <input type="radio" id="radio1" name="radio" value="1" onChange="this.submit()"/><label for="radio1">Safe</label> 
    <input type="radio" id="radio2" name="radio" value="2" onChange="this.submit()"/><label for="radio2">Caution L1</label> 
    <input type="radio" id="radio3" name="radio" value="3" onChange="this.submit()" /><label for="radio3">Caution L2</label> 
    <input type="radio" id="radio4" name="radio" value="4" onChange="this.submit()"/><label for="radio4">Closed</label> 
    <input type="hidden" name="id" value="<?=$row["id"]?>" /> 
</form> 

而且你需要編輯你的php來反映這個差異