如何使用Code Igniter编辑MySQL表行?

时间:2011-10-14 23:25:43

标签: php mysql codeigniter doctrine

所以我有一个管理员控制面板,我在其中编辑特定用户并将他们的userGroup更改为“user”或“admin”,我在他们的流旁边的每个人物频道中都有一个聊天框。(这是一个直播流媒体网站)。问题是:让我们说用户帐单在Franks聊天框(id 1)中发帖, 这保存在MySQL Table(chatUsers)中:用户名:Bill,类型:user,ChannelID:1

现在假设我指定User Bill为Admin ... Bill回到Franks频道,但没有任何管理员权限,但他确实拥有其他所有频道的管理权限。这是为什么?

因为Bill仍然存储为:“用户名:Bill,类型:用户,ChannelID:1”,在Franks频道中。

总而言之:当降级某人或将他们指定为管理员时,我想在MySQL表“chatUsers”中找到该人名的所有实例并将其删除。

(注意:当我更新一个玩家(userGroup)时,它会在表“user”中设置他们的等级。如果他们在聊天框中发帖,他们的名字和等级只会出现在“chatUsers”中。)

以下是提交以更新用户的表单:

<option value="admin" <?=($users->userGroup == 'admin') ? 'selected="selected"' : null?>>admin</option> 

它转移到form_validation:

$this->form_validation->set_rules('userGroup', 'Usergroup', 'trim|required|strip_tags|xss_clean'); 

$rows['userGroup'] = $this->input->post('userGroup');

它被转移到db_users.php:

$this->hasColumn('userGroup','enum', null, array('values' => array('user','admin'), 'default' => 'user'));

现在在表单验证中我有这段代码:

$rows['id']     = $id;
$rows['firstName']   = $this->input->post('firstName');
$rows['lastName']   = $this->input->post('lastName');
$rows['email']    = $this->input->post('email');
$rows['paypalEmail']  = $this->input->post('paypalEmail');
$rows['gender']    = $this->input->post('gender');
$rows['userGroup']   = $this->input->post('userGroup');
$rows['status']    = $this->input->post('status');
$rows['allowBroadcasting'] = $this->input->post('allowBroadcasting');

$DBUsers = new DB_Users();

if ($DBUsers->saveIt($rows)) {

if($rows['status'] == 'rejected') {

 // Get and update user channels
 $userChannels = Doctrine_Query::create()->from('DB_Channels')->where('userId = ?', $id)->fetchArray(array(), Doctrine_Core::HYDRATE_ARRAY);
 if(is_array($userChannels) AND count($userChannels) > 0) {
  foreach ($userChannels as $userChannel) {
   $channelRows['id']   = $userChannel['id'];
   $channelRows['suspended'] = 1;

   $DBChanneles = new db_Channels();
   $DBChanneles->saveIt($channelRows);
  }
 }


 // Get and update user channels
 $userVideos = Doctrine_Query::create()->from('DB_Videos')->where('userId = ?', $id)->fetchArray(array(), Doctrine_Core::HYDRATE_ARRAY);
 if(is_array($userVideos) AND count($userVideos) > 0) {
  foreach ($userVideos as $userVideo) {
   $videoRows['id']  = $userVideo['id'];
   $videoRows['status'] = 'rejected';

   $DBVideos = new db_Videos();
   $DBVideos->saveIt($videoRows);
  }
 }

} 

if ($DBUsers->saveIt($rows)) { 

我可以添加以下内容:

$chatUsers = Doctrine_Query::create()->from('DB_chatUsers')->where('userId = ?', $id)-   >fetchArray(array(), Doctrine_Core::HYDRATE_ARRAY);
 if(is_array($chatUsers) AND count($chatUsers) > 0) {
  foreach ($chatUsers as $chatUser) {
   $chatUserRows['id']   = $chatUser['id'];
   $chatUserRows['type']  = ('userGroup');

   $DBChatUsers = new db_chatUsers();
   $DBChatUsers->saveIt($chatUsersRows);
  }
 } 

?我不确定这是否正确..我是PHP的新手

1 个答案:

答案 0 :(得分:1)

这不是Code Igniter问题,你的数据库设计都错了,

您不应该在聊天表中存储除ID以外的任何用户信息,这样当您更新users表时,其他地方都会获得更新信息。