我有一个新闻列表,我也有一个表单来搜索新闻。Pdo未定义的索引通知
所以我创建了一个会话,它存储了一个sql命令,如果搜索表单被引用,我想运行,并且运行良好。
问题是当表单没有submited时,我的sql命令在sql语句中有{$ _SESSION [where]},然后我有一个未定义索引'where'错误。
所以,有林的问题是,当我不传递任何价值,我的$搜索,则SQL命令需要的值$搜索,然后因为我没有通过这个值从来就得到了错误说法没有定义这样的变量。
我正在尝试几个小时的各种替代方案,但我没有找到任何好的解决方案,请给我一点帮助?
我有名字sendForm一种形式,那么我有这样的代码,当用户提交表单:
<?php if(isset($_POST['sendForm']))
{
$search = $_POST['search'];
if(!empty($search) && $search != 'Search...')
{
$_SESSION['where'] = "AND titulo LIKE '%$search%'";
header('Location: index2.php?exe=posts/news');
}
//clean search
else
{
unset($_SESSION['where']);
header('Location: index2.php?exe=posts/news');
}
}
?>
然后我有我的文章,但也为搜索读statment结果
$pag = (empty($_GET['pag']) ? '1' : $_GET['pag']);
$max = 15;
$begin = ($pag * $max) - $max;
$readNews = $pdo->prepare("SELECT * from news {$_SESSION[where]} ORDER BY data DESC LIMIT ?, ?");
$readNews->bindValue(1, $begin,PDO::PARAM_INT);
$readNews->bindValue(2, $max,PDO::PARAM_INT);
$readNews->execute();
'session_start();'未加载。另外,这是什么'从新闻选择* {$ _SESSION [where]}'? –
将'{$ _SESSION [where]}'更改为'{$ _SESSION [',其中']}'和@ Fred-ii-上面所述^ – Darren
您的查询读为'(“SELECT * from news AND titulo LIKE' $ search%ORDER BY ...'如果'news'和'titulo'是两列,那么你可能打算使用逗号,很难说出你想达到什么样的结果,你想达到什么样的结果? –