Issue with adding checkbox values to mySQLdb

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
speckytwat
New php-forum User
New php-forum User
Posts: 14
Joined: Tue Sep 05, 2017 12:33 am

Wed Oct 11, 2017 12:57 pm

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).

Code: Select all

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:

Code: Select all

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

chorn
php-forum Active User
php-forum Active User
Posts: 411
Joined: Fri Apr 01, 2016 2:18 am

Thu Oct 12, 2017 3:58 am

if your database coulmn is of type INT, you can't insert multiple values. And Arrays default to "Array" when used as string. You need a One-to-many table, just search for it.

Post Reply