-1

**it throws an error

Undefined offset: 0 in C:\xampp\htdocs\test_login\add_data.php on line 10

line 10 has $_SERVER["REQUEST_METHOD" == "POST"]**

<?php 
require_once 'mysql_connect.php';

if ($_SERVER["REQUEST_METHOD" == "POST"]) {
    if (empty($_POST['username'])) {
        echo "Username required.";
    }else{
    $username = handle_data($_POST['username']);
    }

    if (empty($_POST['password'])) {
        echo "Password required.";
    }
    else{
    $password = handle_data($_POST['password']);
    }

    if ($_POST['cpassword'] != $_POST['password']) {
        echo "Passwords donot match!";
    }

    if (!is_numeric($_POST['age'])) {
        echo "Age must be a number.";
    }


$query = "INSERT INTO students(username, password, age) VALUES(?, ?, ?)";
$stmt = mysql_stmt_prepare($conn, $query);
if ($stmt) {
    mysqli_stmt_bind_param($stmt, 'ssi', $username, $password, $age );
    mysqli_execute($stmt);
    echo "Registration Made!";
    mysqli_stmt_close($stmt);
    mysqli_close($conn);
}

}

function handle_data($data){
$data = trim($data);
$data = stripcslashes($data);
$data = htmlspecialchars($data);
return $data;
}

?>

What is the actual error it is pointing at??

Arun Kumaresh
  • 6,211
  • 6
  • 32
  • 50
unixdox
  • 55
  • 4
  • 1
    Possible duplicate of [$\_POST vs. $\_SERVER\['REQUEST\_METHOD'\] == 'POST'](https://stackoverflow.com/questions/409351/post-vs-serverrequest-method-post) – Jenish Jun 30 '17 at 06:29
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – M. Eriksson Jun 30 '17 at 06:30
  • It's evaluating `"REQUEST_METHOD" == "POST"` with == meaning compare, so this returns 0 as they aren't the same and so trying to fetch $_SERVER[0]. – Nigel Ren Jun 30 '17 at 06:31

2 Answers2

3

you have semantic error in this line, REQUEST_METHOD is a key to get the method from $_SERVER global variable. change this line

if ($_SERVER["REQUEST_METHOD" == "POST"]) {

with

if ($_SERVER["REQUEST_METHOD"] == "POST") {

you can also check this with $_POST global variable as

if (isset($_POST) && !empty($_POST)) {
RAUSHAN KUMAR
  • 5,846
  • 4
  • 34
  • 70
0

you have semantic error on line no 10

use this

if($_SERVER["REQUEST_METHOD"] == "POST");
Passionate Coder
  • 7,154
  • 2
  • 19
  • 44