不使用php mysql

时间:2016-04-29 01:53:34

标签: php mysql

我有一个名为read,write和delete操作的角色菜单列表。我正在列出菜单,包含读取,写入,删除操作,带有一个复选框以插入数据库但是当我尝试插入其不插入多个时插入在mysql中有一行我的代码

enter image description here

数据库 enter image description here

代码 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"');
    }
}

?>

此致

0 个答案:

没有答案