Jump to content

updating multiple records


BraisbyI
Go to solution Solved by Barand,

Recommended Posts

Hi,

I am trying to create an admin page for a local Gym Club to allow an Administrator to be able to update club prices which then show on different screens on the site.

 

I have been able to display the "Prices Admin" page which basically reads a MySQL database table (pricelist), display the description, member price and non-member price and allows the user to update any of these fields.

 

My problem is that when I try writing the data back to the database with the "UPDATE" statement it fails, what I mean is that nothing updates.  I have at the moment commented out the actual update statement and put in a "file_put_contents" command" to try and work out what is in the various fields before the UPDATE is run.  I find that the display/INPUT works perfectly well but when I do a foreach on the $record array variable I am getting strange results.

 

I may not have explained this very well but here are the relevant sections of my code;

 

// Display and Input section

 

<div id="admin-area">
    <br><br>
    <a class="admin-left">ADMIN (Class Screen Text & Prices)</a>
    <br>
    <div class="admin">
      <form name="prices" class="pure-form pure-g " action="?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
        <fieldset>
          <textarea id="price-data" name="price-data" rows="5" cols="90">
            <?php print(htmlentities($data)) ?>
          </textarea>
          <br><br>
          <label class="pure-u-1-3">  </label>
          <label class="pure-u-1-3"> <b>Members</b> </label>
          <label class="pure-u-1-3"> <b>Non-Members</b> </label>
          <?php
          $i = 0;
          while($i < $count)
          {
          ?>
            <input class="pure-u-1-3" id="price_label" type="text" name="records[$i][detail]" value="<?= htmlentities($records[$i]['detail'])?>" maxlength="40"  />
            <input class="pure-u-1-3" id="price_member" type="text" name="records[$i]" value="<?= htmlentities($records[$i]['price_member'])?>" maxlength="10" placeholder="Member Price" />
            <input class="pure-u-1-3" id="price_non" type="text" name="records[$i][nonmember]" value="<?= htmlentities($records[$i]['price_nonmember'])?>" maxlength="10" placeholder="Non Member Price" />
            <br>
          <?php
            $i++;
          }
          ?>
          <div>
            <br>
            <input class="button-input" id="submit" type="submit" name="update" value="Update" />
          </div>
        </fieldset>
      </form>
    </div>
  </div>
 
This results in 11 records displayed on the screen correctly.
 
// In the update section I have removed the actual UPDATE and just used the "put_file_contents()" to show what is in the records array.
 
<?php
    if (isset($_POST['update']))
    {
      if($_POST['update']=='Update')
      {
         file_put_contents('codetest.txt', "File Count - " . $count . "\n", FILE_APPEND);
        
        $i = 0; 
        foreach ($_POST[records] as $row)
        {         
          file_put_contents('codetest.txt', $i . " " . $row['detail'] . " " . $row['member'] . " " . $row['nonmember'] . "\n", FILE_APPEND);
          $i++;
        }    

 

 

When run the contents in the codetest.txt is;

  File Count - 11

  0  Party: Airtrack  40.00  30.00

 

The strange thing here is that the $i = 0 is the first record but the $row['detail'], $row['member'] and $row['nonmember'] details of "Party: Airtrack",  40.00, 30.00 are from record 11 ie the last record.   

 

If you want to see the UPDATE code I have been using please let me know and I will post it up here.

 

Any help would be appreciated here.

 

Ian

Link to comment
Share on other sites

Barand, many thanks for this, made the change and immediately got the required output from the "file_put_contents", really should have picked that one out but sometimes you can look at these things too long.

 

Just putting back the PDO statements to test the MySQL update but the data is now all there so I see no problem with this. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.