简体   繁体   中英

Pdo undefined index and invalid parameter number

I´m trying to develop a basic search system and I´m getting an issue with my pdo statement adding my $_SESSION to the sql statement.

Errors Im getting:

Notice: Undefined index: where in -> $readNews = $pdo->prepare("SELECT * from news $_SESSION[where] ORDER BY data");

Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in -> $readNews->execute();

Somebody there see something that Im doing wrong?

if(isset($_POST['sendForm']))
    {
        $search = $_POST['search'];
        if(!empty($search) && $search != 'Title:')
        {
            $_SESSION['where'] = "WHERE title LIKE ?";
        }
        else
        {
            unset($_SESSION['where']);
        }

    }

PDO statement:

    $readNews = $pdo->prepare("SELECT * from news $_SESSION[where]  ORDER BY date");  
    $readNews->bindValue(1, "%$search%");
    $readNews->execute();

I already did this with normal mysql, like this and its working:

"select * from news {$_SESSION[where]} ORDER BY date DESC";

But now I´m trying to do with PDO..

未定义索引:表示您的$ _SESSION数组中没有此类项目

$_SESSION[where] should be {$_SESSION['where']}

Other wise interpreter try to find where as constant.

if(isset($_POST['sendForm']))
    {
        $search = $_POST['search'];
        if(!empty($search) && $search != 'Title:')
        {
            $_SESSION['where'] = "WHERE title LIKE ?";
        }
        else
        {
            $_SESSION['where']='';//changed here because you are trying to access it
        }

    }

and

$readNews = $pdo->prepare("SELECT * from news {$_SESSION['where']}  ORDER BY date");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM