Xtremer360 Posted March 26, 2012 Share Posted March 26, 2012 I am still having issues with getting the second part of this function to work right. It inserts fine into the first table but after that it's not performing the second insert. function sendMessage($recipients, $bcc, $subject, $message, $sender) { $data = array( 'subject' => $subject, 'senderID' => $sender, 'message' => $message ); $this->db->insert('usersPersonalMessages', $data); $insertID = $this->db->insert_id(); foreach ($recipients AS $recipient) { $recipientData = array( 'usersPersonalMessagesID' => $insertID, 'userID' => $recipient ); } if ($bcc) { foreach ($bcc AS $userID) { $recipientData['userID'] = $userID; $recipientData['bcc'] = 1; } } $this->db->insert('usersPersonalMessagesRecipients', $recipientData); } Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/ Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 What errors do you receive? Have you debugged this at all? Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331096 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 Is my logic even right you think? Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331099 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 I am getting a invalid argument for this line foreach ($recipients AS $recipient) AND me having to use the set method but I don't think I'd have to for this line $this->db->insert('usersPersonalMessagesRecipients', $recipientData); Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331100 Share on other sites More sharing options...
mr.noname Posted March 26, 2012 Share Posted March 26, 2012 in the code example the $recipients and $bcc argument MUST BE an array so if the $recipients and $bcc is not an array you will get some warning message well, if this is the codeigniter ( i think it is codeigniter ) the insert line is corrected *** make sure *** in your code example the recipient will always replace by bcc id before save if you pass $bcc into a function Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331117 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 Say what? Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331249 Share on other sites More sharing options...
cpd Posted March 26, 2012 Share Posted March 26, 2012 Assuming you want to add multiple bits of data and your insert() function can handle that you need to add square brackets to the end of the two array variables as follows: foreach($bcc AS $userID){ $recipientData['userID'][] = $userID; $recipientData['bcc'][] = 1; } Else you may as well just assign them values instead of cycling through. Even then the array structure is slightly dodgy; consider changing it to the following: foreach($bcc AS $userID){ $recipientData[] = array('userID' => $userID, 'bcc' => 1); } Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331251 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 Awesome but what do you mean by that last part. Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331253 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 However I did want to say that I'm getting the same to issues still. Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331254 Share on other sites More sharing options...
cpd Posted March 26, 2012 Share Posted March 26, 2012 What's your error because you haven't actually told us yet. Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331261 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 <p>Severity: Warning</p> <p>Message: Invalid argument supplied for foreach()</p> <p>Filename: kow_auth/pmmodel.php</p> <p>Line Number: 120</p> </div><br /> <b>Fatal error</b>: Cannot use [] for reading in <b>/home/xtremer/public_html/kowmanager/application/models/kow_auth/pmmodel.php</b> on line <b>137</b><br /> function sendMessage($recipients, $bcc, $subject, $message, $sender) { $data = array( 'subject' => $subject, 'senderID' => $sender, 'message' => $message ); $this->db->insert('usersPersonalMessages', $data); $insertID = $this->db->insert_id(); foreach ($recipients AS $recipient) { $recipientData = array( 'usersPersonalMessagesID' => $insertID, 'userID' => $recipient ); } if ($bcc) { foreach ($bcc AS $userID) { $recipientData['userID'][] = $userID; $recipientData['type'][] = 2; } } $this->db->insert('usersPersonalMessagesRecipients', $recipientData[]); } Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331263 Share on other sites More sharing options...
cpd Posted March 26, 2012 Share Posted March 26, 2012 What's line 120 and line 137? Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331266 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 120: foreach ($recipients AS $recipient) 137: $this->db->insert('usersPersonalMessagesRecipients', $recipientData[]); Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331268 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 basically these lines are screwing everything: foreach ($recipients as $recipient) { $recipientData = array( 'usersPersonalMessagesID' => $insertID, 'userID' => $recipient ); } 1. $recipients needs to be an array, it's not. Do something like: if(!is_array($recipients)) { echo "not an array"; exit; } 2. You will just be redefining $recipientData every loop, overwriting the previous values. Do something like: $recipientData = array(); foreach ($recipients as $recipient) { $recipientData[$insertID][] = $recipient; ); } Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331269 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 Yeah for some reason its not an array but I don't know why its not. Here's my posting parameters from the form: bcc[] 10000 bcc[] 10001 message dafdafdasf recipient[] 10000 recipient[] 10001 sender 10000 subject Testiung submit Submit Message And here's what's inside my controller: // Function used for login form validation function pmsubmit() { $outputArray = array('error' => 'yes', 'message' => 'unproccessed'); // Sets validation rules for the login form $this->form_validation->set_rules('recipient[]', 'Recipient', 'required|is_natural_no_zero'); $this->form_validation->set_rules('bcc[]', 'Bcc', 'is_natural_no_zero'); $this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean'); $this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean'); $this->form_validation->set_rules('sender', 'Sender', 'required|integer'); // Checks to see if login form was submitted properly if (!$this->form_validation->run()) { $outputArray['message'] = 'There was a problem submitting the form! Please refresh the window and try again!'; } else { if ($this->users->usersExists($this->input->post('recipient[]'))) { $outputArray['message'] = 'One or more of the recipients could not be found in the database!'; } if ($this->input->post('bcc[]')) { if ($this->users->usersExists($this->input->post('bcc[]'))) { $outputArray['message'] = 'One or more of the bcc users could not be found in the database!'; } } $sender = $this->users->getUserByUserID($this->input->post('sender')); if ($sender == NULL) { $outputArray['message'] = 'You were for some reason not found in the database!'; } if ($this->pmmodel->sendMessage($this->input->post('recipient[]'), $this->input->post('bcc[]'), $this->input->post('subject'), $this->input->post('message'), $this->input->post('sender'))) { $outputArray = array('success' => 'Yes', 'message' => 'Message was sent successfully!'); } else { $outputArray['message'] = 'Message could not get sent!'; } } echo json_encode($outputArray); } Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331271 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 this is the line that we care about: $this->pmmodel->sendMessage($this->input->post('recipient[]'), $this->input->post('bcc[]'), $this->input->post('subject'), $this->input->post('message'), $this->input->post('sender'))) you need to check the output of $this->input->post('recipient[]') Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331276 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 So I need to do print_r($this->input->post[recipient[]) Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331277 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 I tried that but for some reason nothing printed Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331283 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 do var_dump($this->input->post('recipient[]'); Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331284 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 I'm assuming on the controller? Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331286 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 I did right above the sendMessage function in the controller it says bool(false) Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331289 Share on other sites More sharing options...
AyKay47 Posted March 26, 2012 Share Posted March 26, 2012 well, keep back-tracing like this until you find the root of the issue. Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331293 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 okay well recipient[] is not an array but recipient is Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331294 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 The controller is apparently fixed however now the model is still needing fixed. Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331297 Share on other sites More sharing options...
Xtremer360 Posted March 26, 2012 Author Share Posted March 26, 2012 It says <b>Fatal error</b>: Cannot use [] for reading in <b>/home/xtremer/public_html/kowmanager/application/models/kow_auth/pmmodel.php</b> on line <b>141</b><br /> but there's not even 141 lines in the file. Quote Link to comment https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331299 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.