samuel_lopez Posted October 22, 2015 Share Posted October 22, 2015 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 buttonAnimal_Classification table should look like this when savedid animal_id thing_id 1 1(ex.dog) 1(ex. pitbull) 2 1(ex.dog) 2(ex. chiwawa) Please help. Thanks Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 22, 2015 Share Posted October 22, 2015 Is this homework? Quote Link to comment Share on other sites More sharing options...
samuel_lopez Posted October 22, 2015 Author Share Posted October 22, 2015 No, I'm just practicing a multiple saving in php . Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 22, 2015 Share Posted October 22, 2015 (edited) 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 October 22, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.