php - Fetching and submitting data to database -


my code fetches entries database , prints them separate forms accept or deny each of them using buttons of different actions. whenever perform action on 1 of forms, not operation on same form instead moves order starting first entry in database.

enter image description here

in picture above, if accepted second form, perform function on 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 professor username= '$user1'")->fetch_object()->name;   //taking name list of professors equal userid logged in (if hes professor, show, if not 0) $overrides = $mysqli->query("select * overrides professor= '$name' , 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=? 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 not authorized access page.</span> please <a href="index.php">login</a>.             </p>         <?php endif; ?>  </html> 

any fixing please?

it because sending form id , have same id (it invalid) forms.

if make unique id form this:

<form method="post" name="hf" id="hf_<?php echo $row['id'] ?>" autocomplete="off"> 

and after change javascript this:

document.getelementbyid('hf_<?php echo $row['id'] ?>').action = action; document.getelementbyid('hf_<?php echo $row['id'] ?>').submit(); 

it working

if can little improve code have suggestions you:

if use don't need more javascript , can test button submitted in php (best practice). example:

<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> 

and php testing

if(isset($_post['hf'])){      if($_post['hf']['accept']){          // accept      }      elseif($_post['hf']['reset']){          // reset      }  } 

if see don't need set separate actions per each action , have form prefix , means can send multiple forms on url 1 processed


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -