提问人:Skidoods 提问时间:12/6/2019 更新时间:12/6/2019 访问量:38
插入mysqli的查询似乎不起作用,我无法弄清楚原因
Insert query for mysqli seems to not be working and I can't figure out why
问:
对于学校的一个项目,我必须为一家公司创建一个网站。当我偶然发现这个问题时,我正在研究寄存器系统。由于某种原因,我的查询无法在我的数据库上运行。我尝试在phpmyadmin中输入查询,效果很好,但是如果我想在php中使用查询,它不起作用。我使用的数据库是预制的,它不是自动递增的,所以当我想插入新数据时,我必须添加所有行的所有值。
这是我正在使用的数据库的连接 我使用标题消息来指示注册是否成功:
function dbConnectionRoot () {
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "wideworldimporters";
$connection = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
return $connection;
}
这是我正在使用的插入脚本:
$sql = "INSERT INTO customers (
CustomerID, CustomerName, CustomerCategoryID,
PhoneNumber, FaxNumber, EmailAddress,
HashedPassword, BillToCustomerID, BuyingGroupID,
PrimaryContactPersonId, AlternateContactPersonID, DeliveryMethodID,
DeliveryCityID, PostalCityId, AccountOpenedDate,
StandardDiscountPercentage, IsStatementSent, IsOnCreditHold,
PaymentDays, WebsiteURL, DeliveryAddressLine1,
DeliveryPostalCode, PostalAddressLine1, PostalPostalCode,
LastEditedBy, ValidFrom, ValidTo)
VALUE (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";
$statement = dbConnectionRoot()->prepare($sql);
mysqli_stmt_init(dbConnectionRoot());
if (dbConnectionRoot()->connect_errno) {
header("Location: signup:php?error=connection");
exit();
} elseif(!dbConnectionRoot()->query($sql)) {
header("Location: signup.php?error=queryerror");
exit();
} else {
//First we has the password. This is because if a hacker were to hack into the database, it could only see the hashed passwords.
// We use this hashing method(bcrypt) because it is always updated when there is a security breach.
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$maxCustomerID++;
//Again, we are making a prepared statement for security. This time we use 27 s'es because we want 27 different variables
$statement->bind_param("sssssssssssssssssssssssssss",
$maxCustomerID, $fullName, $customerCategory, $phoneNumber, $faxNumber, $email, $hashedPassword, $billToCustomerID,
$buyingGroupID, $primaryContactPersonId, $alternateContactPersonID, $deliveryMethodID, $deliveryCityID, $postalCityId, $accountOpenedDate,
$standardDiscountPercentage, $isStatementsent, $isOnCreditHold, $paymentDays, $websiteURL, $deliveryAddressLine1, $deliveryPostalCode,
$postalAddressLine1, $postalPostalCode, $lastEditedBy, $validFrom, $validTo);
$statement->execute();
return($statement);
//A message that the signup was succesfull
header("Location: signup.php?signup=success");
exit();
这是注册表单片段
<form action="signupfunctions.php" method="post">
<label>Volledige naam: </label><input type="text" name="FullName"><br>
<label>E-mail adres: </label><input type="text" name="EmailAddress"><br>
<label>Wachtwoord: </label><input type="password" name="Password"><br>
<label>Herhaal uw wachtwoord: </label><input type="password" name="PasswordRepeat"><br>
<label>Telefoon nummer: </label><input type="text" name="PhoneNumber"><br>
<label>Fax nummer: </label><input type="text" name="FaxNumber"><br>
<button type="submit" name="signupbutton" >Registreer</button>
</form>
答:
0赞
lafor
12/6/2019
#1
您的 SQL 语句中存在语法错误。它应该是:,而不是 。INSERT INTO ... VALUES
VALUE
评论
0赞
Patrick Q
12/6/2019
当然,在“我尝试在phpmyadmin中输入查询,效果很好”的说法中打了一个洞
0赞
Skidoods
12/6/2019
我都试过了,而且都很好用VALUE
VALUES
评论
dbConnectionRoot()
mysqli_stmt_init()
mysqli_stmt_init()
elseif(!dbConnectionRoot()->query($sql))
signup.php?error=queryerror
prepare()
mysqli_stmt_init()