2011-04-13 49 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来反映这个差异