Jump to content

Recommended Posts

please please please, somebody help. I'm new at php, and I'm going nuts trying to figure out this one task. In one form, I need to enter the results of two sets of checkboxes  into two tables. Nothing has worked, so I'm going back to basics and building from there.

 

I built this code below. It works but only puts the first row in, matching the first checkbox selected. Nothing else

 

Here's the code:


 

<?php

if (isset($_POST['submitted'])) {

$errors = array();

 

  $item_id = ($_POST['item_id']);

  $column2 = ($_POST['column2_id']);

 

 

  if (empty($errors)) {

    require ('databaseConnect.php');

 

    foreach ($_POST['column2_id'] AS $key => $column2) {

 

      $query = "INSERT INTO tablename(item_id, column2_id)

      VALUES ('$item_id', '$column2')";

 

      $result = mysql_query ($query);

 

      if ($result) {

          echo 'one plant has been added';

          exit();

 

      } else {

      echo 'system error. No plant added';

      echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>';

      exit();

      }

    mysql_close();

    }

  }

 

}

 

?>

 

<body>

<FORM action="test.php" method="post">

 

<input type="text" name="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" />

<br>

<input type="checkbox" name="column2_id[]" value="1" > one <br>

<input type="checkbox" name="column2_id[]" value="2" > two <br>

<input type="checkbox" name="column2_id[]" value="3" > three <br>

 

 

 

</fieldset><br><br>

<input type="hidden" name="submitted" value="TRUE">

<input type="submit" />

 

</form>

 

 


Link to comment
https://forums.phpfreaks.com/topic/208642-foreach-only-puts-first-check-box-in-db/
Share on other sites

Ok, I tried that. Unfortunately, it's doing the same thing. Only the first row goes into the database. Here's what I wrote:

 

 

for ($i=0;$i<count($column2);$i++)

