我正在研究中学的成绩管理系统。
我不想使用他/她的注册号作为唯一ID为特定用户输入相同的主题。即如果我为约翰输入数学,我不能再为约翰输入数学。
以下是我的代码:
<?php
$msg = "";
$connection = mysqli_connect("localhost", "root", "", "result_mg_db");
if (isset($_POST['submit'])) {
$regno = addslashes(strip_tags($_POST['regno']));
$subj = addslashes(strip_tags($_POST['subject']));
$examscore = addslashes(strip_tags($_POST['examscore']));
$cascore = addslashes(strip_tags($_POST['cascore']));
$date = date("Y-m-d h:i:sa");
$confirm_subj = mysqli_query($connection, "SELECT subject FROM jss1_resulttbl WHERE regno = '$regno'");
if ($confirm_subj == $_POST['subject']) {
$msg = "This subject has been registered";
} else {
if ($cascore == ""|| $examscore == ""|| $subj == "" || $regno == "" ) {
$msg = "Please fill all fields";
} else {
$reg_subj = mysqli_query($connection, "INSERT INTO jss1_resulttbl VALUES ('', '$regno', '$subj', '$examscore', '$cascore', '$date')");
if (!$reg_subj) {
echo "Subject Registration failed ";
}else {
$msg = "You have Successfully entered score for " . $subj . "<br/>";
}
}
}
}
?>
答案 0 :(得分:1)
首先运行查询以检查数据库是否已有一行 注册号和主题。然后将数据插入数据库。
$result = mysqli_query("SELECT * FROM jss1_resulttbl WHERE regno='$regno' AND subject='$subject'");
$num_rows = mysql_num_rows($result);
if (!$num_rows) {
// Here write query to insert data to database
}