0

Here's my code, not sure what I'm doing wrong here... First I check for the passed variable and add the bit about how to handle the form...

// Check for a user id:
if (empty($_POST['user_id'])) {
    $errors[] = 'Oops! You forgot to enter your users.';
} else {
    $user_id = mysqli_real_escape_string($dbc, trim($_POST['user_id']));
}

if (empty($errors)) { // If everything's good.

        // Add the user to the database:
        $q = "INSERT INTO users (user_id, page_id, account_id ) VALUES ('$user_id', '$page_id', '$id' )";
        $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

        //CLOSE UP THE SUBMIT CONDITIONAL

Then I select the users and echo the form

echo '<form action="addusers.php" method="post">';
        while ($row = mysqli_fetch_array($show, MYSQLI_ASSOC)) {
            echo '<p><label for="learner">Add</label>
                <input type="checkbox" id="user_id" value="' . $row['user_id'] . '" name="user_id" /></p>';
    }

echo '<p><input type="submit" name="submit" value="Submit" /></p>
    <input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name="account_id" value="$id" />
<input type="hidden" name="course_id" value="$course_id" />
</form>';

This creates a list of my users, and allows me to select as many as I like, but when I click to submit, it only enters the last user into the database, not all of them?

// UPDATE Okay- I've edited the code so that the value of the checkbox is an array and know I need to use the foreach loop to pass the variables into the database, but could someone check this code for that insert?

    // Add the user to the database:
$q = "INSERT INTO users (user_id) VALUES ('$user_id')";
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
foreach($_POST as $key => $value){
    if ($r->execute(Array($key, $value))){
        echo "user added <br />";
    }
} // end for each loop
2
  • Check that you have more than 1 user in the DB. Also, what is in $show? Commented Jan 3, 2011 at 5:22
  • There is more than 1 user, tho only 1 at a time is being entered. The $show variable is a reference to my code to select the users for the form. This is working fine and I didn't include it to save space. Commented Jan 3, 2011 at 5:31

2 Answers 2

2

Use:

<input type="checkbox" id="user_id" value="' . $row['user_id'] . '" name="user_id[]" />

(note the [] after user_id in the "name" attribute).

That will cause $_POST['user_id'] to be an array of values, one for each checked checkbox.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this solution but it only entered a value of 0 into my database for the 'user_id' ? is there anything I need to do on that first box of code (the validation and form handling) to let it know it's an array or does the [] alone do it?
@WillHerndon:You have to loop through $_POST['user_id'] while inserting record
So how do I modify my INSERT function? Do I just add the [] to the end of the user_id variable?
0

try this replace

<input type="checkbox" id="user_id" value="' . $row['user_id'] . '" name="user_id" /></p>';

by

<input type="checkbox" id="user_id" value="' . $row['user_id'] . '" name="user_id[]" /></p>';

// try to make your user_id checkbox as array like user_id[]

6 Comments

I tried this solution but it only entered a value of 0 into my database for the 'user_id' ? is there anything I need to do on that first box of code (the validation and form handling) to let it know it's an array or does the [] alone do it?
in next page u must use loop to access these values
So how do I modify my INSERT function? Do I just add the [] to the end of the user_id variable?
use foreach loop and inside foreach loop use your insert query
Sorry - still having trouble with this, I get an error which reads "Duplicate entry '0' for key 1"... I've posted the code I used above in an edit to my question
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.