Problem with $placeholders?

my original code works ok which is:

  //The basic SELECT statement
    $select = 'SELECT id, name';
    $from = ' FROM category';
    $where = ' WHERE TRUE';
    
    $placeholders = array();
if ($_GET['type']==1) //category is selected
{
    $where .= " AND name LIKE :name";
    $placeholders[':name'] = '%' . $_GET['text'] . '%';    
try
{
    $sql = $select . $from . $where;
    $s = $pdo->prepare($sql);
    $s->execute($placeholders);
}
catch (PDOException $e)
{
    $error = 'product type error.';
    include 'error.html.php';
    exit();
}

However, I add a alternative type name called alter1, the code becomes:

  //The basic SELECT statement
    $select = 'SELECT id, name';
    $from = ' FROM category';
    $where = ' WHERE TRUE';
    
    $placeholders = array();
    
if ($_GET['type']==1) //category is selected
{
    $where .= " AND name LIKE :name OR name LIKE :alter1";      //new line
    $placeholders[':name'] = '%' . $_GET['text'] . '%';      
    $placeholders[':alter1'] = '%' . $_GET['text'] . '%';         //new line

try
{
    $sql = $select . $from . $where;
    $s = $pdo->prepare($sql);
    $s->execute($placeholders);
}
catch (PDOException $e)
{
    $error = 'product type error.';
    include 'error.html.php';
    exit();
}

my problem is the alter1 seems not working correctly, I’m guessing I’m not doing right with the “placeholder part”

Is it ok that you’re filtering the same field with the same text twice?

Yes, that doesn’t make sense - you check that the name field contains the same text.

I see what I wanna do is : AND name LIKE :name OR alter1 LIKE :alter1

It was a mistake. Thanks for pointing it out. It should be AND name LIKE :name OR alter1 LIKE :alter1

Has it fixed your problem?

Yes, It does. Thank you very much.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.