-1

I am trying to insert the selected values of checkboxes into a mysql database using php. I just can't understand why it's not working because it echoes out all the values fine, but it will only insert the first selected one into the database.

HTML:

<b>Injury type:</b>
Bruise <input type='checkbox' name='InjuryType[]' value='1'><br>
Cut <input type='checkbox' name='InjuryType[]' value='2'><br>
Graze <input type='checkbox' name='InjuryType[]' value='3'><br>
Break <input type='checkbox' name='InjuryType[]' value='4'><br>
Bump <input type='checkbox' name='InjuryType[]' value='5'><br>

PHP:

foreach($_POST['InjuryType'] as $value) {
$insert = mysql_query("INSERT INTO AccidentInjuryLink(InjuryID) VALUES ('$value')");
echo $value; 
}
7
  • print_r($_POST); is the data what you expected? Commented Feb 21, 2013 at 20:00
  • 1. Don't use the mysql driver, use PDO instead. This prevents SQL injections; I can also see that you're not escaping your posted values. 2. Maybe that column has a primary index which means all numbers must be unique, thus only inserting the first value. Commented Feb 21, 2013 at 20:01
  • Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. (Also, like silkfire said, you're vulnerable to SQL injection here, too.) Commented Feb 21, 2013 at 20:02
  • 1
    Your code is open to SQL injection attacks, here is a brief overview of what they are and how to prevent them. Commented Feb 21, 2013 at 20:02
  • are you connected to database? Commented Feb 21, 2013 at 20:02

1 Answer 1

1

Maybe that column has a primary index which means all numbers must be unique, thus only inserting the first value.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.