簡體   English   中英

無法將數據插入MySQL數據庫

[英]Cannot insert data into MySQL Database

所以我在這里遇到了一些問題。 我正在嘗試做一些非常基本的事情,即通過Apache localhost網站將數據存儲到MySQL數據庫中。 在我的網站上發送數據后,它不會存儲到數據庫中。 我會先發布我的代碼,然后再給你一些信息:

標題中的代碼:

<?php
$link=mysqli_connect("localhost","root","mypassword","database") or die("Couldn't connect to DB");
?>

碼:

<?php include ( "./inc/header.inc.php" ); ?>
<?php
$reg = @$_POST['reg'];
// declaring variables to prevent errors
$fn = ""; // First Name
$ln = ""; // Last Name
$un = ""; // Username
$em = ""; // Email
$emc = ""; // Emailcheck
$pswd = ""; // Password
$pswdc = ""; // Passwordcheck
$d = ""; // Signup Date
$u_check = ""; // Check if username exists
// registration form
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$emc = strip_tags(@$_POST['emailcheck']);
$pswd = strip_tags(@$_POST['password']);
$pswdc = strip_tags(@$_POST['passwordcheck']);
date_default_timezone_set('Europe/Berlin');
$d = date("Y-M-D"); // Year - Month - Day

if ($reg) {
    // Check if emails are equal
    if ($em==$emc) {
        // Check if user already exists
        $u_check = mysqli_query($link, "SELECT username FROM users WHERE username= '$un'");
        // Count the amount of rows where username = $un
        $check = mysqli_num_rows($u_check);
        if ($check == 0) {
            // Check all of the fields have been filled in
            if ($fn&&$ln&&$un&&$em&&$emc&&$pswd&&$pswdc) {
                // Check that passwords match
                if ($pswd==$pswdc) {
                    // Check the max length of username/first name/last name does not exeed 25 characters
                    if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
                        echo "The maximum limit for username/first name/last name is 25 characters!";
                    } else {
                        // Check the max length of password does not exeed 25 characters and is not less than 5 characters
                        if (strlen($pswd)>30||strlen($pswd)<5) {
                            echo "Your password must be between 5 and 30 characters long. So should your ######!";
                        } else {
                            // encrypt password and passwordcheck using md5 before sending to database
                            $pswd = md5($pswd);
                            $pswdc = md5($pswdc);
                            $query = mysqli_query($link, "INSERT INTO users (id, username, first_name, last_name, email, password, sign_up_date, activated) VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')");
                            die("<h2>Welcome to #######</h2>Login to your account to get ######!");
                        }
                    }
                } else {
                    echo "Your passwords don't match.";
                }
            } else {
                echo "Please fill in all of the fields.";
            }
        } else {
            echo "This Username already exists.";
        }
    } else {
        echo "Your Emails don't match.";
    }
}
?>
        <div id="wrapper2">
            <table>
                <tr>
                    <td width="60%" valign="top">
                        <h2>Join ####### today!</h2>
                    </td>
                    <td width="40%" valign="top">
                        <h2>Sign up below.</h2>
                        <form action="#" method="POST">
                            <input type="text" name="fname" size="25" placeholder="First Name" /><br /><br />
                            <input type="text" name="lname" size="25" placeholder="Last Name" /><br /><br />
                            <input type="text" name="username" size="25" placeholder="User Name" /><br /><br />
                            <input type="text" name="email" size="25" placeholder="E-Mail" /><br /><br />
                            <input type="text" name="emailcheck" size="25" placeholder="E-Mail Check" /><br /><br />
                            <input type="text" name="password" size="25" placeholder="Password" /><br /><br />
                            <input type="text" name="passwordcheck" size="25" placeholder="Password Check" /><br /><br />
                            <input type="submit" name="reg" value="Sign Up!">
                        </form>
                    </td>
                </tr>
            </table>
<?php include ( "./inc/footer.inc.php" ); ?>

因此,與數據庫的連接正常。 我已經檢查過了。 數據正確存儲在變量中。 我在數據庫中通過phpMyAdmin手動插入一個'Test'用戶名,如果我在我的網站上輸入相同的用戶名,代碼就會正確地從數據庫中提取用戶名並將其與輸入進行比較(結果是“用戶名已存在”) 。 就像我已經說過的那樣,當我正確地用數據填充表單時,代碼終止但數據不會存儲到數據庫中。

這里有一些版本信息:Apache / 2.4.16(Unix)PHP / 5.5.29

本指南基本上用於設置Apache,PHP,MySQL,phpMyAdmin: Mac OSX指南

我通過die(mysqli_error($link));檢查錯誤die(mysqli_error($link)); ,得到了這個回應:

不正確的整數值:''對於第1行的列'id' - >將參數''更改為NULL。

第二次錯誤檢查給了我這個:

日期值不正確:第1行的'sign_up_date'列的'2015-Nov-Wed'

NOW()方法就行了。 並且感謝指出日期錯誤:應該是$ d = date(“Ymd”);

嘗試這個:

INSERT INTO users (username, first_name, last_name, email, password, sign_up_date, activated) VALUES ('$un','$fn','$ln','$em','$pswd','$d','0')`

如果您的id列是自動增量,則您無需自行填寫。

對於您的日期,您可以查看此鏈接以獲取日期參考。 date結構化列需要YY-MM-DD格式,您可以通過以下方式獲取:

$d = date("Y-m-d");

您系統中的用戶是否可以上傳視頻? :P

  1. 開始將以下內容添加到php文件的頂部(打開后立即打開[

    使用error_reporting(E_ALL); ini_set('display_errors','on');

這將顯示您在頁面上有什么錯誤。

  1. 然后使用檢查您的查詢錯誤

     $query = mysqli_query($link, "INSERT INTO users (id, username, first_name, last_name, email, password, sign_up_date, activated) VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')") or die(mysqli_error($link); 

第二個將打印mysql錯誤,然后您可以使用它來解決您的問題。

您可以隨時使用上述內容來查找問題,您的問題可能如下

在您的查詢中,您為id發送空值。

更改

"INSERT INTO users (id, username, first_name, last_name, email, password, sign_up_date, activated) VALUES ('','$un','$fn','$ln','$em','$pswd','$d','0')"

"INSERT INTO users (username, first_name, last_name, email, password, sign_up_date, activated) VALUES ('$un','$fn','$ln','$em','$pswd','$d','0')"

暫無
暫無

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

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