通过php中的复选框获取值来更新多行

时间:2011-04-17 05:50:21

标签: php

你可以帮助我吗? 我有三张桌子:有:users(userid,uername,password,groupid),group(groupid,groupname),menus(menuid,menuname),permissions(menuid,gorupid): 我想更新到表:权限 在这里我的编码:

<?php
    require_once("includes/session.php");
    $post = (!empty($_POST)) ? true : false;
    if ($post)
 {

     require_once("includes/connection.php");
     require_once("includes/functions.php");
     //GET value from page user submited
   $groupname = mysql_prep($_POST['groupname']);
   $desc = mysql_prep($_POST['desc']);
   $st_val = mysql_prep($_POST['valstore']);//store as array ex:(1,2,3,4,5,);
  $store_del_exp = explode("," ,$st_val);
  $store_del_exp_count=count($store_del_exp);
  for($j=0; $j<$store_del_exp_count;$j++)
  {
     $t_id = trim($store_del_exp[$j]);
         $groupid = mysql_prep($_POST['groupid']);
          if($t_id>0){
             $sql = "UPDATE permissions SET menus_menuid ='$t_id' WHERE groups_groupid ='".(int)$groupid."'";
            $result = dbQuery($sql);
          }
  }
       if(mysql_affected_rows){
            echo "Successfully";

        }
 }
?>

1 个答案:

答案 0 :(得分:0)

这只是一个平底船,因为您在这里使用的某些功能不是标准PHP功能:

<?php
require_once( 'includes/session.php' );
$P = $_POST;
if( count( $P ) ){
  require_once( 'includes/connection.php' );
  require_once( 'includes/functions.php' );
 // GET value from page user submitted
  $groupname = mysql_real_escape_string( $P['groupname'] );
  $groupid   = mysql_real_escape_string( $P['groupid'] );
  $desc      = mysql_real_escape_string( $P['desc'] );
  $st_val    = $P['valstore'];
 // Extract Rows to Update
  $store_del_exp = explode( ',' , $st_val );
 // Execute Updates / Insertions
  $ok    = array();
  $error = array();
  $sqlChk = 'SELECT 1 FROM `permissions` WHERE `groups_groupid`=%s';
  $sqlIns = 'INSERT INTO `permissions` ( `menus_menuid` , `groups_groupid` ) VALUES ( %s , %s )';
  $sqlUpd = 'UPDATE `permissions` SET `menus_menuid`=%s WHERE `groups_groupid`=%s';
  foreach( $store_del_exp as $menu_id ){
    $sql = sprintf( $sqlChk , $groupid );
    $res = mysql_query( $sql );
    if( $res ){
      if( mysql_num_rows( $res )>0 ){
       // Row(s) Exist, Update
        $sql = sprintf( $sqlUpd , $menu_id , $groupid );
        $res = mysql_query( $sql );
        if( $res ){
          $ok[] = 'Updated Record for $menu_id = '.$menu_id.' and $groupid = '.$groupid;
        }else{
          $error[] = 'Failed to Update Record for $menu_id = '.$menu_id.' and $groupid = '.$groupid.' MySQL Error = '.mysql_error();
        }
      }else{
       // Row(s) Do Not Exist, Insert
        $sql = sprintf( $sqlIns , $menu_id , $groupid );
        $res = mysql_query( $sql );
        if( $res ){
          $ok[] = 'Inserted Record for $menu_id = '.$menu_id.' and $groupid = '.$groupid;
        }else{
          $error[] = 'Failed to Insert Record for $menu_id = '.$menu_id.' and $groupid = '.$groupid.' MySQL Error = '.mysql_error();
        }
      }
    }else{
      $error[] = 'Failed to Check Record for $menu_id = '.$menu_id.' and $groupid = '.$groupid.' MySQL Error = '.mysql_error();
    }
  }
 // Check Results
  if( count( $error )==0 ){
    echo 'All Actions Completed Successfully';
  }else{
    echo count( $ok ).' Actions Completed OK';
    echo '<pre>'.implode( "\n" , $ok ).'</pre>';
    echo count( $error ).' Errors Encountered';
    echo '<pre>'.implode( "\n" , $error ).'</pre>';
  }
?>