0

I am trying to insert some values into a database from a form submitted by the user.

$id = mysql_insert_id();//Gets ID of last value inserted into related table
foreach( $_POST[ 'equipment' ] as $checkBoxIndex => $checkBoxValue ) {

mysql_query("INSERT INTO recipeequipment (recipeid, equipmentid, datetimeentered) VALUES('".$id."', '".$checkBoxValue."', '".$datetime."')");

}

The only thing that needs adding to this insert statement is the quantity, which is held in a similar fashion to the checkboxes, but in textboxes. like this:

<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment[]" value="'.$rowequipment['equipmentid'].'"/>
      <input type="text" name="count[]" id="count[]" size="3" value="" />'
     .$rowequipment['description']."<br />";
      }?>

The piece i need help with is the second input column. I need to put the values that correspond with the check boxes into the insert statement, but am unsure of how to do this.

6
  • 1
    I will recommend to read about sql injections Commented Nov 25, 2011 at 14:29
  • Nice SQL injection holes. Mind if I drive this digital truck thrhough them into your site and do some donuts on your server? Commented Nov 25, 2011 at 14:30
  • Why checkboxes at all? Why can't you use text fields only? Commented Nov 25, 2011 at 14:30
  • I would suggest reading up on SQL injections and also OOP with PHP. Commented Nov 25, 2011 at 14:34
  • Yes ofcourse i will be mysql_real_escape_string() the inputs but i am just trying to find out how to get the quantity. If i use a foreach loop for the text fields as i have done above i will get every field even the empty ones. Commented Nov 25, 2011 at 14:34

1 Answer 1

1

Ignoring all the vunrabilities in your code for now, doing something like this should allow you to get a unique id and match the checkbox up to the input:

<?php while($rowequipment = mysql_fetch_assoc($sqlequipment)) {
echo '<input type="checkbox" name="equipment['.$rowequipment['equipmentid'].']" value="'.$rowequipment['equipmentid'].'"/>
      <input type="text" name="count['.$rowequipment['equipmentid'].']" id="count-'.$rowequipment['equipmentid'].'" size="3" value="" />'
     .$rowequipment['description']."<br />";
      }?>

Then once that form is submitted you would simply loop through each checkbox by doing something like:

foreach ($_POST['equipment'] as $id => $checkboxValue) {
  $textInputValue = $_POST['count'][$id];
  // do something with the above value here
}

Although, I'm not quite sure why you're using checkboxes like this - is it so you ignore updating the rows that they don't check off or something?

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

Comments

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.