php - “未定义索引通知”

标签 php mysql mysqli

我只是想从行中获取一个值,但它没有发生,我只收到一条通知,上面写着:

Notice: Undefined index: sm_value in D:\xampp\htdocs_header.php on line 16

<?php
    require "./conf/db.php"; // Additional data

    session_start();

    if (isset($_SESSION["UserID"])) {
    }
    else {
        header('Location: login.php?=redirect');
    }

    // If user click to logout
    if (isset($_GET["account"]) && $_GET['account'] == "logout") {
        unset($_SESSION["UserID"]);
        session_destroy();
        header("Location: index.php"); // Redirect him/her to index.php
        exit();
    }

    $name = mysqli_escape_string($mysqli, $_POST['sm_value']);
    $GetTitle = $mysqli->query("select * from sm_options where sm_value='$name'");
    $row = $GetTitle->fetch_array(MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title><?php echo $row['sm_name'];?></title>
        ....

也许语法有问题?或者方法?如何获取该值?

数据库如下所示:

Enter image description here

我感谢任何形式的帮助:)

最佳答案

当您的表单尚未提交时,就会发生这种情况。

因此您需要在语句之前添加一个条件,如下所示:

if(!empty($_POST) and array_key_exists("sm_value", $_POST)) {
    $name = mysqli_escape_string($mysqli, $_POST['sm_value']);
    $GetTitle = $mysqli->query("select * from sm_options where sm_value='$name'");
    $row = $GetTitle->fetch_array(MYSQLI_ASSOC);
    // Every statement and HTML which is required under it when the value is not empty
}
else {
    // An error message, when you say the details are not available.
}

关于php - “未定义索引通知”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36481769/

相关文章:

php - 如何优化 php 中嵌套循环的 mysql 查询

php - Laravel 4 where in condition with DB::select 查询

PHP 数组没有返回值

php - 将框架重定向到 https?

php - PHPUnit 中两个 TestCase 类之间的依赖测试

php - 热更新mysql_fetch_array

mySQL - 如何将日期格式从 dd-mm-yy 更改为 yy-mm-dd?

php - mysqli_affected_rows() 返回 -1 但查询有效

php - 如何使用 php 和 mysqli 在一次事务中上传多个图像文件?

php - mysqli_query() 上的空白页;?