MJ93
MJ93

Reputation: 5286

How can I edit a MySQL table row with Code Igniter?

so I have an admin control panel where I edit a specific user and change their userGroup to either “user” or “admin”, I also have a chat box inside each persons channel next to their stream.(this is a live streaming site). The problem is: lets say User Bill posts in Franks chat box (id 1), This is saved in MySQL Table (chatUsers) as: Username: Bill, Type: user, ChannelID: 1

Now let’s say I appoint User Bill as Admin…Bill goes back to Franks channel but does not have any admin powers, he does however have admin powers for every other channel. Why is this?

Because Bill is still stored as: “Username: Bill, Type: user, ChannelID: 1” in Franks channel.

So to sum it all up: When demoting someone or appointing them as admin, I would like to find all instances of that persons name in MySQL table “chatUsers” and erase it.

(Note: When I update a players (userGroup) it sets their rank in Table “user”. Their name and rank will only show up in “chatUsers” if they post in a chat box.)

Here is the form that gets submitted to update the user:

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

It gets transferred here to form_validation:

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

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

It gets transferred to db_users.php:

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

Now inside form validation I have this block of code:

$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);
  }
 }

} 

Under

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

Could I add something like:

$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);
  }
 } 

? I'm not sure if that's even right.. I'm kind of new to PHP

Upvotes: 0

Views: 494

Answers (1)

Hailwood
Hailwood

Reputation: 92581

This is not a Code Igniter Issue, your database design is all wrong,

You should not be storing any user information in the chat table other than the ID, that way when you update the users table everywhere else gets the update information.

Upvotes: 1

Related Questions