提交表单时,会抛出错误

时间:2017-06-30 06:26:51

标签: php mysql form-submit

**它会抛出错误

  

未定义的偏移量:第10行的C:\ xampp \ htdocs \ test_login \ add_data.php中的0

第10行有$ _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;
}

?>

它指向的实际错误是什么?

2 个答案:

答案 0 :(得分:2)

此行中有semantic errorREQUEST_METHOD是从$_SERVER全局变量获取方法的关键。改变这一行

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

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

您也可以使用$_POST全局变量

进行检查
if (isset($_POST) && !empty($_POST)) {

答案 1 :(得分:0)

你在第10行有semantic error

使用此

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