![]() |
Need Help With Form Submission - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Need Help With Form Submission (/showthread.php?tid=50229) Pages:
1
2
|
Need Help With Form Submission - El Forum - 03-19-2012 [eluser]vincej[/eluser] Hi - Can someone PLEASE help me figure out how to get my data into my Database from my form ?? I have a CI Form which allows the user to amend and then submit the dates and locations. It looks like this: Code: Pick Up ID Location Date 1 Date 2 Date 3 Date 4 Remove? In order to facilitate easy searches and DB management the Dates Table looks like this: Code: puid locationid dates Every date falls within the same DB column. Using Group_CONCAT in the query and EXPLODE in the view , It all works very well for pivoting the dates into a horizontal display. The resulting array which feeds the form looks like this: Code: Array View Code: foreach ($pickupdates as $item) { At this point things stop working properly. I know that my model does not work and is flawed. I get an error saying invalid argument of the 'Foreach' in the model code below. I am not sure how to properly construct the $Data array in the CI form so that my update controller and update model amend the fields correctly bearing in mind the nature of the data structures and the fact that it is being presented in a converted Human readable date yet stored as date stamps. Controller: Code: function Updatelocation(){ Model Code: function Updatelocation(){ If ANY ONE can provide me a steer in the right direction I will be forever grateful !! Many Thanks !! Need Help With Form Submission - El Forum - 03-19-2012 [eluser]CroNiX[/eluser] Where are you setting the form element with the name of 'locationid'? It looks like: Code: $data = array('name'=>$item['location'], 'size'=>20,'value' => $item['location']); echo form_input($data); Code: $data = array('name'=>'locationid', 'size'=>20,'value' => $item['location']); echo form_input($data); Are you sure $_POST['locationid'] is an array? It seems it would be a single integer, since it's an id to a particular location. In your Updatelocation() method, you are overriding the 'dates' key, so the final array will only contain the last date. You also don't define $locationid, but use it in your where(). You also enclose '$theDates[0]' in single quotes, which will be taken literally and not a variable. Need Help With Form Submission - El Forum - 03-19-2012 [eluser]vincej[/eluser] Hi CroxNix - Thanks so much for riding over the hill and coming to the rescue ! apologies for being such a Dufus . following your recommendations I have made some alterations to the code - I hope correctly and in the right places. I seem to be impacting the DB now as all the existing test dates have been set to 0 and the locationid is now set to a single location. However, on the not so good front, I'm still deep in the weeds with several errors. The DB is being updated with a single location, and the dates themselves are not getting through. My CI form is also now giving me lots of 'Undefined Offset'. I'm also not getting hardly anything off the Post array: Code: Array ( [location] => Varsity [0] => Wed 31 Dec 1969 [locationid] => 2 [submit] => Save Changes ) Here is the current: View: Code: <?php Model Code: function Updatelocation(){ Need Help With Form Submission - El Forum - 03-19-2012 [eluser]vincej[/eluser] Update: I have tried manually entering dates into the DB. 2 things happen. Firstly the 'Offsets' disappear - Good. However, when I do a 'Submit' 2 things can be observed: 1 - All the entered dates are attributed to a single location '2' , 2 - All the manually entered dates are set to Zero. Need Help With Form Submission - El Forum - 03-19-2012 [eluser]CroNiX[/eluser] This is part of your problem: Code: $data = array( if you check $data, it will probably only contain: Code: $data = array( You also have no where() statement in your update(), so it will update all. You will need a unique identifier for your where, like puid instead of just locationid (since there are many ids with the same locationid). Need Help With Form Submission - El Forum - 03-19-2012 [eluser]vincej[/eluser] Quote:if you check $data, it will probably only contain Ok - I get that, but how do I overcome that .. I mean, all my dates go into the very same column. How do I refer to the same column without suing the same key ? Need Help With Form Submission - El Forum - 03-19-2012 [eluser]CroNiX[/eluser] They go into the same column, but in different rows. The row that matches the puid that you need in your where(). First, you need to get the puid in your form. If you try to update by locationid, you will have problems. Like for locationid = 1, there are 4 of them. For each date you need to know the locationid AND the puid so you can: Code: $data = array('dates' => the_date); Need Help With Form Submission - El Forum - 03-19-2012 [eluser]vincej[/eluser] Ok - CroxNix - if I could buy you a beer I'd buy you 10! Changed my query to include, puid. so it now read: Code: function Updatelocation(){ and I still get "undefined Index TheDates[0]" through to [3] I'm also getting: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\CountryLane\system\core\Exceptions.php:185) Need Help With Form Submission - El Forum - 03-20-2012 [eluser]vincej[/eluser] HI CroxNix - Ok, I have been working on my model and have made a few changes. Firstly for the benefit of clarity I removed all the locations barr 1. Secondly I put specific 'names' into the CI form within the view. Thirdly I changed my model to reflect the changes in the form and I added a strtotime() so that the DB get UNIX date stamps. All the undeclared indexes are gone. also the POST array is now giving me all the dates from the from - all good. BUT, my update query is still not updating the DB. I think this is because with only a single pickup id ( puid ) it is trying to push 4 dates into a single row .. that isn't going to work, unless I perhaps missed something big. So ... maybe I should do an IMPLODE() on the POST array so can get them all on 1 row with 1 puid ? If I was successfull doing that then, I would not need GROUP_CONCAT() at the initial SELECT time. I would rather not do it as it could add more complexity ... but if needs must .. I am really sorry to be a burden, but this has really befuddled me. anyway - here is my data" POST: Code: Array ( [location] => Collingwood [Dates1] => Wed 31 Dec 1970 [Dates2] => Wed 31 Dec 1969 [Dates3] => Wed 31 Dec 1969 [Dates4] => Wed 31 Dec 1969 [locationid] => 4 [puid] => 4 [submit] => Save Changes ) View: Code: <?php Model: Code: function Updatelocation(){ Need Help With Form Submission - El Forum - 03-20-2012 [eluser]CroNiX[/eluser] Each date should have its own puid, but the same locationid. So in your post data, there should be 4 puids, 4 dates and 1 locationid (since they all share the same location). You will need to keep track of what puid belongs to each date, maybe each (puid1, puid2, etc) in a hidden form field. Then your updates would look something like: Code: $locationid = $this->input->post('locationid'); Note, this isn't how I would actually do it. 4 queries is inefficient but I wanted to show you where you are going wrong. I'd look into update_batch() to do this in a single query instead of 4. |