我正在处理一个将接收用户信息的表单,当用户单击“提交”按钮时,它将被保存在数据库的 user 表中。
问题在于用户输入似乎未记录在表中,我不确定原因。这是表单页面的轮廓
add_profile.php
<?php
require('Model/database.php');
$query = 'SELECT *
FROM goal
ORDER BY userGoalID';
$statement = $db->prepare($query);
$statement->execute();
$categories = $statement->fetchAll();
$statement->closeCursor();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/add_profile.css"/>
</head>
<body>
<main>
<div class="fw">
<h1 class="fw__title">Insert Your Data!</h1>
<div class="fw__progress">
<ul class="fw__breadcrumbs">
<li class="fw__breadcrumbs__item">
<div class="fw__breadcrumbs__element">1</div>
</li>
<li class="fw__breadcrumbs__item">
<div class="fw__breadcrumbs__element">2</div>
</li>
<li class="fw__breadcrumbs__item">
<div class="fw__breadcrumbs__element"><span class="done"></span></div>
</li>
</ul>
</div>
<form id="form" class="fw__wrapper" novalidate method="post" action="profile.php">
<fieldset class="fw__inputs fw__step">
<input type="text" name="first_name" placeholder="First Name*" data-label="First Name" data-validation="required"/>
<input type="text" name="last_name" placeholder="Last Name*" data-label="Last Name" data-validation="required"/>
<input type="email" name="email" placeholder="Email*" data-validation="required email" data-label="Email" />
<input type="text" name="phone" placeholder="Phone" data-label="Phone" data-validation="phone"/>
<div class="fw__inputs__error"><p class="fw__inputs__error--message"></p></div>
<input class="fw__button" type="button" value="Next" onclick="next()"/>
</fieldset>
<fieldset class="fw__inputs fw__step">
<input type="text" name="age" placeholder="Age" data-label="Age" data-validation="required naturalNumber"/>
<input type="text" name="weight" placeholder="Weight" data-label="Weight" data-validation="required naturalNumber"/>
<input type="text" name="height" placeholder="Height" data-label="Height" data-validation="required naturalNumber"/>
<label for="sttype" class="fw__inputs__label fw__inputs__label--inline">Goal*</label>
<select id="sttype" class="fw__inputs__selector" name="goal_id" data-label="Your Goal" data-validation="required select">
<option value="" disabled selected hidden>Please select one...</option>
<?php foreach ($categories as $goal) : ?>
<option value="<?php echo $goal['userGoalID']; ?>">
<?php echo $goal['userGoalType']; ?>
</option>
<?php endforeach; ?>
</select>
<div class="fw__inputs__error"><p class="fw__inputs__error--message"></p></div>
<input class="fw__button fw__button--negative" type="button" value="Back" onclick="back()"/>
<input id="submit" class="fw__button" type="submit" value="Review Data"/>
</fieldset>
<section class="fw__result fw__step">
<h4 class="fw__result__title">Your Details</h4>
<div id="results"></div>
<input class="fw__button fw__result__button" type="submit" value="Submit">
</section>
</form>
</div>
</main>
<script src="js/add_profile.js"></script>
</body>
</html>
对于所有sql命令,我都有一个单独的页面,称为 profile_test.php
<?php
session_start();
if(!isset($_SESSION['user_id'])){
header('Location: login.php');
exit;
}else{
$user_id = filter_input(INPUT_POST, 'user_id', FILTER_VALIDATE_INT);
$name = filter_input(INPUT_POST, 'name');
$age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT);
$weight = filter_input(INPUT_POST, 'weight', FILTER_VALIDATE_FLOAT);
$height = filter_input(INPUT_POST, 'height', FILTER_VALIDATE_FLOAT);
$goal_id = filter_input(INPUT_POST, 'goal_id', FILTER_VALIDATE_INT);
$first_name = filter_input(INPUT_POST, 'first_name');
$last_name = filter_input(INPUT_POST, 'last_name');
$email = filter_input(INPUT_POST, 'email');
$phone = filter_input(INPUT_POST, 'phone', FILTER_VALIDATE_INT);
//Validate INPUT_POST
if ($goal_id == null || $goal_id == false ||
$name == null || $age == null || $weight == null || $height == null || $first_name == null || $last_name == null || $email == null || $phone == null) {
$error = "Invalid user data. Check all fields and try again.";
//include('../Error/error.php');
} else {
require_once('Model/database.php');
//Add user information
$query = 'INSERT INTO user
(userID, userName, userAge, userWeight, userHeight, userGoalID, firstName, lastName, userEmail, userPhone)
values
(:userID ,:name, :age, :weight, :height, :goal_id, :first_name, :last_name, :email, :phone)';
$statement = $db->prepare($query);
$statement->bindValue(':userID', $_SESSION['user_id']);
$statement->bindValue(':name', $name);
$statement->bindValue(':age', $age);
$statement->bindValue(':weight', $weight);
$statement->bindValue(':height', $height);
$statement->bindValue(':goal_id', $goal_id);
$statement->bindValue(':first_name', $first_name);
$statement->bindValue(':last_name', $last_name);
$statement->bindValue(':email', $email);
$statement->bindValue(':phone', $phone);
$statement->execute();
$statement->closeCursor();
// Display the Product List page
include('index.php');
}
}
?>
Javascript仅用于验证。最初,用户将插入他们的第一组数据(名字,姓氏,电子邮件,电话号码),然后转到第二页以插入其健康数据(年龄,体重,身高和目标)。只有在那之后,用户才能单击提交按钮以将要记录在用户表中的数据
。