簡體   English   中英

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>

您的 SQL 語句中有語法錯誤。 它應該是: INSERT INTO ... VALUES ,而不是VALUE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM