BraisbyI Posted September 13, 2014 Share Posted September 13, 2014 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 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted September 13, 2014 Share Posted September 13, 2014 you mention a MySQL database but I don't see any attempts to read from one or write to (update) one. Did you forget to post that part? And - please follow forum rules and post the code in the proper tags. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted September 13, 2014 Solution Share Posted September 13, 2014 Where you have name="records[$i][detail]" you need name="records[<?=$i?>][detail]" otherwise only the last set of inputs is posted instead of all of them with unique numeric indexes Quote Link to comment Share on other sites More sharing options...
BraisbyI Posted September 13, 2014 Author Share Posted September 13, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.