为什么php表单在value =" name"不值=" $ _ POST ['价值']"?

时间:2017-11-02 07:45:49

标签: php

需要一些帮助。代码运行正常,具有固定值插入,但问题有变量。我得到的只是空名。双重检查,但无法找到问题。任何帮助表示赞赏。

mysql输出:

id name
5      

代码     

if (isset($_SESSION['user_id'])) {
  header('Location: index.php');
}

include_once 'includes/db_connect.php';

// define variables and set to empty values
$name_error = '';
$name = '';

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

  if (empty($_POST['name'])) {
    $name_error = 'Name is required';
  } else {
    $name = mysqli_real_escape_string($connection, $_POST['name']);
    // check if name contains only letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
      $name_error = 'Only letters and white space allowed';
    }
  }

  $sql = "INSERT INTO users (name) VALUES ('" . $name . "')";

  if (!mysqli_query($connection, $sql)) {
    die('Error : ' . mysqli_error($connection));
  }

}

?>


<!-- create account  -->
<form class="form-inline" action="<?php echo ($_SERVER['PHP_SELF']); ?>" method="post">

  <!-- name -->
  <div class="form-group">
    <input type="text" name="register_name" value="<?php echo $name; ?>" class="form-control" placeholder="Name" size="35" required>
  </div>

  <!-- submit -->
  <div class="form-group">
    <input type="submit" name="submit" value="submit" class="btn btn-primary btn-flat"></input>
  </div>

</form>

1 个答案:

答案 0 :(得分:0)

$_POST['name']更改为$_POST['register_name']

因为您在register_name

中使用form field