简体   繁体   中英

Submitting multiple forms to database one by one

my code fetches entries from the database and prints them into separate forms so that i could accept or deny each of them using buttons of different actions. Whenever i perform an action on one of the forms, it would not do the operation on the same form but instead it moves by order starting from the first entry in the database.

在此处输入图片说明

In the picture above, if i accepted the second form, it would perform the function on the first form.

Code:

<?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 "&nbsp;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:&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 />";
                  echo "<br />Professor:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " . $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>

Any help for fixing please?

Your JS will always just submit the values contained in the last <form> the way you have it set up.

The biggest problem here is that you have multiple <form> elements on your page but nothing ties your button to a particular form.

I'd suggest you use JQuery to send an AJAX request for each accept or reject instead.

The following articles might help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM