我正在创建一个用于管理数据库的用户界面。每个条目都有两个单选按钮,一个用于验证(以及随后通过电子邮件发送给相关机构),另一个用于删除条目。代码如下所示,粗体部分是与此功能相关的部分。 几个问题: 1)没有表单属性可以这样做吗?如果是这样,我该怎么做?
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>
答案 0 :(得分: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'];
答案 1 :(得分: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>";
答案 2 :(得分:0)
使用类似<input type='radio' name='option[your_id]' value='validate'>
的内容来识别该项目。
喜欢:<input type='radio' name='option[$id]' value='validate'>