{

 

      $query = "INSERT INTO tablename(item_id, column2_id)

      VALUES ('$item_id', '$column2[$i]')";

 

      $result = mysql_query ($query);

 

      if ($result) {

 

          echo 'your items have been added';

 

Alright then, to start debugging, see what the $_POST array actually contains. put echo '<pre>'; print_r($_POST); echo '</pre>'; at the head of the script and see if it contains the values you'd expect it to.

ok, I've done that.  It's definitely knows what I've clicked. Still only the first entry goes into the database. Here's what it created.

 

Array

(

    [item_id] => 20

    [column2_id] => Array

        (

            [0] => 1

            [1] => 5

            [2] => 9

            [3] => 10

        )

    [submitted] => TRUE

)

one item has been added

Found it (I think). You have a curly bracket out of place. add one after $result = mysql_query ($query);, and remove one just prior to the closing ?> tag. Try that, and see if it needs any further tweaking.

uh-oh! Doing that, generated this error on reloading of the page:

 

 

Warning: Invalid argument supplied for foreach() in /filepath/htdocs/filename.php on line 15

 

Fatal error: Call to undefined function mysql_real_query() in /filepath/htdocs/filename.php on line 18

Sure... here it is below: (by the way, how do you guys post this code in those windows, in the forum? are you just putting it in an html form text box?)


 

<html>

<title>title</title>

<?php

 

echo '<pre>';

print_r($_POST);

echo '</pre>';

 

if (isset($_POST['submitted']))

{

$errors = array();

 

  $item_id = ($_POST['plant_id']);

  $column2 = ($_POST['column2_id']);

 

 

  if (empty($errors))

  {

 

    require ('databaseconnect.php');

 

 

    for ($i=0;$i<count($medicine);$i++)

      {

 

      $query = "INSERT INTO tablename(item_id, column2_id)

      VALUES ('$item_id', '$column2[$i]')";

 

      $result = mysql_query ($query);

      }

 

  if ($result)

      {

 

      echo 'items have been added';

 

      exit();

 

      } else {

 

      echo 'system error. Not added';

 

      echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>';

 

      exit();

 

      }

    mysql_close();

 

  }

 

 

}

 

 

?>

 

 

 

<body>

 

 

<FORM style="border: 1px dotted red; padding: 2px" action="self_formname.php" method="post">

 

item id field:<br>

only enter numbers here<br>

//this will be a hidden passed variable in finished form

 

<input type="text" name="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" />

<br><br>

 

 

characteristics:<br>

<input type="checkbox" name="column2_id[]" value="1" > one <br>

<input type="checkbox" name="column2_id[]" value="2" > two <br>

<input type="checkbox" name="column2_id[]" value="3" > three <br>

<input type="checkbox" name="column2_id[]" value="4" > four <br>

<input type="checkbox" name="column2_id[]" value="5" > five <br>

<input type="checkbox" name="column2_id[]" value="6" > six <br> 

<input type="checkbox" name="column2_id[]" value="7" > Seven <br>

 

</fieldset><br><br>

<input type="hidden" name="submitted" value="TRUE">

<input type="submit" />

 

</form>

 

</body>

 

</html>

You're original code you used is correct

<?php
if (isset($_POST['submitted'])) {
$errors = array();

  $item_id = ($_POST['item_id']);
  $column2 = ($_POST['column2_id']);


  if (empty($errors)) {
    require ('databaseConnect.php');

    foreach ($_POST['column2_id'] AS $key => $column2) {

      $query = "INSERT INTO tablename(item_id, column2_id)
      VALUES ('$item_id', '$column2')";

      $result = mysql_query ($query);

      if ($result) {
           echo 'one plant has been added';
           exit();

      } else {
      echo 'system error. No plant added';
      echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>';
      exit();
      }
    mysql_close();
    } 
  }

} 

?>

<body>
<FORM action="test.php" method="post">

<input type="text" name="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" />
<br>
<input type="checkbox" name="column2_id[]" value="1" > one <br> 
<input type="checkbox" name="column2_id[]" value="2" > two <br> 
<input type="checkbox" name="column2_id[]" value="3" > three <br> 



</fieldset><br><br>
<input type="hidden" name="submitted" value="TRUE">
<input type="submit" />

</form>

 

The reason why your script is only adding one value into your database is because you're callingexit (on line 21) when your query successfully inserts a new row.

19.      if ($result) {
20.           echo 'one plant has been added';
21.           exit();
22.
23.      } else {

Delete exit(); and your script will execute as you intended.

 

(by the way, how do you guys post this code in those windows, in the forum? are you just putting it in an html form text box?)

Sure, wrap your code within code tags (


or


)

To have the code in the code or php boxes, just post it within

 . . . 

or

 . . . 

bbcode tags.

 

I've just tested this, and it should work fine. There is no plant_id field in the form, there is item_id. See my comment in the code regarding that, also, to submit a form to itself, you can simply leave it as <form action="". . . There's no need to specify the script name.

 

<html>
<title>title</title>
<?php
if (isset($_POST['submitted'])) {
   $errors = array();
   $item_id = ($_POST['item_id']); // This was $_POST['plant_id'], which didn't exist in the form.
   $column2 = ($_POST['column2_id']);
   if (empty($errors)) {
      require ('databaseconnect.php');
      foreach ($column2 as $val ) {
         $query = "INSERT INTO tablename(item_id, column2_id) VALUES ('$item_id', '$val')";
         mysql_query($query) or die( mysql_error() );
         // echo $query . '<br />';
      }
      if ($result) {
         echo 'items have been added';
      }
   } else {
      echo 'system error. Not added';
      echo '<p>' . mysql_error() . '<br><br>query:' . $query . '</p>';
   }
   mysql_close();
}
?>

<body>


<FORM style="border: 1px dotted red; padding: 2px" action="" method="post">

item id field:<br>
only enter numbers here<br>
//this will be a hidden passed variable in finished form

<input type="text" name="item_id" value="<?php if(isset($_POST['item_id'])) echo $_POST['item_id']; ?>" />
<br><br>


characteristics:<br>
<input type="checkbox" name="column2_id[]" value="1" > one <br>
<input type="checkbox" name="column2_id[]" value="2" > two <br>
<input type="checkbox" name="column2_id[]" value="3" > three <br>
<input type="checkbox" name="column2_id[]" value="4" > four <br>
<input type="checkbox" name="column2_id[]" value="5" > five <br>
<input type="checkbox" name="column2_id[]" value="6" > six <br>
<input type="checkbox" name="column2_id[]" value="7" > Seven <br>

</fieldset><br><br>
<input type="hidden" name="submitted" value="TRUE">
<input type="submit" />

</form>

</body>

</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.