0

I'm working with two forms on the same page. both have the same select where the admin can select a user. for that, I'm using find_all() function I've created. which Select everything from the given table name. then I'm using while loop to populate select options. they are displaying on the first form correctly. but the second form is empty. I want to know why this is happening. any idea? thank you :)

<?php $users = find_all('ph_users'); ?>

// first form
<form action="credits_manage.php" method="post">
  <div class="form-group">
    <label for="credits_amount">Credits Amount</label>
    <input type="number" placeholder="Enter credits amount" id="credits_amount" class="form-control" required>
  </div>
  <div class="form-group">
    <label for="user">Select User</label>
    <select name="user" id="user" class="form-control" required>
      <option value="">Select...</option>
      <?php while ($user = mysqli_fetch_assoc($users)) { ?>
        <option value="<?php echo $user['user_id']; ?>"><?php echo $user['username']; ?></option>
      <?php } ?>
    </select>
  </div>
  <input type="submit" value="Give Credits" name="give-credits-submit" class="btn btn-primary waves-effect waves-light mt-2">
</form>

// second form
<form action="">
  <div class="form-group">
    <label for="credits_amount">Credits Amount</label>
    <input class="form-control" type="password" value="lachiweb" id="credits_amount" required>
  </div>
  <div class="form-group">
    <label for="remove_credits_user">Select User</label>
    <select name="remove_credits_user" id="remove_credits_user" class="form-control" required>
      <option value="">Select...</option>
      <?php while ($remove_credits_user = mysqli_fetch_assoc($users)) { ?>
        <option value="<?php echo $remove_credits_user['user_id']; ?>"><?php echo $remove_credits_user['username']; ?></option>
      <?php } ?>
    </select>
  </div>
  <div class="form-group">
    <label for="reason">Choose a Reason</label>
    <input class="form-control" type="password" value="lachiweb" id="reason" required>
  </div>
  <input type="submit" value="Remove Credits" name="update-password-submit" class="btn btn-primary waves-effect waves-light mt-2">
</form>
  • It's always better to use a name to the form and I don't know why you are using the same id in both input tags. – Hirumina Mar 12 '20 at 06:27
  • its either you create a tabular presentation of all the users, then on the right most column of the row, put the add/remove credits action. you'll only need one form then you can simply provide with user id to use (the usual crud like table). plus you get the benefit of having it paginated. or do a band aid solution to put all the fetched users into a variable then present them with your current two forms – Kevin Mar 12 '20 at 06:37
  • but to answer your question, the fetch on the second one doesn't work since on the first fetch it already reached the end of rows (it shifted on the end/last now), so on the second, there's nothing to fetch – Kevin Mar 12 '20 at 06:39
  • an alternative is to reset the pointer back to the first row before calling the fetch on the second https://www.php.net/manual/en/mysqli-result.data-seek.php#85629 – Kevin Mar 12 '20 at 06:43

1 Answers1

-1

I think its because you are using mysqli_fetch_assoc($users)) twice and thats not how it works i am not sure about this but i think you can only use it once because it has ready cycled through all the users so there is nothing else to circle through.

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
LoaiAkram
  • 81
  • 4