Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3976774
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:50:41+00:00 2026-05-20T04:50:41+00:00

I am trying to implement a function that will insert a new entry in

  • 0

I am trying to implement a function that will insert a new entry in a database if a field with same name (as the one given) doesn’t already exist. In particular I want to restrict duplicate usernames in a table.

The only way I could think was to run a select query and then if that doesn’t return anything run the insert query. For some reason though I cant get it to work…


My db select Function

function getAllUsers($user)
{
    $stmt = $this->db->stmt_init();
    $stmt->prepare('SELECT username from users where username=? ');
    $stmt->bind_param("s", $user);
    $stmt->bind_result($username);
    $stmt->execute();
    $results = array();
    while($stmt->fetch())
    {
        $results[] = array('username' => $username);
    }        
    $stmt->close();        
    return $results;
}

My php code (this is in a different page)

foreach ($GLOBALS['db']->getAllUsers($_POST['username']) as $i)
{
    $results =  "".$i['username']."";       
    break;
}    
if(strcmp($results, "")==0)
{
    if($GLOBALS['db']->addUser($_POST['username'],$_POST['password']))
    {
            session_destroy();                
            echo "registerSucces";
    }
    else
    {
            session_destroy();
            echo "registerError";
    }
}
else
{
        echo "userNameExists";
}

Can you see whats wrong with this???

Thanks

Mike


Still cant find how to make the above code work but just in case someone needs this: A temporary simple solution is not to compare strings at all and instead have a counter in the foreach loop and then check that upon a desired number(0 in my case)…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:50:42+00:00Added an answer on May 20, 2026 at 4:50 am

    SQLite supports the UNIQUE constraint. Simply declare a UNIQUE index on the column and check whether an INSERT fails.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to C++ and trying to implement a turtle emulator that will read
I'm trying to implement an ActionMailer function that will send out a newsletter to
I am trying to implement a function that will check to see if a
Trying to implement a function in my linkedlist class that will return total amount
I'm trying to implement unit tests for function that uses imported external objects. For
I'm trying to implement a unit test for a function in a project that
Hey guys, I'm currently trying to implement a function using C that takes in
I am trying to implement in C++ a function that determines the cut of
I'm trying to implement some kind of caching in a PHP script that will
I'm trying to create a simple program or function that will take the users

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.