Home > database >  trying to stop duplicate entry for widget ids for users that have already added widget id
trying to stop duplicate entry for widget ids for users that have already added widget id

Time:09-30

Hi there am coding a small check function that will check a mysql database for a user_id and a widget_id and if the widget id is found to be in table that matchs the user id then it would not let the person add that widget agen the issue i have is soon as one widget is added to database it will not let me add any more for that user_id table would look like this

widget_id  user_id
1          12
5          12
6          10
9          2
1          2
1          10

so if a user adds same widget as a other user it will allow it but if that user say user (10) tries to re-add widget (1) it would disallow it but if user (10) selects widget (9) it would allow it

  public function check_u_widgets($user_id, $widget_id)
  {
    global $db, $system;
    $result = $db->query(sprintf("SELECT * FROM users_widgets WHERE user_id = %s", secure($user_id) )) or _error("SQL_ERROR_THROWEN");
    if ($result->num_rows > 0) {
      $wid = $result->fetch_assoc();
      if($wid['widget_id'] == $widget_id) {
        throw new Exception(__("Sorry, It seem's that this widget is already assigned"));
      }
    }
    return false;
  }

any one got any ideas on how to sort this thanks alot

CodePudding user response:

Check for both conditions (AND) and if found then don't allow.

<?php

function check_u_widgets($user_id, $widget_id)
{
    global $db, $system;
    $result = $db->query(sprintf("SELECT * FROM users_widgets WHERE user_id = %s AND widget_id = %s", secure($user_id), secure($widget_id))) or _error("SQL_ERROR_THROWEN");
    if ($result->num_rows > 0) {
        throw new Exception(__("Sorry, It seem's that this widget is already assigned")); // to this user
    }
    return false;
}

  • Related