dongsiju1941 2016-04-29 01:53
浏览 15

不使用php mysql插入多个数据

I have a roles called read,write,and delete operation for a list of menu.I am listing menu with read,write,delete operation with a checkbox to insert to database but when i try to insert its not inserting multiple its inserting has one row in mysql here my code

enter image description here

database enter image description here

Code Form.php

<form method="post" action="insertuser.php" class="form-horizontal">
<fieldset>
    <div class="form-group">
        <label class="col-sm-2 control-label">Username</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="username" id="username"  required>
            <span class="help-block">Please Enter Username</span>
              <span class="help-block" id="name_status"></span>

        </div>
    </div>
</fieldset>

<fieldset>
    <div class="form-group">
        <label class="col-sm-2 control-label">Password</label>
        <div class="col-sm-10">
            <input type="password" class="form-control" name="password" id="password"  required>
            <span class="help-block">Please Enter Password</span>

        </div>
    </div>
</fieldset>

<fieldset>
    <div class="form-group">
        <label class="col-sm-2 control-label">Email Address</label>
        <div class="col-sm-10">
            <input type="email" class="form-control" name="emailaddress" id="emailaddress"  required>
            <span class="help-block">Please Enter Email Address</span>
              <span class="help-block" id="name_status1"></span>

        </div>
    </div>
</fieldset>

<fieldset>
    <div class="form-group">
        <label class="col-sm-2 control-label">Role Name</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="rolename" id="rolename"  required>
            <span class="help-block">Please Enter Role Name</span>
              <span class="help-block" id="name_status2"></span>

        </div>
    </div>
</fieldset>

<table id="example" class="table table-striped table-bordered" >
<thead>
    <th data-field="id" data-sortable="true">Menu Name</th>
    <th data-field="name" data-sortable="true">Read</th>
    <th data-field="actions" data-sortable="true">Edit</th>
    <th data-field="actions" data-sortable="true">Delete</th>
</thead>
<tbody>

<?php
$i=0;

$selectquery=mysql_query("select * from menumanagement");
while($row=mysql_fetch_array($selectquery))
{

?>
<tr>
    <td><?php  echo  $row["mn_menuname"]; ?></td>    
    <td>
        <label class="checkbox checkbox-inline checked">
            <span class="icons"><span class="first-icon fa fa-square-o"></span><span class="second-icon fa fa-check-square-o"></span></span><input type="checkbox"  name="read1[]" id="read1" value="1_<?php  echo $row["mn_id"]; ?>">
        </label>
    </td>
    <td>
        <label class="checkbox checkbox-inline checked">
            <span class="icons"><span class="first-icon fa fa-square-o"></span><span class="second-icon fa fa-check-square-o"></span></span><input type="checkbox" name="edit1[]" id="edit1" value="1_<?php  echo  $i."_".$row["mn_id"]; ?>">
        </label>
    </td>
    <td>
        <label class="checkbox checkbox-inline checked">
            <span class="icons"><span class="first-icon fa fa-square-o"></span><span class="second-icon fa fa-check-square-o"></span></span><input type="checkbox"  name="delete1[]" id="delete1" value="1_<?php  echo  $i."_".$row["mn_id"]; ?>">
        </label>
    </td>

</tr>
<?php } ?>
</tbody>
</table>

<center><button type="submit" class="btn btn-fill btn-warning btn-wd" id="addmenu">Add User</button></center>

</div>
</form>

Insert.php

<?php

include_once("includes/db.php");

if(!empty($_POST["username"]))
{
    $username=$_POST["username"];
}

if(!empty($_POST["password"]))
{
    $password=$_POST["password"];
}

if(!empty($_POST["emailaddress"]))
{
    $emailaddress=$_POST["emailaddress"];
}

$insertquery=mysql_query("insert into useraccess(useraccess_username,useraccess_password,useraccess_emailaddress) values ('$username','$password','$emailaddress')");
$userid=mysql_insert_id();

if($insertquery)
{

    if(empty($_POST["read1"]))  
    {
        $readid=0;
    }

    if(empty($_POST["edit1"]))  
    {
        $editid=0;
    }

    if(empty($_POST["delete1"]))    
    {
        $deleteid=0;
    }

    if($_POST["read1"])
    {
        $readid="";
        $menuid="";
        $countread=count($_POST["read1"]);

        for($i=0;$i<$countread;$i++)
        {
            $read1=$_POST["read1"][$i];
            $readdiv =explode("_", $read1);
            $readid .=$readdiv[0];
            $menuid .=$readdiv[1];
        }
    }

    if($_POST["edit1"])
    {
        $editid="";
        $menuid="";
        $countedit=count($_POST["edit1"]);

        for($i=0;$i<$countedit;$i++)
        {
            $edit1=$_POST["edit1"][$i];

            $editdiv=explode("_", $edit1);
            $editid .=$editdiv[0];
            $menuid .=$editdiv[1];
        }

    }

    if($_POST["delete1"])
    {
        $deleteid="";
        $menuid="";
        $countdelete=count($_POST["delete1"]);
        for($i=0;$i<$countdelete;$i++)
        {
         $delete1=$_POST["delete1"][$i];

        $deletediv=explode("_", $delete1);
        $deleteid .=$deletediv[0];
         $menuid .=$deletediv[1];

        }
    }


    $insertquery1=mysql_query("insert into rolemanagement(menuid,role_read,role_write,role_delete,userid) value ('$menuid','$readid','$editid','$deleteid','$userid')");

    if($insertquery1)
    {
        header('Location: adduser.php?status="Role has been added"');
    }
}

?>

Regards

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程
    • ¥20 模型在y分布之外的数据上预测能力不好如何解决