Jump to content

How to save multiple checkbox value using only 1 save button


samuel_lopez

Recommended Posts

I have this code that came from table thing

<?php 
include 'config.php';

$animal_id = 1; //came from animal table(ex. dog)

 $sql= "Select * from thing order by description desc";
 $mysqls = $mysqli->query($sql);
    while ($row = mysqli_fetch_assoc($mysqls)) {

        $desc = $row['description']; //ex . pitbull,persian cat,chiwawa
        $id = $row['id'];
        echo "<input type='checkbox' name='things[]' value='".$id."'/>$desc<br />";
        

    }

 ?>
  

How can I save checked checkbox value  in another table called Animal_Classification using only 1 button
Animal_Classification table should look like this when saved

id      animal_id        thing_id

1           1(ex.dog)     1(ex. pitbull)  

2           1(ex.dog)     2(ex. chiwawa)            

 

 

Please help. Thanks     
 

Link to comment
Share on other sites

This should help give you an idea.

<?php
if ($_POST)
    {
    $db = new PDO("mysql:host=localhost;dbname=phphelp_form_array", "user", "pass");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $insertStmt = $db->prepare("INSERT INTO datatable (field1, field2) VALUES (?,?)");
    
    for ($i = 0; $i < count($_POST['field1']); $i++)
        {
        $insertStmt->execute(array(
            $_POST['field1'][$i],
            $_POST['field2'][$i]
        ));
        }
    }
?>
<form action="<?php echo $_SERVER['script_name']; ?>" method="post">
   <b>field1 1</b><br>
   <label>field1 <input type="text" name="field1[]"></label>
   <br>
   <label>field2 <input type="text" name="field2[]"></label>
   <br>
   <b>field1 2</b><br>
   <label>field1 <input type="text" name="field1[]"></label>
   <br>
   <label>field2 <input type="text" name="field2[]"></label>
   <input name="" type="submit" value="Submit">
</form>
Edited by benanamen
Link to comment
Share on other sites

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.