Jump to content

Issue inserting checkbox values into mySQL from PHP


speckytwat

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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>';
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.