dongshi9407 2014-11-20 06:41
浏览 56
已采纳

PHP HTML表单多维数组MySQL

I have php page with code: Everything is working fine.

<?php
session_start();
include 'db.php';//database connection

if ( isset($_POST['permission']))
{
$x=$_POST['permission'];
$a=print_r($x);//print array structure

$permit_group=array('Dean','Principal');//redefine of permission array as already    
//defined in form permission[Dean][] etc.

$permit_no=count($permit_group);//count array size of all defined areas
$b=print_r($permit_no);//its out put is 2; i am looking for: $permit_no=count($x)???

for($i=0; $i<$permit_no; $i++)//for loop for all possible permission (Dean, Principal   
etc.)
{

$jj=count($permit_group['$i']);//count size of subarea given to to individual area 
//person and output is 0; i am looking for: $jj=count($x['area'])???
$c=print_r($jj);

for($j=0; $j<jj; $j++)
{
$x1=$permit_group[$i];//i am looking for this:$x1=$x[$i]; How can i do this ?
$x2=$x[$permit_group[$i]][$j];//i am looking for this:$x2=$x[$i][$j];How can i do this?
mysqli_query($connection,"INSERT INTO upermission (area, subarea) VALUES   
('$x1','$x2')");
}
}
}
?>
<div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<ul>Dean
<li><input type="checkbox" name="permission[Dean][]" value="list"/><label>List 
Principal   
</label></li>
<li><input type="checkbox" name="permission[Dean][]" value="Edit"/><label>Edit     
Principal   
</label></li>
</ul>

<ul>Principal: 
<li><input type="checkbox" name="permission[Principal][]" value="create"/>  
<label>Create Faculty</label></li>
<li><input type="checkbox" name="permission[Principal][]" value="edit"/><label>Edit  
Faculty</label></li>
<li><input type="checkbox" name="permission[Principal][]" value="add"/>  
<label>Add Faculty</label></li>
<li><input type="checkbox" name="permission[Principal][]" value="delete"/>  
<label>Delete Faculty</label></li>
</ul>
</div>
<input type="submit" name="submit" />
</form>
</div>

My questions are:

1) I am looking to get Area (Dean,Princial etc) and Subarea (Add, Edit etc.) value from HTML form only not to create array again as i created in php part?

2) Also to calculate size of only checked Area and Subarea, because size of Area and
subarea is different, to run both for loops to insert data into database table?

Please guide me how can i do?

Thanks

  • 写回答

3条回答 默认 最新

  • dsjq6977 2014-11-20 09:45
    关注

    HI, If your problem is only with the loops, then try this.

    $post_val = $_POST['permission'];   
    foreach($post_val as $key=>$value)
    {
        $x1 = $key;     
        for($i=0;$i<count($value);$i++)
        {
            $x2 = $value[$i];           
            mysqli_query($connection,"INSERT INTO upermission (area, subarea) VALUES
           ('".$x1."','".$x2."')");         
        }
    }
    

    If you want to build all this in the HTML only, then you have to deal with either javascript or jQuery.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何用QDomDocument读取内容为空格的xml数据
  • ¥15 请阅读下面代码,帮我修改下代码
  • ¥15 关于#microsoft#的问题:电脑启动后不显示桌面图标和窗口,除任务栏外无法操作任何东西
  • ¥15 如何输入百度,显示本地下载的html文件页面,地址栏还显示百度的地址
  • ¥15 通过kinect制作换装程序但是服装不贴合(标签-ar)
  • ¥20 matlab如何绘制三维瀑布图
  • ¥15 关于用abap来解决动态规划的问题,但是要求输出索引值,这个是难点
  • ¥15 在ISIS中什么是IP从地址
  • ¥15 压测时,并发量过高时,响应时间出现尖刺
  • ¥15 关于vmprotect3.8.4虚拟文件一项