以空格形式添加的值php sql

kiayqfof  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(279)
<?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>

没有错误,但是每次我提交时都会添加一个空行。数据库中的表如下:user\u id user\u name user\u password full\u name age phone\u number email role我使用的是PHP7.2,来自phpmyadmin服务器的sql正在xampp上运行。尝试只使用trim而不使用isset,这给了所有参数一个未定义的索引错误

px9o7tmv

px9o7tmv1#

你的主要问题是:
在您的表单中,每个输入都有 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 ,即“”。

相关问题