PHP未将数据插入数据库

时间:2018-12-02 06:38:26

标签: php html mysql forms

我正在尝试将数据插入phpmyadmin数据库。 它没有给出任何错误,但是当我查看php我的管理员时,未插入任何数据:

代码在这里:

    <?php //get the data and assign to variables
     if (isset($_POST['submit'])) {

        $username = $_POST["Username"];
        $email = $_POST["Email"];
        $password = $_POST["Password"];

        $gender = $_POST["Gender"];

        $bestKnowLanguage = $_POST["Languages"];

        $DOB = $_POST["DOB"];
        $DOB_Splitted = preg_split("/-/", $DOB);

        $DOB_Year = $DOB_Splitted[0];
        $DOB_Month = $DOB_Splitted[1];
        $DOB_Date = $DOB_Splitted[2];

        $DOB_Formatted = date('d-m-y',strtotime($_POST['DOB']));

        echo "$username<br>";
        echo "$email<br>";
        echo "$password<br>";
        echo "$gender<br>";
        echo "Best Known Language ".$bestKnowLanguage.'<br>';
        echo "$DOB<br>";

            if (!empty($_POST['languages'])) {
               foreach ($_POST['languages'] as $value) {
               echo $value.' ';
               }
             }
       }
    ?>

数据库连接:

 <?php
    $conn = mysqli_connect("localhost", "root", "", "coders");
    $sql = "INSERT INTO coder_system(email,username,password,gender,DOB,known_language,best_known_language) VALUES ('$email','$username','$password','$gender','$DOB_Formatted','$bestKnowLanguage','$bestKnowLanguage')";
        if(mysqli_query($conn,$sql)) {
            echo "Successfull";
        }

    mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:0)

可能是因为它阻塞了您的DOB字段,mysql日期的格式应为YYYY-MM-DD,而您使用的是d-m-y

您是否尝试按照建议将$DOB_Formatted = date('d-m-y',strtotime($_POST['DOB']));更改为$DOB_Formatted = date('Y-m-d',strtotime($_POST['DOB']));