插入复选框数据php

时间:2015-02-25 05:15:23

标签: php jquery html mysql sql

任何人都可以帮助我如何在数据表中将数据插入fruitdid

// DATABASE:

表结果

resultsid | usernumber | fruitid

    <input type="checkbox" name="results[]" value="orange"> Orange
    <input type="checkbox" name="results[]" value="apple"> Apple
    <input type="checkbox" name="results[]" value="grapefruit"> Grapefruit
    <input type="checkbox" name="results[]" value="banana"> Banana
    <input type="checkbox" name="results[]" value="watermelon"> Watermelon <br>

<span style="color: red" id="limitmsg"></span>
<input type="submit" value="Save" name="btnsave">
</form>

2 个答案:

答案 0 :(得分:1)

  

一种方法是只遍历已提交的复选框:

   if(isset($_POST['btnsave'])) {
     //inserting the location details
     foreach($_POST["results"] as $fruit_id) {
       $query = "INSERT INTO fruits(id, fruit_id) VALUES('$id', '$fruit_id')";
       mysqli_query($conn, $query );
     }
   }

答案 1 :(得分:0)

试一试

<?php
if(isset($_GET['submit'])){
$fruit = $_GET['fruit'];
$con = ''; //mysqli database connection
$sql = $con('insert into `table` (`title`) VALUES ('{$fruit}');
if($con->query($sql)){echo 'success';} else {echo 'failed';}
}
?>


<form action="<?=$_SERVER['PHP_HOST']?>" method="get">
<input type="checkbox" name="fruit" value="orange"> Orange
<input type="submit" value="Submit" >
</form>