Jump to content

Recommended Posts

Hi, I'm trying to insert multiple records into a mySQL table for a basic intgernal messaging system. I've got it working except for some checkboxes which I use to select the users that the sender wants to send their message to. Basically each checkbox corresponds to the unique ID of the user (recipient).

if($_POST['sendmessage'])
{
        foreach ($_POST['_RecipientID'] as $recipientid)
            {
            $recipientid = $mysqli->real_escape_string($_POST['_RecipientID']);    
            $recipientemail = $mysqli->real_escape_string($_POST['_RecipientEmail']);    
            $messagetext = $mysqli->real_escape_string($_POST['_MessageText']);    
            $sender = $mysqli->real_escape_string($_POST['_Sender']);    
            $dateadded = date('Y-m-d');
        
        $addmessagetotable = $mysqli->query("INSERT INTO messages (RecipientID, MessageText, Sender, DateSent) VALUES ('$recipientid','$messagetext','$sender','$dateadded')");
            }
}

In one example, I try to add a message for recipients with IDs 1 and 6, but in the database table the Recipient ID shows as 0 (although the messages are both added into the table, just without the Recipient Ids)

My checkbox in the form is:

<input type="checkbox" name="_RecipientID[]" value=<?php echo $memberid;?> /> //Member ID is grabbed from the table of member details and is then used as Recipient ID for the message
<?php echo $firstname.' '.$surname.' Member ID '.$memberid;?> //So the sender knows who they're messaging i.e output the real name with that ID
When I echo the _RecipientID it just shows as "Array". I know it's an array but that doesn't help. Anyone know what I need to do to get the Recipient ID added into the db table successfully? Everything else is inputted without any issue.

I've also tried using var_dump(); but that doesn't print anything to the screen.

I tried doing this:

$recipients .= $recipientid.",";
- and then added $recipients into the query rather than $recipientid, but this inputs the first Recipient ID twice (in the two separate records), instead of the first Recipient ID in the first record and the second Recipient UD in trhe second record.

Your foreach loop creates a variable ($recipientid) that should contain the ID you're looking for. Try changing this

$recipientid = $mysqli->real_escape_string($_POST['_RecipientID']);
 
To this
$recipientid = $mysqli->real_escape_string($recipientid);

 

 

When I echo the _RecipientID it just shows as "Array".

 

Here's one way to show an array quickly:

echo '<pre>' . print_r($_POST['_RecipientID'], true) . '</pre>';
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.