<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$username = $fullname = $password = $age = $phonenumber = $role = $email = "";
$username_err = $fullname_err = $password_err = $age_err = $phonenumber_err =
$role_err = $email_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate name
if(var_dump($_POST["username"]))
{
$input_name = trim($_POST["username"]);
if(empty($input_name)){
$username_err = "Please enter a name.";
} elseif(!filter_var($input_name, FILTER_VALIDATE_REGEXP,
array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$username_err = "Please enter a valid name.";
} else{
$username = $input_name;
}
}
// Validate fullname
if(var_dump($_POST["username"]))
{
$input_fname = trim($_POST["fullname"]);
if(empty($input_fname)){
$fullname_err = "Please enter a name.";
} elseif(!filter_var($input_fname, FILTER_VALIDATE_REGEXP,
array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$fullname_err = "Please enter a valid name.";
} else{
$fullname = $input_fname;
}
}
// Validate age
if(var_dump($_POST["age"]))
{
$input_age = trim($_POST["age"]);
if(empty($input_age)){
$age_err = "Please enter your age.";
} else{
$age = $input_age;
}
}
// Validate phonenumber
if(var_dump($_POST["phonenumber"]))
{
$input_phonenumber = trim($_POST["phonenumber"]);
if(empty($input_phonenumber)){
$phonenumber_err = "Please enter a proper phonenumber.";
} else{
$phonenumber = $input_phonenumber;
}
}
// Validate role
if(var_dump($_POST["role"]))
{
$input_role = trim($_POST["role"]);
if(empty($input_role)){
$role_err = "Please enter a proper role.";
} else{
$role = $input_role;
}
}
// Check input errors before inserting in database
if(empty($username_err) && empty($fullname_err) && empty($age_err) &&
empty($phonenumber_err) && empty($role_err)){
// Prepare an insert statement
$sql = "INSERT INTO users (user_name, full_name, age, phone_number,
role) VALUES (?, ?, ?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssiis", $param_username,
$param_fullname, $param_age, $param_phonenumber, $param_role);
// Set parameters
$param_username = $username;
$param_fullname = $fullname;
$param_age = $age;
$param_phonenumber = $phonenumber;
$param_roll = $roll;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
echo "Records created successfully. Redirect to landing page";
// Records created successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Record</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>ADD NEW USERS</h2>
</div>
<p>Please fill this form to start trading.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
<label>Name</label>
<input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
<span class="help-block"><?php echo $username_err;?></span>
</div>
<div class="form-group <?php echo (!empty($fullname_err)) ? 'has-error' : ''; ?>">
<label>FULL NAME</label>
<input type="text" name="fullname" class="form-control" value="<?php echo $fullname; ?>">
<span class="help-block"><?php echo $fullname_err;?></span>
</div>
<div class="form-group <?php echo (!empty($age_err)) ? 'has-error' : ''; ?>">
<label>AGE</label>
<input type="number" name="age" class="form-control" value="<?php echo $age; ?>">
<span class="help-block"><?php echo $age_err;?></span>
</div>
<div class="form-group <?php echo (!empty($phonenumber_err)) ? 'has-error' : ''; ?>">
<label>PHONENUMBER</label>
<input type="number" name="phonenumber" class="form-control" value="<?php echo $phonenumber; ?>">
<span class="help-block"><?php echo $phonenumber_err;?></span>
</div>
<div class="form-group <?php echo (!empty($role_err)) ? 'has-error' : ''; ?>">
<label>ROLE </label>
<form action="" method="post">
<input type="radio" name="radio" value="<php echo $role; ?>">INVESTOR
<input type="radio" name="radio" value="<php echo $role; ?>">MANAGER
<span class="help-block"><?php echo $role_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>
</div>
</div>
我提交的每一次添加空白行都没有错误 数据库中的表如下 用户身份 用户名 用户密码 全名 年龄 电话号码 电子邮件 角色 我正在使用php 7.2和phpmyadmin服务器上的sql在xampp上运行 仅使用不带isset的Trim进行尝试,这给所有参数带来了未定义的索引错误
答案 0 :(得分:0)
您的主要问题:
在您的表单中,每个输入都有name="name"
。
它应该是“用户名”,“全名”,“电话号码”,..
这就是为什么您在$_POST['username']
中没有收到任何值的原因。
那么为什么不出现任何错误?因为如果isset($_POST['username'])
为假,则不会设置一个:
if(isset($_POST["username"]))
{
$input_name = trim($_POST["username"]);
if(empty($input_name)){
$username_err = "Please enter a name.";
} elseif(!filter_var($input_name, FILTER_VALIDATE_REGEXP,
array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$username_err = "Please enter a valid name.";
} else{
$username = $input_name;
}
}
// NO ELSE here. here you should set $fullname_err
因此,在INSERT上,您绑定到$username
的原始初始化值“”。