【问题标题】:Search Engine Script - Undefined variables搜索引擎脚本 - 未定义的变量
【发布时间】:2016-10-23 10:27:30
【问题描述】:

我的代码有一个小问题,我不知道是什么问题..

我正在尝试用 PHP 制作一个示例搜索引擎工具,一切正常,直到我尝试从数据库中搜索一些帖子...当我这样做时,它说我没有 $header 和 $生物声明...

<?php

include('connection.php');

$query =mysqli_real_escape_string($dbc, $_POST['query']);

$q = mysqli_query($dbc, "SELECT id FROM search WHERE header LIKE '%$query%' OR bio LIKE '%$query%'");


$num = mysqli_num_rows($q);

echo $num;

if(!$query){
echo "Enter a query...";
} else {

if($num != 0)
{

    echo "<hr>";
    while ($fetch = mysqli_fetch_assoc($q)){

       $id = $fetch['id'];
        $header = $fetch['header'];
        $bio = $fetch['bio'];


    echo "<strong>" . $header . "</strong>";
    echo "<blockquote><p>" . $bio . "</p></blockquote>";
    echo "<hr>";

    }

} else {
    echo "No results where found .. ";
}

}

?>

和形式

<div style = "width:300px; margin:auto;">

    <h1> Add Search Criteria</h1>
    <p> Type a header and bio below to add to search engine</p>

    <p>
        <input id="header" name = "header" type="text" placeholder="header" style="width:100%;">
    </p>

    <p>
        <textarea id="bio" name="bio" cols="40" rows="7" placeholder="Write a bio.."></textarea>
    </p>

    <p>
        <center>
            <button id="submit">Submit Search</button>
        </center>
    </p>

    <div id="add_error" style="text-align:center"></div>

    <hr>

    <h1>Search The Database</h1>
    <p>Please type something to search to database</p>

    <p>
        <input name = "query" id="query" type="text" placeholder="search">
        <button id="search">Search</button>
    </p>

    <div id="search_error">

    </div>

</div>

这是它的输出

注意:未定义的索引:第 25 行 C:\wamp64\www\mywebsite\Search\search.php 中的标头 调用栈

时间记忆功能定位

1 0.0021 242472 {main}( ) ...\search.php:0

( ! ) 注意:未定义索引:第 26 行 C:\wamp64\www\mywebsite\Search\search.php 中的 bio 调用栈

时间记忆功能定位

1 0.0021 242472 {main}( ) ...\search.php:0

【问题讨论】:

  • 在您的选择查询中添加标题和简介列。
  • headerbio 添加到您的查询中,例如SELECT id, header, bio FROM search WHERE header LIKE '%$query%' OR bio LIKE '%$query%'。但是您确实应该使用准备好的查询来防止攻击。

标签: php html mysqli


【解决方案1】:

在您的选择查询中添加标题和简介列。

$q = mysqli_query($dbc, "SELECT id, header, bio FROM search WHERE header LIKE '%$query%' OR bio LIKE '%$query%'");

【讨论】:

  • 或 ...选择所有 SELECT * FROM ....谢谢你的想法,你让我大开眼界
  • 请不要选择 Select * 选项,因为您最终会从表中获取所有列,包括您不需要的列。仅将列添加到您需要在脚本中处理的 Select 语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2015-01-20
相关资源
最近更新 更多