我有一个显示MySQL表数据的表。我有一个额外的列,为每个条目添加一个复选框。
是否可以将该列行与包含MySQL数据的列中的其他数据相关联?
按下' Save'然后将复选框条目保存到新的mysql表中。按钮。
这是一张图片,向您展示我的意思:
代码:
<?php
$connection = mysql_connect('localhost','admin','root');
if( isset($_POST['submit']) )
{
if( isset( $_POST['cb_change'] ) && is_array( $_POST['cb_change'] ))
{
foreach( $_POST['cb_change'] as $emp_number => $permission)
{
$sql = "UPDATE `rights` SET Permission='".mysql_real_escape_string($permission)."' WHERE emp_number='".mysql_real_escape_string($emp_number)."'";
echo __LINE__.": sql: {$sql}\n";
mysql_query( $sql );
}
}
}
?>
<p style="text-align: center;">
<span style="font-size:36px;"><strong><span style="font-family: trebuchet ms,helvetica,sans-serif;"><span style="color: rgb(0, 128, 128);">File Database - Administration Panel</span></span></strong></span></p>
<p style="text-align: center;">
</p>
<head>
<style type="text/css">
table, td, th
{
border:1px solid #666;
font-style:Calibri;
}
th
{
background-color:#666;
color:white;
font-style:Calibri;
}
</style>
</head>
<form method="post" action="admin.php">
<?php
if (!$connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('users', $connection);
//mysql_query('INSERT into rights(Emp_num, ID, Name, Surname) SELECT emp_number, employee_id, emp_firstname, emp_lastname FROM hs_hr_employee');
$result = mysql_query("SELECT emp_number, employee_id, emp_firstname, emp_lastname, Permissions FROM rights");
mysql_query("INSERT INTO rights (emp_number, employee_id, emp_firstname, emp_lastname)
SELECT emp_number, employee_id, emp_firstname, emp_lastname
FROM hs_hr_employee
ON DUPLICATE KEY UPDATE employee_id = VALUES(employee_id), emp_number = VALUES(emp_number)
");
$duplicates = mysql_query("SELECT emp_number, employee_id, emp_firstname, emp_lastname, count(*) FROM rights GROUP BY emp_number, employee_id, emp_firstname, emp_lastname having count(*) > 1");
$count = mysql_num_rows($duplicates);
if ($count > 0) {
while ($row = mysql_fetch_assoc($duplicates)) {
$field = $row["emp_number"];
$limit = $row["count(*)"] - 1;
mysql_query("DELETE FROM rights WHERE emp_number='$field' LIMIT $limit");
}
mysql_free_result($duplicates);
}
echo "<center>";
echo "<table >
<tr>
<th>Employee Number</th>
<th>ID</th>
<th>Name</th>
<th>Surname</th>
<th>Permissions</th>
<th>Change</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['emp_number'] . "</td>";
echo "<td>" . $row['employee_id'] . "</td>";
echo "<td>" . $row['emp_firstname'] . "</td>";
echo "<td>" . $row['emp_lastname'] . "</td>";
echo "<td>" . $row['Permissions'] . "</td>";
echo "<td> <select name='cb_change[]'><option value='all'>All</option> <option value='remote'>Remote Gaming</option> <option value='landbased'>Landbased Gaming</option> <option value='general'>General Gaming</option> </select> </td>";
echo "</tr>" ;
}
#echo "<td>" . $row['Change'] . "</td>";
echo "</table>";
echo "</center>";
#$_POST['cb_permissions'];
mysql_close($connection);
?>
<p style="text-align: center;">
</p>
<p style="text-align: center;">
</p>
<p style="text-align: right;">
<input name="Save_Btn" type="button" value="Save" />
</p>
</form>
非常感谢任何帮助。
答案 0 :(得分:0)
假设您指的是标题为“更改”的选择列表列之后的另一列。 使用this ...
在更改列后添加另一个表格单元格echo "<td> <input type="checkbox" name='fieldName[]' value="<?php echo $row['emp_number']; ?>" > </td>";
在您发布表单后,$ _POST ['fieldName']数组将包含已单击行的ID。
尝试并确保$ row ['emp_number']来自您的表主键,以确保该值是唯一的。