我的代码从数据库中提取条目并将它们打印成单独的表单,以便我可以使用不同操作的按钮接受或拒绝它们中的每一个。每当我在其中一个表单上执行操作时,它都不会在同一表单上执行操作,而是从数据库中的第一个条目开始按顺序移动。
在上图中,如果我接受第二张表格,它将在第一张表格上执行该功能。
代码:
<?php
} else if ($usertype == 1) {
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
$server = "";
$user = "";
$pass = "r=~";
$db = "";
$db3 = "aukwizcq_professors";
$user1 = $_SESSION['username'];
$mysqli = new Mysqli($server, $user, $pass, $db) or mysqli_error($mysqli);
$mysqli5 = new Mysqli($server, $user, $pass, $db3) or mysqli_error($mysqli);
$name= $mysqli5->query("SELECT name FROM professor WHERE username= '$user1'")->fetch_object()->name; //taking name from list of professors that are equal to the userid logged in (if hes a professor, it will show, if not 0)
$overrides = $mysqli->query("SELECT * FROM Overrides WHERE professor= '$name' AND status ='1'");
$num_rows = mysqli_num_rows($overrides);
echo " Pending Overrides: " . $num_rows;
?>
<?php
while($row = mysqli_fetch_array($overrides)) { ?>
<fieldset>
<form method="post" action="dbheads.php" name="HF" id="HF" autocomplete="off">
<?php
echo "First Name: " . $row['name'] . "<br />";
echo "<br />Mid. Name: " . $row['mname'] . "<br />";
echo "<br />Fam. Name: " . $row['fname'] . "<br />";
echo "<br />Student ID: " . $row['sid'] . "<br />";
echo "<br />Scolarship: " . $row['sc'] . "<br />";
echo "<br />Phone No: " . $row['phone'] . "<br />";
echo "<br />Email: " . $row['email'] . "<br />";
echo "<br />Subject: " . $row['subject'] . "<br />";
echo "<br />Section: " . $row['section'] . "<br />";
echo "<br />Semester: " . $row['semester'] . "<br />";
echo "<br />Professor: " . $row['professor'] . "<br />";
$id = $row['id'];
echo "<input type='hidden' name='id' value='$id'>";
$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'>";
//$professor = $row['professor'];
// echo "<input type='hidden' name='professor' value='$professor'>";
?>
<br />
<div>
<label for="comments" accesskey="c">Notes & Comments:</label><br />
<textarea name="comments" id="comments" cols="50" rows="5"></textarea>
<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
}
?>
dbheads.php
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<html>
<?php
$mysql_host = "localhost";
$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 = 2;
$id = $_POST['id'];
$stmt = $mysqli->prepare("UPDATE Overrides SET status=? WHERE id='$id'");
$stmt->bind_param("s", $status);
$stmt->execute();
print 'Error : ('. $mysqli->errno .') '. $mysqli->error;
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>
请帮忙修理?
答案 0 :(得分:0)
我心中有一件事: 你正在使用JS
<script type="text/javascript">
function submitForm(action)
{
document.getElementById('HF').action = action;
document.getElementById('HF').submit();
}
</script>
以表格ID提交表格。但是您为循环中生成的所有表单提供了相同的ID。 JS正在执行第一次身份证件。
答案 1 :(得分:0)
这是因为您通过ID发送表单,并且您对所有表单都有相同的ID(无效)。
如果你要为这样的表单创建唯一的id:
<form method="post" name="HF" id="HF_<?php echo $row['id'] ?>" autocomplete="off">
在你改变你的JavaScript之后:
document.getElementById('HF_<?php echo $row['id'] ?>').action = action;
document.getElementById('HF_<?php echo $row['id'] ?>').submit();
它会正常工作
如果我几乎无法改进您的代码,我会为您提供以下建议:
如果你使用 ,你不需要更多的JavaScript,你可以测试用PHP提交的按钮(最佳实践)。例如:
<form method="post" name="HF" id="HF_<?php echo $row['id'] ?>" autocomplete="off">
<input type="hidden" name="HF[id]" />
<input type="hidden" name="HF[name]" />
<input type="hidden" name="HF[mname]" />
<input type="hidden" name="HF[fname]" />
<input type="hidden" name="HF[sid]" />
<button type="submit" value="1" name="HF[accept]">Accept</button>
<button type="submit" value="1" name="HF[reset]">Reset</button>
</form>
和PHP测试
if(isset($_POST['HF'])){
if($_POST['HF']['accept']){
// do accept
}
elseif($_POST['HF']['reset']){
// do reset
}
}
如果您发现我不需要为每个操作设置单独的操作,并且我有表单前缀,这意味着我可以在此网址上发送多个表单,但只会处理一个