PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 2, 2022

[FIXED] why does my $type variable get saved as 'Admin''?

 July 02, 2022     pdo, php, sql, xampp     No comments   

Issue

am trying to get the number of admins in my table it does work normally but now that i added inner join for two tables i get this error of an extra ' in the word admin and i don't know how to remove it so here is my function in userc.php :

    function admincountsearch($key)
    {
        $con = config::getConnexion();
        $sql = "SELECT  user.username,user.name,user.lastname,user.id, 
                    user.email, user.image, user.verified, usero.type, 
                    usero.description,usero.ban 
            FROM user 
                INNER JOIN usero ON user.id_o = usero.id 
            WHERE user.username LIKE :keyword 
                OR user.id LIKE :keyword 
                OR user.name LIKE :keyword 
            WHERE usero.type=:type ";
        $stmt = $con->prepare($sql);
        $type ="Admin" ;
        $stmt->bindValue(':keyword', '%' . $key . '%', PDO::PARAM_STR);
        $stmt->bindValue(':type',$type, PDO::PARAM_STR);
        $stmt->execute();
        $stmt->fetch();
        $results = $stmt->rowCount();
        return $results;
    }

and here is how i call it in find.php:

<span class="info-box-number"><?php echo $userc->admincountsearch($key); ?></span>

and here is the error that i got:

: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE usero.type='Admin'' at line 2 in C:\xampp\htdocs\Project\Integre\Controller\userc.php:458 Stack trace: #0 C:\xampp\htdocs\Project\Integre\Controller\userc.php(458): PDOStatement->execute() #1 C:\xampp\htdocs\Project\Integre\Views\find.php(200): userc->admincountsearch('g') #2 {main} thrown in


Solution

You have 2 WHERE clauses in your statement. You probably meant

SELECT
  user.username,
  user.name,
  user.lastname,
  user.id,
  user.email,
  user.image,
  user.verified,
  usero.type,
  usero.description,
  usero.ban
FROM
    user
INNER JOIN usero ON
    user.id_o = usero.id
WHERE (
  user.username LIKE :keyword OR
  user.id LIKE :keyword OR
  user.name LIKE :keyword
) AND
usero.type=:type


Answered By - Guido Faecke
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing