PHP - 当我在下拉列表中选择所有值时如何插入所有行

时间:2017-08-24 06:43:01

标签: php mysqli

 <select class="form-control" name="brid">
                      <option value="" disabled selected="selected">Choose Branch</option>
                      <option value="All">All</option>

<?php 
    require_once '../db/db.php';

    $select = $conn->query("SELECT * FROM branches where access = 'User' or access = 'USER' ");

    while($row = mysqli_fetch_assoc($select)) {

            echo "<option value='".$row['brid']."'>".$row['brid']."</option>";
    }
?>
     </select>

这是我选择分支的代码,我想要的就是当我选择所有值时。 branches表中的所有行都具有相同的任务。

<?php          
require '../db/db.php';

    $tkid = $_POST['tkid'];
    $brid = $_POST['brid'];
    $sdate = $_POST['sdate'];
    $edate = $_POST['ddate'];
    $tkname = $_POST['tkname'];
    $tkdesc = $_POST['tkdesc'];
    $noteby = $_POST['noteby'];

        $select = $conn->query("SELECT * FROM TASK WHERE tkid = '$tkid'");
        $count = mysqli_num_rows($select);

        if ($count == 0) {

        $query = $conn->query("INSERT INTO TASK (brid,tkid,tkname,tkdesc,sdate,edate,ntby) 
            VALUES ('$brid','$tkid','$tkname','$tkdesc','$sdate','$edate','$noteby')");

            echo "<script>alert('The task is successfully created');</script>";
            echo "<script>window.location.assign('../load/atask.php')</script>";
        }

        else{

            echo "<script>alert('The task is already created');</script>";
            echo "<script>window.location.assign('../load/atask.php')</script>";

        }


?>

这是我的insertfile,我想要的是将数据插入到分支表中的所有用户

2 个答案:

答案 0 :(得分:0)

通过foreach循环运行请求,在循环中根据请求中的行名称进行插入。

答案 1 :(得分:0)

我已经找到了答案感谢你们的建议

        $arr_br = array();

        $selectagain = "SELECT * FROM branches where access = 'User' ";

        if($countrows = mysqli_query($conn, $selectagain)){

            while($row = mysqli_fetch_assoc($countrows)){
                $arr_br[count($arr_br)] = $row["brid"];
            }
        }

        for($x = 0; $x < count($arr_br); $x++) {
            $insert = $conn->query("INSERT INTO TASK(brid,tkid,tkname,tkdesc,sdate,edate,ntby) VALUES ('".$arr_br[$x]."',(SELECT CONCAT('Pandayan-Task #',(count(id) + 1 )) from TASK as tkid), '$tkname', '$tkdesc', '$sdate', '$edate', '$noteby') ");

        echo "<script>alert('The task is successfully created');</script>";
        echo "<script>window.location.assign('../load/atask.php')</script>";

        }

    }

    else {

    $select = $conn->query("SELECT * FROM TASK WHERE tkid = '$tkid'");
    $count = mysqli_num_rows($select);

    if ($count == 0) {

    $query = $conn->query("INSERT INTO TASK (brid,tkid,tkname,tkdesc,sdate,edate,ntby) 
        VALUES ('$brid','$tkid','$tkname','$tkdesc','$sdate','$edate','$noteby')");

        echo "<script>alert('The task is successfully created');</script>";
        echo "<script>window.location.assign('../load/atask.php')</script>";
    }

    else{

        echo "<script>alert('The task is already created');</script>";
        echo "<script>window.location.assign('../load/atask.php')</script>";

    }


    }