我有一个HTML表单,提交时将数据发送到createUser.php。然后将数据存储在变量中,并使用enterUserData函数将其插入数据库。我var_dump $ _POST数组,所有数据都在那里,但是,它没有进入数据库。我检查了拼写错误,删除了婚姻状态,因为它在数据列表中,将$ _POST更改为$ _REQUEST,但它仍然无法正常工作。也没有400或500错误。与数据库的连接很好。我在程序的其他部分使用相同的connect.php文件,它们工作正常。任何洞察这个问题将不胜感激。
这是表格。
<form id="createUserForm" class="" action="createUser.php" method="POST" onsubmit="return createUser.validateForm('createUserForm')">
<label for="formUsername">Username: </label>
<input id="formUsername" type="text" required name="Username" placeholder="Username">
<br />
<label for="formPassword">Password: </label>
<input id="formPassword" type="text" required name="Password" placeholder="Password">
<br />
<label for="formFirstname">Firstname: </label>
<input id="formFirstname" type="text" required name="Firstname" placeholder="Firstname">
<br />
<label for="formLastname">Lastname: </label>
<input id="formLastname" type="text" required name="Lastname" placeholder="Lastname">
<br />
<label for="formFavColor">Favorite Color: </label>
<input id="formFavColor" type="text" name="Favorite Color" placeholder="Favorite Color">
<br />
<label for="formFavMove">Favorite Movie: </label>
<input id="formFavMove" type="text" name="Favorite Movie" placeholder="Favorite Movie">
<br />
<label for="formFavAnimal">Favorite Animal: </label>
<input id="formFavAnimal" type="text" name="Favorite Animal" placeholder="Favorite Animal">
<br />
<label for="formFaveVideogame">Favorite Videogame:</label>
<input id="formFaveVideogame" type="text" name="Favorite Videogame" placeholder="Favorite Videogame">
<br />
<label for="formMarital">Marital Status: </label>
<input list="formMarital" name="Marital Status">
<datalist id="formMarital" type="text" name="" placeholder="Marital Status">
<option value="Single"></option>
<option value="Married"></option>
<option value="None of my business"></option>
</datalist>
<br />
<button id="submitCreateUser" type="submit" name="button">Create User</button>
</form>
这是createUser.php文件。
include 'loginManager.php';
function enterUserData($Username,$Password,$Firstname,$Lastname,$DateJoined,$FavoriteColor,$FavoriteMovie,$FavoriteAnimal,$FavoriteVideogame,$MaritalStatus){
include 'connect.php';
$query = $loginSystem->prepare("INSERT INTO users (Username,Password,FirstName,LastName,DateJoined,FavoriteColor,FavoriteMovie,FavoriteAnimal,FavoriteVideogame,MaritalStatus) VALUES ($Username,$Password,$Firstname,$Lastname,$DateJoined,$FavoriteColor,$FavoriteMovie,$FavoriteAnimal,$FavoriteVideogame,$MaritalStatus)");
$query->execute();
}
$username = $_POST["Username"];
$password = $_POST["Password"];
$firstname = $_POST["Firstname"];
$lastname = $_POST["Lastname"];
$dateJoined = strftime('%D');
$favoriteColor = $_POST["Favorite Color"];
$favoriteMovie = $_POST["Favorite Movie"];
$favoriteAnimal = $_POST["Favorite Animal"];
$favoriteVideogame = $_POST["Favorite Videogame"];
$maritalStatus = $_POST["Marital Status"];
enterUserData($username,$password,$firstname,$lastname,$dateJoined,$favoriteColor,$favoriteMovie,$favoriteAnimal,$favoriteVideogame,$maritalStatus);
这是connect.php文件。
$host = "localhost";
$username = "root";
$password = "";
$dbname = "login_system";
$dsn ="mysql:host=$host;dbname=$dbname;";
$loginSystem = new PDO($dsn,$username,$password);
答案 0 :(得分:0)
您需要使用'
。这是您的新enterUserData
功能:
function enterUserData($Username,$Password,$Firstname,$Lastname,$DateJoined,$FavoriteColor,$FavoriteMovie,$FavoriteAnimal,$FavoriteVideogame,$MaritalStatus){
include 'connect.php';
$query = $loginSystem->prepare("INSERT INTO users (Username,Password,FirstName,LastName,DateJoined,FavoriteColor,FavoriteMovie,FavoriteAnimal,FavoriteVideogame,MaritalStatus) VALUES ('$Username','$Password','$Firstname','$Lastname','$DateJoined','$FavoriteColor','$FavoriteMovie','$FavoriteAnimal','$FavoriteVideogame','$MaritalStatus')");
$query->execute();
}
答案 1 :(得分:0)
请尝试以下代码: -
$dateJoined = strftime('%D'); to $dateJoined = date('Y-m-d');
并替换此行: -
$query = $loginSystem->prepare("INSERT INTO users (Username,Password,FirstName,LastName,DateJoined,FavoriteColor,FavoriteMovie,FavoriteAnimal,FavoriteVideogame,MaritalStatus) VALUES ('$Username','$Password','$Firstname','$Lastname','$DateJoined','$FavoriteColor','$FavoriteMovie','$FavoriteAnimal','$FavoriteVideogame','$MaritalStatus')");
保存数据时,日期格式可能会抛出错误。
答案 2 :(得分:0)
我认为准备好的陈述应该更像这样:
$query = $loginSystem->prepare("INSERT INTO users (Username,Password,FirstName,LastName,DateJoined,FavoriteColor,FavoriteMovie,FavoriteAnimal,FavoriteVideogame,MaritalStatus) VALUES (?,?,?,?,?,?,?,?,?,?)");
$query->bind_param("ssssssssss", $Username,$Password,$Firstname,$Lastname,$DateJoined,$FavoriteColor,$FavoriteMovie,$FavoriteAnimal,$FavoriteVideogame,$MaritalStatus);
$query->execute();
答案 3 :(得分:0)
所以我想出了问题所在。一些文本字段的名称在单词之间有一个空格。在$ _POST中收集时,PHP将用下划线替换空格。在第16-20行,我使用了一个空格,因为这就是用HTML拼写名称的方式,而不是下划线。正如其他人指出的那样,我忘了在准备好的声明中加上变量名。感谢所有帮助过的人!