Jump to content

Recommended Posts

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); 
    }

Link to comment
https://forums.phpfreaks.com/topic/259713-not-inserting/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331117
Share on other sites

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);
}

Link to comment
https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331251
Share on other sites

<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[]); 
    }

 

 

Link to comment
https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331263
Share on other sites

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;
            );
        }

Link to comment
https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331269
Share on other sites

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); 
    }

Link to comment
https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331271
Share on other sites

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[]')

Link to comment
https://forums.phpfreaks.com/topic/259713-not-inserting/#findComment-1331276
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.