Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么php表单可以使用value=";名称“;非值="$_POST[&"x27;value&"x27;]“是什么;?_Php - Fatal编程技术网

为什么php表单可以使用value=";名称“;非值="$_POST[&"x27;value&"x27;]“是什么;?

为什么php表单可以使用value=";名称“;非值="$_POST[&"x27;value&"x27;]“是什么;?,php,Php,这里需要帮助。使用固定值插入时,代码运行良好,但变量存在问题。我得到的只是一个空名字。仔细检查,但没有发现问题。感谢您的帮助 mysql输出: id name 5 代码 if(isset($\u会话['user\u id'])){ 标题('Location:index.php'); } include_once“includes/db_connect.php”; //定义变量并设置为空值 $name_error=''; $name=''; 如果($\u服务器['REQUEST\u

这里需要帮助。使用固定值插入时,代码运行良好,但变量存在问题。我得到的只是一个空名字。仔细检查,但没有发现问题。感谢您的帮助

mysql输出:

id name
5      
代码
if(isset($\u会话['user\u id'])){
标题('Location:index.php');
}
include_once“includes/db_connect.php”;
//定义变量并设置为空值
$name_error='';
$name='';
如果($\u服务器['REQUEST\u METHOD']=='POST'){
if(空($_POST['name'])){
$name_error='name是必需的';
}否则{
$name=mysqli\u real\u escape\u字符串($connection,$\u POST['name']);
//检查名称是否仅包含字母和空格
如果(!preg_match(“/^[a-zA-Z]*$/”,$name)){
$name_error='只允许字母和空格';
}
}
$sql=“插入到用户(名称)值(“$name.”)中”;
if(!mysqli_query($connection,$sql)){
die('Error:'.mysqli_Error($connection));
}
}
?>

$\u POST['name']
更改为
$\u POST['register\u name']


因为您在
表单字段中使用
register\u name

了解防止SQL注入的准备好的语句我得到的名称、电子邮件、密码和部门都是空的?在数据库中,您从不插入电子邮件、密码等。表单中的字段名为
register\u name
而不是
name
,数据库中只有id和名称,名称为空。那么如何在文本框中显示此空名称?再次确认你的问题,然后在这里张贴我的坏我把我的脚本缩短到名字
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>