替换PHP“未定义变量”错误?

时间:2014-11-16 17:21:37

标签: php dictionary undefined

所以基本上我想创建一个在线词典,要搜索的词是由输入引入的,我想这样做,如果它找不到db中的单词+定义来说出一条消息“我们找不到任何定义“或类似的东西,在我的代码中,因为它无法找到它,它的方式是”未定义的变量“

<?php
include ("header.php");
$search = $_POST['se'];
include ("connect.php");

$dictionary=mysqli_query($con,"SELECT * FROM `dictionary` WHERE word = '$search'");

while($row = mysqli_fetch_array( $dictionary )) {
    $word=$row['word'];
    $description=$row['definition'];
    }
                    echo "<div class=\"webResult\">
                    <h2>$word</h2>
                    <p>$description</p>
                    </div>";
include ("footer.php");
?>

P.S。:我知道我的代码很容易受到SQLi的攻击,但我稍后会解决这个问题。

5 个答案:

答案 0 :(得分:1)

尝试在isset

中包装未定义的变量
if (isset($word) && isset($description)) {
    echo "<div class=\"webResult\">
    <h2>$word</h2>
    <p>$description</p>
    </div>";
}
else {
    echo "Nothing found";
}

$search = $_POST['se'];

也是如此
if(!isset($_POST['se'])) {
    echo "Nothing found";
    exit;
}

答案 1 :(得分:0)

if($row = mysqli_fetch_array( $dictionary )) {
    $word=$row['word'];
    $description=$row['definition'];

    echo "<div class=\"webResult\">
    <h2>$word</h2>
    <p>$description</p>
    </div>";
} else {
    echo "$search not found";
}

答案 2 :(得分:0)

while($row = mysqli_fetch_array( $dictionary )) {

    $word=$row['word'];
    $description=$row['definition'];

 }
if(!empty($word) && !empty($description)){

      echo '<div class=\"webResult\">';
      echo '<h2>$word</h2>';
      echo '<p>$description</p>';
      echo '</div>"; 

}else{
  echo 'could not find your word';
}


include ("footer.php");
?>

如果变量有值,则必须验证变量

答案 3 :(得分:0)

首先在$word循环内创建变量$descriptionwhile。这意味着它们之外没有任何scope。这应该是最可能的原因,因为您收到variable not defined错误。

这里有几个选择。

一个,您可以在<{em>} {/ 1}}循环之外创建这些变量,然后像现在一样为它们分配新值。如果你选择这样做,这就是你的代码的样子:

while

如果您在返回的$search = $con->escape_string($_POST['se']); $dictionary = $con->query("SELECT * FROM `dictionary` WHERE word = '$search'"); $word = ""; $description = ""; while($row = $dictionary->fetch_assoc()) { $word = $row['word']; $description = $row['definition']; } if (!empty($word) && !empty($description)) { echo '<div class="webResult"><h2>' . $word . '</h2><p>' . $description . '</p></div>'; } else { echo "We couldn't find any definition"; } 中有多个结果,并希望使用列表中的最后一个,这将对您有用。

两个,如果您可能只返回一个结果,或者您只想使用列表中的第一个结果,则可以使用mysqli_resource语句内部echo语句,用于检查是否返回了有效结果。例如:

if

注意:

在上面的示例中,我们使用$search = $con->escape_string($_POST['se']); $dictionary = $con->query("SELECT * FROM `dictionary` WHERE word = '$search'"); $word = ""; $description = ""; $row = $dictionary->fetch_assoc(); if($row) { $word = $row['word']; $description = $row['definition']; if (!empty($word) && !empty($description)) { echo '<div class="webResult"><h2>' . $word . '</h2><p>' . $description . '</p></div>'; } else { echo "We couldn't find any definition"; } } 来检查变量。这是因为!empty()在这里毫无意义,因为我们已经创建了(设置)变量。

要阅读的内容:

答案 4 :(得分:-1)

试试这个:

include ("header.php");
$search = $_POST['se'];
include ("connect.php");

$dictionary=mysqli_query($con,"SELECT * FROM `dictionary` WHERE word = '$search'");


while($row = mysqli_fetch_array( $dictionary )) {
    $word=$row['word'];
    $description=$row['definition'];
    }

if(isset($word)){
               echo "<div class=\"webResult\">
                    <h2>$word</h2>
                    <p>$description</p>
                    </div>";
}
else{
    echo "We couldn't find any definition";
}
include ("footer.php");