【发布时间】:2013-01-28 02:34:16
【问题描述】:
我将使用我以前发布的代码,因为我正在开发同一个程序。我想要的是如何才能将所有选定的值保存在一行中,用户的学生 ID 不会重复。请帮助...
<?php session_start(); ?>
<?php
//server info
$server = 'localhost';
$user = 'root';
$pass = 'root';
$db = 'user';
// connect to the database
$mysqli = new mysqli($server, $user, $pass, $db);
// show errors (remove this line if on a live site)
mysqli_report(MYSQLI_REPORT_ERROR);
?>
<?php
$_SESSION['username'];
$voter = $_SESSION['username'];
echo 'Student ID: '. $voter.'';
echo "<br />";
if ($_POST['representatives']){
$check = $_POST['representatives'];
foreach ($check as $ch){
global $voter;
$mysqli->query("INSERT INTO sample (studentid, candidate1) VALUES ('".$voter."', '". $ch ."')");
echo $ch. "<br>";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<script type="text/javascript">
<!--
function get_representatives_value()
{
for (var i=0; i < document.list.representatives.length; i++)
{
if (document.list.representatives[i].checked)
{
return document.getElementById('candidates').innerHTML = document.list.representatives[i].value
}
}
}
//-->
</script>
title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="candidate.css" rel="stylesheet" type="text/css">
</head>
<body> <p id="txt"></p>
<form name="list" action="president2.php" method="post" onSubmit="return get_representatives_value()">
<div id="form">
<?php
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM candidate_info WHERE position= 'representatives' AND department ='CCEITE' ORDER BY cand_id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border='1' cellpadding='10'>";
// set table headers
echo "<tr><th>Student ID</th><th>Candidate ID</td><th>Course</th><th colspan = '3'>Name</th></tr>";
while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->cand_studid . "</td>";
echo "<td>".$row->cand_id."</td>";
echo "<td>" . $row->course . "</td>";
echo "<td coslpan ='5'>" . $row->fname . " ". $row->mname ." ". $row->lname ." </td>";
echo "<td><input type ='checkbox' name='representatives[]' id='". $row->cand_studid ."' value='" . $row->cand_studid . "' onchange='get_representatives_value()' /></td>";
echo "</tr>";
}
echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
echo "<input type='submit' name='representatives value='Submit' />";
?>
</div>
</form>
<table>
<tr><td>Preview List</td></tr>
<tr><td>Candidates: </td><td id="candidates"> </td></tr>
</table>
</body>
</html>
这是我的输出和选中复选框的预览
现在这是我的数据库的预览。这是我从上面的预览中选择的结果,学生证在我的桌子上重复。
我想要的就是这样保存
还有一件事,我如何预览我在复选框上选择的所有内容,这是我的预览输出,表格下方是用户单击复选框时的候选预览列表。但它只返回一个且仅返回最后一个选择的值,因为选择多个复选框将被打印。我怎么能在数组中应用这个方法,因为这个预览,我删除了输入类型 name='representative' 上的 '[]' 并且它可以工作,但在存在 '[]' 的情况下不行。
【问题讨论】:
-
如果我理解正确,您想为学生添加候选人吗?一个学生可以有多少候选人?您希望保存它的方式不符合数据库规范化规则。 en.wikipedia.org/wiki/Database_normalization
-
@HMR.. 在我的样本中,一个学生只能有两个候选人,是的,我违反了规范化,我知道这一点,这就是我请求帮助纠正它的原因,因为我不知道怎么解决。
-
如果只有固定数量的候选值,您可以将它们作为学生表中的列。我认为您正在为学生寻找更新声明,并在处理帖子时更新 2 个候选列。
-
@HMR .. 如果是这样,我想怎么做?我还是不知道..
-
我添加了一些关于如何更新示例表的伪代码的答案。如果您想规范化数据库,您必须将候选人放在单独的表中,删除具有特定学生 ID 的条目并插入用户提交表单时发布的条目。您可能想阅读有关规范化的良好解释(维基百科文章不太好)并查看有关连接表的 mysql 文档。
标签: php javascript