使用单选按钮更新数据库的简单PHP任务

时间:2011-04-13 00:44:01

标签: php mysql html-table radio-button

这里的前端设计师,不是PHP开发人员,我真的需要帮助。需要建立一个允许客户端登录自己的区域来更新表的系统。该表将有大约20条记录,每行将使用单选按钮进行编辑(4个选项)。只有2列,一列用于ID,一列称为状态(可编辑数据)。

观察者只会看到客户端所做的更改,并且背景颜色会根据状态更改为该行,这意味着可编辑部分必须保存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(仅限客户端访问)动态数据更改,目前为止已编码为硬编码的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 个答案:

答案 0 :(得分: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链接的格式设置有疑问,该行应如下所示:

'更新'; ?&GT;'

虽然上面的内容不太合适,因为它没有提交表单,所以单选按钮值不会被传递...而是你应该这样做:

<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以反映这种差异