SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help for new prgrammer

    There is probably a very simple solution to this problem but I can't see it. I have taken some of the code that Kevin used in his PHP series and I have tried to add to it.

    I took the code for Users and Roles and copied Roles again and renamed it User_Type so I would have 2 checkbox areas to assign value to a user, but I keep getting error when trying to add new users. The edit and delete functions work fine and update all tables correctly. I even renamed the role id field to rolid so there wouldn't be any conflicts with roleid in the userrole table.

    The code is as follows:

    // Build the list of User Types
    $sql = "SELECT id, name FROM user_type";
    $result = mysqli_query($link, $sql);
    if (!$result)
    {
    $error = 'Error fetching list of User Types.';
    include 'error.html.php';
    exit();
    }

    while ($row = mysqli_fetch_array($result))
    {
    $user_types[] = array(
    'id' => $row['id'],
    'name' => $row['name'],
    'selected' => FALSE);
    }

    // Build the list of User Roles
    $sql = "SELECT rolid, name FROM role";
    $result = mysqli_query($link, $sql);
    if (!$result)
    {
    $error = 'Error fetching list of Roles.';
    include 'error.html.php';
    exit();
    }

    while ($row = mysqli_fetch_array($result))
    {
    $roles[] = array(
    'rolid' => $row['rolid'],
    'name' => $row['name'],
    'selected' => FALSE);
    }
    include 'form.html.php';
    exit();

    Addform fields are here--------------------

    if (isset($_POST['user_types']))
    {
    foreach ($_POST['user_types'] as $user_type)
    {
    $user_typeid = mysqli_real_escape_string($link, $user_type);
    $sql = "INSERT INTO userstype SET
    userid='$userid',
    user_typeid='$user_typeid'";
    if (!mysqli_query($link, $sql))
    {
    $error = 'Error assigning selected User Type to User.';
    include 'error.html.php';
    exit();
    }
    }
    }

    if (isset($_POST['roles']))
    {
    foreach ($_POST['roles'] as $role)
    {
    $roleid = mysqli_real_escape_string($link, $role);
    $sql = "INSERT INTO userrole SET
    userid='$userid',
    roleid='$roleid'";
    if (!mysqli_query($link, $sql))
    {
    $error = 'Error assigning selected Role to User.';
    include 'error.html.php';
    exit();
    }
    }
    }

    header('Location: .');
    exit();

    Any thoughts would be appreciated.

  2. #2
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Without looking much at your PHP code I can tell your SQL INSERT statement is incorrect.

    For specific columns you can use:

    INSERT INTO table_name (columns) VALUES (value1, value2, value3)

    Or you can use the following, except you are not specifying the column names.

    INSERT INTO table_name VALUES (value1, value2, value3)

    Also, please be more specific as to what "error" you get.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,094
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Cerium View Post
    Without looking much at your PHP code I can tell your SQL INSERT statement is incorrect.

    For specific columns you can use:

    INSERT INTO table_name (columns) VALUES (value1, value2, value3)

    Or you can use the following, except you are not specifying the column names.

    INSERT INTO table_name VALUES (value1, value2, value3)

    Also, please be more specific as to what "error" you get.
    Ehm, the syntax he uses, "INSERT INTO userrole SET userid='$userid', roleid='$roleid'"; is perfectly fine

    You can either do
    INSERT INTO datable (col1,col2,col3,...,colN) VALUES (val1,val2,val3,...,valN)

    or

    INSERT INTO datable VALUES (val1,val2,val3,...,valN)

    or

    INSERT INTO datable SET col1=val1,col2=val2,col3=val3,...,colN=valN
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,094
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I do see a potential problem here:

    PHP Code:
    $roles[] = array(
    'rolid' => $row['rolid'],
    'name' => $row['name'],
    'selected' => FALSE);

    shouldn't that be 'roleid' => $row['roleid'] ??
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •