Set each username can select items in spinner once

In my case, now each username can only select items in the spinner once. Average, if the counter has 5 items, the user can select all of them, but all of them can only be selected once. Below are my favorite php data:

<?php 

if($_SERVER['REQUEST_METHOD']=='POST'){

//Getting values
$username = $_POST['username'];

$name = $_POST['name'];

//Creating an sql query

$sql = "INSERT INTO Selection (username, name) VALUES 
('$username','$name')"; 
//Importing our db connection script
 require_once('dbConnect.php');

//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Select Successfully';
}else{
echo 'Sorry, You Already Select it Before';
}

//Closing the database 
mysqli_close($con);
}

      

The name in this php means the item in the spinner. I don't know how to set each username to select the entire item in the spinner only once. I am using localhost phpmyadmin.

+3


source to share


3 answers


You can specify a unique constraint for the username and name columns.

Modify the table Selection

using this code:



ALTER TABLE `Selection` ADD UNIQUE `unique_index`(`username`, `name`);

      

Now, if you try to insert any username and name pair that is already inserted, it will fail.

+1


source


Why don't you test if the record is already in the database before you insert it? Maybe this code (Untested) can help:



if($_SERVER['REQUEST_METHOD']=='POST')
{
    //Getting values
    $username = $_POST['username'];
    $name = $_POST['name'];

    //Importing our db connection script
     require_once('dbConnect.php');

    //Creating an sql query
    $check_sql = "SELECT * FROM Selection WHERE username='$username' AND name='$name' LIMIT 1";
    $check_res = $mysqli->query($con,$check_sql);
    if($check_res && mysqli_num_rows($check_res) >0)
    {
        echo 'Sorry, You Already Select it Before';
    }
    else
    {
        $sql = "INSERT INTO Selection (username, name) VALUES ('$username','$name')"; 
        if(mysqli_query($con,$sql))
        {
            echo 'Select Successfully';
        }
        else
        {
            echo "Select failed for some other reason";
        }
    }

    //Closing the database 
    mysqli_close($con);
}

      

+1


source


I think that in order to check that the user only rotates it once, you need to add a flag to the database structure like

 u_id | flag |
--------------
  1   | 1    
--------------

      

So when fetching or fetching or you can tell by checking that you just need to make sure that this particular u_id has already run it once, so it cannot be resolved.

So, check the username or user_id of a particular one before installing.

$sql = "SELECT u_id from user_spins table where u_id = 1 AND flag = 1";
//if yes then don't allow to proceed 
//if no then insert into User_spins table
$sql = "INSERT INTO User_spins (u_id, name,flag) VALUES 
('$username','$name',1)"; 
//Importing our db connection script
 require_once('dbConnect.php');

      

+1


source







All Articles