关于PHP和mySQL,我是一个完整的初学者。我目前正在一个简单的项目中,用户将转到用HTML创建的表单并填写一些信息,并且当他们点击提交时,理论上应该将数据保存到数据库中。我创建了该表格,每次填写该表格时,都会说该帐户已创建,并且没有显示错误。但是,该信息未插入PHPadmin上的数据库中。我一直在尝试通过查看youtube视频和其他stackoverflow论坛来解决此问题,但仍未插入。如果有人能指出我的代码出了什么问题,那将是很好的。我将在下面的代码中附上我认为与之相关的内容。
AsyncPipe
Promise
Here is the main php file
<?php
$email = "";
$password = "";
$errors = array();
// connect to the database
$db = new mysqli('localhost', 'root', '', 'bikeshop');
// check the connection
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
//if the register button is clicked
if (isset($_POST['register'])) {
$firstname = mysqli_real_escape_string($db, $_POST['firstname']);
$lastname= mysqli_real_escape_string($db, $_POST['lastname']);
$streetaddress = mysqli_real_escape_string($db, $_POST['streetaddress']);
$city = mysqli_real_escape_string($db, $_POST['city']);
$state = mysqli_real_escape_string($db, $_POST['state']);
$zipcode = mysqli_real_escape_string($db, $_POST['zipcode']);
$phonenumber = mysqli_real_escape_string($db, $_POST['phonenumber']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password = mysqli_real_escape_string($db, $_POST['password']);
// ensure that form fields are filled properly
if (empty($email)) {
array_push($errors, "Email is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
// if there are no erros, save user to database
if (count($errors) ==0) {
$password = md5($password); //password encryption
$sql = "INSERT INTO customer (FirstName, LastName, Address, City, State, Zip code, PhoneNumber, Email, Password) VALUES ('$firstname','$lastname','$streetaddress','$city','$state','$zipcode','$phonenumber','$email','$password')";
if(!mysqli_query($db, $sql))
{
echo 'Account Created';
}
else
{
echo 'Account creation failed';
}
}
}
?>
答案 0 :(得分:1)
if(count($errors) == 0)
应使用if(empty($errors))
进行更改
那是因为您要检查数组是否等于零。数组可以为空,而变量可以为null,零,未定义(尽管它们并不相同)。
答案 1 :(得分:0)
尝试一下
if (isset($_POST['register'])) {
$firstname = $_POST['firstname'];
$lastname= $_POST['lastname'];
$streetaddress = $_POST['streetaddress'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$phonenumber = $_POST['phonenumber'];
$email = $_POST['email'];
$password = $_POST['password'];