Jump to content

BraisbyI

New Members
  • Posts

    9
  • Joined

  • Last visited

BraisbyI's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. 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.
  2. 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
  3. kicken, OK my understanding of this was totally wrong, I will rewrite this program and while I am at it change other parts of this system to match. Many thanks for your help with this, I am always learning and this has really helped my understanding of the structure of PDO. Hopefully when I get this done correctly the problem will be fixed.
  4. mac_gyver It maybe my lack of understanding with PHP & PDO but I thought that any failure within the "try" statement would be reported by the "catch" statement which immediately follows it? I have the prepare, bind and execute inside the "try" statement with a "catch" immediately after it which has an echo to the screen that an error has occurred, it also outputs the error to a text file before issuing the rollback. I have this problem with or without the rollback being present. I do not understand how you can move the bind statements outside the foreach $data statement and expect anything to work, how does the data get to the record if the bind does not happen inside the foreach? Am I missing something here or is my understanding of this wrong? I do understand that somewhere something must be failing but I do not see how the rollback is happening without the error being reported as the only rollback statement present is within the catch statement which outputs the error to the screen and a text file before the rollback can be initiated. My Catch statement in full which is immediately after the try block is: catch(PDOException $ex) { echo "An Error Occured While Updating " . db_name . "!"; file_put_contents('PDOerror.txt', $ex->getMessage(), FILE_APPEND); echo $ex->getMessage(); $dbh->rollback(); } file_put_contents('PDOdata.txt', $data, FILE_APPEND); I did not put this in my last message because I assumed the " ...... output any error message ........ " line would show I am dealing with any error, the file_put_contents command after the catch sends the updated data held within $data to a text file, but this command is not initiated from within the catch. Thank you for your input, I do welcome any ideas on this problem.
  5. I thought this problem had been fixed by Guru, well the initial record read was fixed. My code has already allowed a user to login and has stored some user data in $_SESSION vars, I then use the userID to find the linked carer records (Name & Addresses). After help from Guru I got these records successfully output to the screen where the user is allowed to modify any of the data. After pressing "Submit" the data then needs writing back to the database and this is where my current problem is. I have output the data at different points to a text file and at each stage it is the updated data, I have even checked this data after what appears to be a successful "commit" but the screen immediately reverts to the original data and on checking the database I find that this has not changed. My code is as follows; The read which was corrected by Guru is: $usrid = $_SESSION['id']; $username = $_SESSION['username']; $usertype = $_SESSION['usertype']; try { $query = "SELECT * FROM carer WHERE valid_users_id = :usrid"; $stmt = $dbh->prepare($query); $stmt -> execute(array(':usrid' => $usrid)); $records = $stmt->fetchAll(PDO::FETCH_ASSOC); $carer_id1 = $records[0]['id']; $carer_id2 = $records[1]['id']; } catch(PDOException $ex) { error message output to screen and error file. } I then output this data to the screen with: <fieldset> <legend><h2>1st Parent / Carer</h2></legend> <label for="fname">First Name:</label> <input id="fname" type="text" size="20" name="records[0][firstname]" value="<?= htmlentities($records[0]['firstname'])?>" autofocus /> <input id="sname" type="text" size="30" name="records[0][surname]" value="<?= htmlentities($records[0]['surname'])?>" /> ....... </fieldset> .. <fieldset> <legend><h2>2nd Parent / Carer</h2></legend> <label for="fname2">First Name:</label> <input id="fname2" type="text" align="right" size="20" name="records[1][firstname]" value="<?= htmlentities($records[1]['firstname'])?>" /> <input id="sname2" type="text" align="right" size="30" name="records[1][surname]" value="<?= htmlentities($records[1]['surname'])?>" /> ......... </fieldset> This all works well, and then the user presses "Submit" and I do: if($_POST['update']=='Update') { $dbh->beginTransaction(); $loop = 1; foreach ($_POST['records'] as $data) { try { $query = "UPDATE carer SET firstname = ':firstname', surname = ':surname', address1 = ':address1', address2 = ':address2', town = ':town', county = ':county', postcode = ':postcode', landline = ':landline', mobile = ':mobile', email = ':email', econtact = ':econtact', ephone = ':ephone' WHERE valid_users_id = ':usrid'"; $stmt = $dbh->prepare($query); $stmt->bindParam(':firstname', $data['firstname'], PDO::PARAM_STR); $stmt->bindParam(':surname', $data['surname'], PDO::PARAM_STR); $stmt->bindParam(':address1', $data['address1'], PDO::PARAM_STR); $stmt->bindParam(':address2', $data['address2'], PDO::PARAM_STR); $stmt->bindParam(':town', $data['town'], PDO::PARAM_STR); $stmt->bindParam(':county', $data['county'], PDO::PARAM_STR); $stmt->bindParam(':postcode', $data['postcode'], PDO::PARAM_STR); $stmt->bindParam(':landline', $data['landline'], PDO::PARAM_STR); $stmt->bindParam(':mobile', $data['mobile'], PDO::PARAM_STR); $stmt->bindParam(':email', $data['email'], PDO::PARAM_STR); $stmt->bindParam(':econtact', $data['econtact'], PDO::PARAM_STR); $stmt->bindParam(':ephone', $data['ephone'], PDO::PARAM_STR); $stmt->bindParam(':usrid', $_SESSION['id'], PDO::PARAM_INT); $stmt->execute(); } catch(PDOException $ex) { ...... output any error message ........ $dbh->rollback(); } file_put_contents('PDOdata.txt', $data, FILE_APPEND); // Development testing shows correct data if ($loop == 1) { $_SESSION['firstname'] = $data['firstname']; $_SESSION['surname'] = $data['surname']; $_SESSION['address1'] = $data['address1']; $_SESSION['address2'] = $data['address2']; $_SESSION['town'] = $data['town']; $_SESSION['county'] = $data['county']; $_SESSION['postcode'] = $data['postcode']; $_SESSION['landline'] = $data['landline']; $_SESSION['mobile'] = $data['mobile']; $_SESSION['email'] = $data['email']; $_SESSION['econtact'] = $data['econtact']; $_SESSION['ephone'] = $data['ephone']; $loop++; } } $subject = 'Profile Update'; $msg = 'Thank you for updating your Profile information'; send_mail('admin@kids4us.org.uk', $_SESSION['email'], $subject, 'You\'ve received this E-Mail because your PROFILE has been changed. If you did not make this change, and believe this account modification to be fraudulant, please contact us'); Email gets sent and the SESSION data is updated $dbh->commit(); } } After this the display reverts back to the unchanged data and the database is unchanged? Any help would be appreciated.
  6. Guru, that's what I am looking for but didn't know the syntax for "records[1][firstname]". I couldn't work out how to get the array[1] with the fieldname [firstname]. Many thanks for all the help with this, now to get this done and out the door.
  7. Ch0cu3r, Obviously this is my misunderstanding of how the fetchAll works, I assumed that both records would be returned but held in an array which would be accessible in some way to assign the $myfirstname=firstname[0] & $myfirstname2=firstname[1] and this is the part I did not understand how to do. The system I am working on displays a screen with 2 Name & Address details side by side. The user must have at least 1 of these filled in and the other can be blank, but at any stage the user is allowed to amend either set of details, which is why I believed they had to be referenced with 2 sets of variables. This data is not just displayed on the screen it is amendable by the user and when the user presses the update button if any fields have been modified I have to be able to put the data which is on the screen back in to the carer file records. If i do a fetch with a while loop or a fetchAll with a foreach loop the only way I can think of making this work is for the variables ($myfisrtname, $mysurname etc) to be arrays themselves?
  8. ginerjm, yes I can agree on that, it would make good sense to use that function. Ch0cu3r, I did look at a loop and in the end may have to use that but the problem is that the 2 carer records I need are going in to 2 different sets of variables, I suppose I could look at making the variables into arrays and using the same variable name with array[0] and array[1] but it seams a very cumbersome way to deal with it. Many thanks for the help, I will go and look at this again, I may still look at trying to do a fetchAll and then assign variables from the array returned.
  9. Hi, Although I have been programming for a long time now I have been asked to help a friend out with some PHP and MySql (never touched either of them before this week) I find some tasks which should be obvious to me are a little alien. My problem is that I want to read a record from a user file using the 'id' and then using data from that record to read 1 or 2 records from a contact/address file. I do not know whether it will be 1 or 2 records as this data is entered by the user. The sample code here shows the user file "valid_user" and the contact/address file called "carer", not having used this form of syntax before I am not 100% sure that the SELECT statement is correct except that it does produce the correct data when run (well the first contact details). $myid = $_SESSION['id']; try { $query="SELECT * FROM carer INNER JOIN valid_users ON valid_users.id = carer.valid_users_id WHERE valid_users.id='$myid'"; $stmt = $dbh->prepare($query); $stmt->execute(); $count = $stmt->rowCount(); $row = $stmt->fetch(PDO::FETCH_ASSOC); $myfirstname = $row['firstname']; $mysurname = $row['surname']; $myaddress1 = $row['address1']; $myaddress2 = $row['address2']; $mytown = $row['town']; $mycounty = $row['county']; $mypostcode = $row['postcode']; $myhome = $row['landline']; $mymobile = $row['mobile']; $myemail = $row['email']; $myecontact = $row['econtact']; $myephone = $row['ephone']; This gets me details for the first contact, which is displayed quite happily but I do not understand how I go about fetching the 2nd contact record (if there is one). I understand I could use fetchAll but I cant then work out how i populate the variables for both the first contact and the second contact when the variables are not the same i.e. the firstname field in "contact" record 1 will populate $myfirstname whereas the firstname field in "contact" record 2 will populate $myfirstname2. It is done this way because I am displaying both sets of contact details on the screen. In my past experience I would just do a Read Next to get the second contact record but obviously no such syntax is available. I also use the record count later on in the program. I know this probably has an easy answer but all the documentation I have read want to talk about apples and pears and whether they are red or yellow and then shows you how to print the array without explaining how you get the separate elements of the array in to variables. Any help would be appreciated
×
×
  • 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.