动态将数据插入数据库

时间:2016-06-21 10:41:48

标签: php mysql arrays

我已经尝试了一些使用数组动态插入数据的代码但面临的问题是在一行中插入了相同的数据,即使复选框未经检查,也会插入数据值,忽略内部的检查值一个“while-loop”..我是这个阵列概念的新手,请帮帮我。

.PHP

<form id="form" name ="form" method = "POST" action="move_ppl.php" class="wizard-big" autocomplete = "off" enctype="multipart/form-data">
                                              <div class="col-md-12">

                                        <?php
                                            $con = mysqli_connect("localhost","***","***","***");
                                            $query = ("SELECT * FROM profile");
                                            $result = mysqli_query($con, $query);
                                            while ($row = $result->fetch_assoc())
                                            {


                                            echo '

                                            <tr>
                                    <td align="left">' . $row['via'] . '<input type="hidden" name="type[]"  value="' . $row['via'] . '"></td>
                                    <td align="left">   <input type="checkbox" name="type[]"  value="macro"/>   Macro </td>
                                    <td align="left">   <input type="checkbox" name="type[]"  value="micro"/>   Micro </td>
                                    <td align="left">   <input type="checkbox" name="type[]"  value="nano"/>    Nano </td>

                                </tr>';



                                            }
                                        ?>
                                <input style="width: 100%;" type="submit" name = "submit" id = "submit" value="Move"  class="btn btn-info"><br><br>
                                        </form>

db.php中

<?php
    session_start();
    define('HOST','localhost');
    define('USER','***');
    define('PASS','***');
    define('DB','***');

    $response = array();

    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

    if(isset($_POST["submit"]) && isset($_POST["type"])){

        //receiving post parameters
             $types = $_POST["type"];
             if(sizeof($types) > 0 ){
               foreach($types as $type){
        // create a new user profile
                                    $sql = "INSERT INTO ppl_tbl (vault_no, via, gname, ppl, macro, micro, nano, created_at) VALUES ('".$_SESSION['via']."', '".$_SESSION['vault_no']."', '".$_SESSION['gname']."', '".$type."','".$type."','".$type."','".$type."', NOW())";
                                    if(mysqli_query($con,$sql)){
                                        header('Location: macro_ppl.php');

                                    }else{
                                        $response["error"] = true;
                                        $response["error_msg"] = "INSERT operation failed";
                                        echo json_encode($response);
                                    }

                }
            }

    }

?>

3 个答案:

答案 0 :(得分:0)

你做错了,只需提交一个包含数据数组的表单

<强>表格

<form id="form" name ="form" method = "POST" action="someForm.php">
    <tr>
        <td align="left">   <input type="checkbox" name="type[]"  value="macro"/>   Macro </td>
        <td align="left">   <input type="checkbox" name="type[]"  value="micro"/>   Micro </td>
        <td align="left">   <input type="checkbox" name="type[]"  value="nano"/>    Nano </td>
    </tr>
</form>

<强> someForm.php

if (isset($_POST['type'])) {
    foreach ($_POST['type'] as $myType) {
        echo $myType
    }
}

您的表格

<form id="form" name ="form" method = "POST" action="move_ppl.php" class="wizard-big" autocomplete = "off" enctype="multipart/form-data">

<?php
    $con = mysqli_connect("localhost","***","***","***");
    $query = ("SELECT * FROM profile");
    $result = mysqli_query($con, $query);
    while ($row = $result->fetch_assoc())
        {
?>
        <tr>
            <td align="left"><?php echo $row['via'] ?><input type="hidden" name="type[]"  value="<?php echo $row['via'] ?>"></td>
            <td align="left">   <input type="checkbox" name="type[]"  value="macro"/> Macro </td>
            <td align="left">   <input type="checkbox" name="type[]"  value="micro"/> Micro </td>
            <td align="left">   <input type="checkbox" name="type[]"  value="nano"/> Nano </td>
        </tr>
<?php       
        }
?>
    <input style="width: 100%;" type="submit" name = "submit" id = "submit" value="Move"  class="btn btn-info"><br><br>
</form>

答案 1 :(得分:0)

如果没有设置,首先复选框值将不会出现在帖子中。

其次,添加许多结果会导致您在循环中调用insert sql。

您可以使用:

var_dump($_POST['type']);

所以你会看到结构的实际外观。

有很多方法可以让这项工作成为:

//setting the variables first
$ppl = 0;
$macro = 0;
$micro = 0;
$nano = 0;

//then run the loop to set them
foreach($types as $type){
    if(in_array($type,['ppl','macro','micro','nano'])) //just to be sure nobody pass something else so we will not override other variables
        $$type = 1;
}

//then write the query
$sql = "INSERT INTO ppl_tbl (vault_no, via, gname, ppl, macro, micro, nano, created_at) VALUES ('".$_SESSION['via']."', '".$_SESSION['vault_no']."', '".$_SESSION['gname']."', '".$ppl."','".$macro."','".$micro."','".$nano."', NOW())";

答案 2 :(得分:0)

在PHP文件中

if (isset($_POST['submit'])) {
  if(isset($_POST['type'])) {
    foreach ($_POST['type'] as $value) {
      echo $value; 
      /*add this in the query, this will return the value of checkbox which are checked*/
   }
 }

}