0

I'm using MYSQL and php to insert data into my sql.

I have two tables users and userrights.

I'm using checkbox to display rights on user edit page. when rights are de-selected or select i wont to update my userrights table.

CREATE TABLE userrights (
  userid int(11) NOT NULL default '0',
  right_name varchar(50) collate latin1_general_ci NOT NULL default '',
  PRIMARY KEY  (userid,right_name)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;


INSERT INTO userrights (userid, right_name) VALUES
(1, 'add'),
(1, 'delete'),
(1, 'edit'),
(1, 'view'),
(2, 'add'),
(2, 'delete'),
(2, 'edit'),
(2, 'view');

CREATE TABLE users (
  id int(11) NOT NULL,
  `name` varchar(100) collate latin1_general_ci default NULL,
  email varchar(100) collate latin1_general_ci default NULL,
  pass varchar(42) collate latin1_general_ci default NULL,
  PRIMARY KEY  (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;

INSERT INTO users (id, name, email, pass) VALUES
(1, 'name', '[email protected]', '5f4dcc3b5aa765d61d8327deb882cf99'),
(2, 'name name', '[email protected]', '1a1dc91c907325c69271ddf0c944bc72');

how do I do this?

2
  • Please clarify... are you asking how to write your queries to update permissions?... Or how to tie that functionality to a checkbox on your site (either using form submission, or Ajax)? Commented Jul 29, 2012 at 16:29
  • how add this functionality to site just by submitting the form Commented Jul 29, 2012 at 16:32

2 Answers 2

1

I've had a similar problem before.

The solution I came up with is truncate the table and reinsert all of the selected entries.

Sign up to request clarification or add additional context in comments.

4 Comments

I did think of deleting all records and then re-enter records again $sql = “INSERT INTO userrights ( 1, ‘add’ );”; $sql .= “INSERT INTO userrights ( 1, ‘edit’ );”; $sql .= “INSERT INTO userrights ( 1, ‘delete’ );”; $result2 = mysql_query($sql);
Please don't use mysql_* functions in new code. They were removed from PHP 7.0.0 in 2015. Instead, use prepared statements via PDO or MySQLi. See Why shouldn't I use mysql_* functions in PHP? for more information.
@user1561124: Also, the use of PDO transaction in conjunction with prepared statements can give you a huge efficiency boost.
thank you for this information I have chnage my database to mysqli
0

You get the data of the checkbox(true or false)

$rows = mysql_num_rows(mysql_query("SELECT * FROM userrights WHERE id = 'x' and right_name = 'y'"); // Please use insted of mysql_* PDO or MySQLi 
if ($rows > 0)
{
        if ($checkbox == false)
        {
        mysql_query("DELETE FROM userrights WHERE id = 'x' AND right_name = 'y'");
        }
}
else
{
// Recht nicht vorhanden --> if checkbox == true
      if ($checkbox == true)
      {
      mysql_query("INSERT INTO... VALUES ($id, $right_name)
      }
}

First you check if the user had the right. If yes you need to check if the right (data from checkbox) is set to false --> delete that row from the table. If the user hadn't that right and the checkbox is true, you must insert that in your mysql_table. The comments must be changed to and if-clause in your code.

2 Comments

if permission have already have been added and if i try select again they will be deleted and how do I add the permission and update user same time
the code above wasn't complete. oh fuuuuuuu! I wrote something in German.... SOrry my fault. I changed the code abvoe!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.