0

I just trying to get a value from the row, but it's not happening and I only get a notice which says:

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>
        ....

Maybe something is wrong with the syntax? Or the method? How do I get the value?

The database looks like this:

Enter image description here

I appreciate any kind of help :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marcell
  • 500
  • 1
  • 4
  • 19
  • Is `$_POST['sm_value']` being sent in the request? – Egg Apr 07 '16 at 16:18
  • Hm, not really sure, where should i check that? – Marcell Apr 07 '16 at 16:25
  • 1
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Mike Doe Jul 07 '19 at 18:17

1 Answers1

1

This happens when your form is not submitted yet.

So you need to add a condition before your statement, something like this:

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.
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ameenulla0007
  • 2,663
  • 1
  • 12
  • 15
  • Tried your solution, but still getting notice in the title :( – Marcell Apr 07 '16 at 16:26
  • okay, not to worry, in programming you have many ways to get a single kind of result. that was one among those, check out the other one now. check my edit. :) – ameenulla0007 Apr 07 '16 at 16:28
  • Hm, seems better, i guess script just doesn't know which value should he get:
    Notice: Undefined variable: row in D:\xampp\htdocs\_header.php on line 31
    31 line is the html (where i query the name)
    – Marcell Apr 07 '16 at 16:32
  • yes, now enclose all the valid things within the condition, means the query and query related retrievals and displays. – ameenulla0007 Apr 07 '16 at 16:34
  • Sorry, i don't get what you saying :( – Marcell Apr 07 '16 at 16:39
  • i mean, the complete page should be within the condition. – ameenulla0007 Apr 07 '16 at 16:40
  • thats all?:( i mean understand what you saying, but there is no other way to do it? – Marcell Apr 07 '16 at 16:47
  • Better add up another condition after the `$row` var declaration, the condition would be something like this, `if(!array_key_exists('sm_name', $row)) { die("value doesn't exist"); }` – ameenulla0007 Apr 07 '16 at 17:16
  • In place of die statement, you can even mention, header redirection to redirect to some place. – ameenulla0007 Apr 07 '16 at 17:17
  • Or another, ideal method is to add default values if not exists. Something like this, `$getTitle= array_key_exists('sm_name', $row) ? $row['sm_name'] : "custom title";` and call the variable `$getTitle` under title tag. – ameenulla0007 Apr 07 '16 at 17:22