1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2008
    Rep Power

    Issue with inserting checkbox values into db

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

    		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.
    Last edited by galahad3; October 12th, 2017 at 07:48 AM.
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Hartford, WI
    Rep Power
    First off, your foreach() is defining $recipientID, so why are you doing it again right away?
    PHP Code:
    foreach ($_POST['_RecipientID' as $recipientID) {
    $recipientID = ...? 
    It does make sense to use this item since it is the checkbox, but if your intent is to just alter the value different from the way it is set, you can just do that, and not call its $_POST again.

    And just to check, when you view the HTML of your checkboxes, are the values all set correctly with the intended IDs?
    Last edited by Triple_Nothing; October 12th, 2017 at 07:26 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.

IMN logo majestic logo threadwatch logo seochat tools logo