-5

Hello I have while adding a login user I want to check if this user is already in DB, it will display a message if not it will be added to DB: This is the form in html:

<form name="inscription" method="post" action="03insert.php">
        login: <input type="text" name="login" value=""/>
               <input type="submit" name="submit" value="Inscription"/>
</form> 

And this the code in PHP:

<?php
        $connection = ConnectionBD();   

        if( isset($_POST['submit']) ){
            $login = $_POST['login'];

            $sql_1 = "SELECT * FROM users WHERE user_login = :login";
            $query = $connection->prepare($sql_1);
            $query->bindParam(':login', $login, PDO::PARAM_STR);
            $query->execute();
            $count = $query->rowCount();
            if ($count > 0){
                echo 'This login already exist';
            }
            else
            {
            $sql_2 = 'INSERT INTO users VALUES(null,"'.$login.'")';
            $result = $connection->exec( $sql_2 );
            if( $result > 0 ){
                echo 'Registered successfully';
            }else{ echo 'ERROR !<br><br>'; }
            }
        }
        unset( $connection );   
    ?>

I don't have error but It add the user even its exist ..Please help!!

Sam Belge
  • 3
  • 6

2 Answers2

0

Your first line of code, you use this:

$connection = ConnectionBD(); 

Why is there no new keyword or is it a standard function? If it's a class you need to instantiate it, i.e.:

$connection = new ConnectionBD(); 
// OR
$connection = ConnectionBD()::initFunction(); // If it's static 

You won't get a thang from it as the object is not instantiated properly

Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
-1

Try to change your query to this:

$sql_1 = "SELECT * FROM users WHERE user_login = :login";
//just removed the single quotes '
RaisoMos
  • 159
  • 4
  • 11
  • 1
    Even though it solved it, this is a very low-quality answer. – Funk Forty Niner Jan 20 '16 at 15:47
  • and I can't see why this comment *"I changed, but nothing changed"* is still in here @SamBelge so delete it – Funk Forty Niner Jan 20 '16 at 15:48
  • @Fred-ii- It didn't solve yet, sorry because Im new in tis website, and I don't knpw how to use it well .. – Sam Belge Jan 20 '16 at 15:55
  • @SamBelge you accepted it earlier and now you've made me look like a fool closing your question about it. What kind of game are you playing here? and to the moderators: why was my comment about "if they were paying attention to comments up there" removed from here?? – Funk Forty Niner Jan 20 '16 at 15:57
  • @SamBelge plus, you just made an edit again, and did not mark it as an edit, in turn being the reason why this person got a downvote for their answer; which is NOT my downvote. So, you're obviously NOT showing us your full code and you state you're not getting errors. That is because you're NOT checking for them and how do we know if you even successfully connected to db? – Funk Forty Niner Jan 20 '16 at 15:59