-1

So I have been converting a file from mysql to mysqli, so when I open the file in localhost, I get this error:

Notice: Undefined index: name in C:\xampp\htdocs\mafiagametest\Safe.php on line 80

This is line 80:

$sql = "SELECT id FROM crimetimes WHERE name='". mysqli_real_escape_string($con, $_POST['name'])."'";

What might be wrong?

  • `$_POST['name']` is not set – Roland Starke Sep 11 '17 at 08:31
  • i think the error is pretty straight forward. do a `var_dump($_POST)` to check if `$_POST` has an element with key : 'name' – Kalu Sep 11 '17 at 08:31
  • this variable is meant to be filled by submitting a form via http post method. Inspect your http headers to see it's not there. – Calimero Sep 11 '17 at 08:31
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Jigar Shah Sep 11 '17 at 08:32

1 Answers1

0

You're getting the error because 'name' isn't set, or, 'name' is being used in a place that can be null. It's a notice from PHP to tell you to fix this because it could produce unexpected results.

To fix this;

<?php
$name = (isset($_POST['name'])) ? $_POST['name'] : die("Missing a name!");

Then run your escape on $name.
By the way, check out prepared statements. Much easier way to handle user inputted data.

IsThisJavascript
  • 1,726
  • 2
  • 16
  • 25