gpt4 book ai didi

php - 表单正在向数据库提交多个条目而不是单个条目

转载 作者:行者123 更新时间:2023-11-28 03:12:00 26 4
gpt4 key购买 nike

我的代码基本上覆盖了学生向特定教授提交的请求(对于满员的类(class))。假设 2 个学生请求覆盖同一类(class),当一位教授登录时,代码获取两个覆盖请求并选择接受/拒绝,当我作为教授接受/拒绝单个覆盖请求时,它确实两个覆盖请求的操作,而不是我选择的那个。

基本上它不会接受/拒绝所选择的请求,它对所有覆盖执行相同的操作。

代码:

<?php
} else if ($usertype == 1) {
$server = "";
$user = "";
$pass = "";
$db = "";
$db2 = "";
$db3 = "";
$user1 = $_SESSION['username'];
$mysqli = new Mysqli($server, $user, $pass, $db) or mysqli_error($mysqli);
$mysqli2 = new Mysqli($server, $user, $pass, $db2) or mysqli_error($mysqli);
$mysqli3 = new Mysqli($server, $user, $pass, $db3) or mysqli_error($mysqli);

$status= $mysqli->query("SELECT status FROM Overrides WHERE professor = '$user1'")->fetch_object()->status;
$overrides = $mysqli->query("SELECT * FROM Overrides WHERE professor = '$user1'");
$num_rows = mysqli_num_rows($overrides);
?>
<form method="post" action="dbheads.php" name="HF" id="HF" autocomplete="off">
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('HF').action = action;
document.getElementById('HF').submit();
}
</script>
<?php if ($status == 1) {

echo "&nbsp;Overrides today: " . $num_rows;
?>
<?php
while($row = mysqli_fetch_array($overrides)) { ?>
<fieldset> <?php
echo "First Name:&nbsp;&nbsp; " . $row['name'] . "<br />";
echo "<br />Mid. Name:&nbsp;&nbsp; " . $row['mname'] . "<br />";
echo "<br />Fam. Name:&nbsp;&nbsp; " . $row['fname'] . "<br />";
echo "<br />Student ID:&nbsp;&nbsp;&nbsp;&nbsp;" . $row['sid'] . "<br />";
echo "<br />Scolarship:&nbsp;&nbsp;&nbsp;&nbsp; " . $row['sc'] . "<br />";
echo "<br />Phone No:&nbsp;&nbsp;&nbsp;&nbsp; " . $row['phone'] . "<br />";
echo "<br />Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['email'] . "<br />";
echo "<br />Subject:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['subject'] . "<br />";
echo "<br />Section:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['section'] . "<br />";
echo "<br />Semester:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $row['semester'] . "<br />";

$name = $row['name'];
echo "<input type='hidden' name='name' value='$name'>";
$mname = $row['mname'];
echo "<input type='hidden' name='mname' value='$mname'>";
$fname = $row['fname'];
echo "<input type='hidden' name='fname' value='$fname'>";
$sid = $row['sid'];
echo "<input type='hidden' name='sid' value='$sid'>";
$sc = $row['sc'];
echo "<input type='hidden' name='sc' value='$sc'>";
$phone = $row['phone'];
echo "<input type='hidden' name='phone' value='$phone'>";
$email = $row['email'];
echo "<input type='hidden' name='email' value='$email'>";
$subject = $row['subject'];
echo "<input type='hidden' name='subject' value='$subject'>";
$section = $row['section'];
echo "<input type='hidden' name='section' value='$section'>";
$semester = $row['semester'];
echo "<input type='hidden' name='semester' value='$semester'>";

?>
<br />
<div>
<label for="comments" accesskey="c">Notes & Comments:</label><br />
<input type="textarea" name="comments" id="comments" cols="35" rows="10">
<br>
</div>
<br>
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('HF').action = action;
document.getElementById('HF').submit();
}
</script>

...

<input type="button" onclick="submitForm('dbheads.php')" value="Accept" />
<input type="button" onclick="submitForm('dbheads2.php')" value="Deny" /></form>

</fieldset>
<br>
<?php } }
?>
<br />

数据库头文件

<?php 
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<html>

<?php
$mysql_host = "";
$mysql_username = "";
$mysql_password = "r!~";
$mysql_database = "";
$user = $_SESSION['username'];
if (login_check($mysqli) == true) : ?>
<p>Welcome <?php echo htmlentities($user); ?>!</p>
<?php
$mysqli = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysqli_error());
$status = 2;

$stmt = $mysqli->prepare("UPDATE Overrides SET status=? WHERE username='$user'");
$stmt->bind_param("s", $status);
$stmt->execute();
echo htmlentities(accepted);
?>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
</p>
<?php endif; ?>

</html>

bheads2.php

<html>

<?php
$mysql_host = "";
$mysql_username = "";
$mysql_password = "";
$mysql_database = "";
$user = $_SESSION['username'];
if (login_check($mysqli) == true) : ?>
<p>Welcome <?php echo htmlentities($user); ?>!</p>
<?php
$mysqli = new Mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database) or die(mysqli_error());
$status = 5;

$stmt = $mysqli->prepare("UPDATE Overrides SET status=? WHERE username='$user'");
$stmt->bind_param("s", $status);
$stmt->execute();
echo htmlentities(denied);
?>
<?php else : ?>
<p>
<span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
</p>
<?php endif; ?>

</html>

关于如何解决这个问题的任何帮助?我是初学者所以请忽略乱码。

最佳答案

看来您正在使用以下查询更新数据库

$stmt = $mysqli->prepare("UPDATE Overrides SET status=? WHERE username='$user'")

这只是说用户名在哪里登录或使用该页面将更新为您选择的状态,您是否为每行覆盖设置了唯一标识符? Override_ID 也许吧。

如果是这样,我会在您的第一个页面上获取该数据并将其像其他数据一样放入隐藏输入中,然后使用以下查询:

$ovid = $_POST['ovid'];
$stmt = $mysqli->prepare("UPDATE Overrides SET status=? WHERE override_id='$ovid'")

编辑:

您似乎还在更新页面上的 WHERE username='$user' 而不是 WHERE professor='$user'

关于php - 表单正在向数据库提交多个条目而不是单个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30141717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com