将数据保存到数据库时出现问题,PHP MYSQL

时间:2020-03-04 16:12:46

标签: php html mysql

请原谅我的代码,因为我仍在学习PHP。

我试图将数据保存到数据库,我成功地将变量$ dobday $ dobmonth $ dobyear保存到数据库。此后,我添加了新变量-$ adderssLine $ townCity $ postcode $ country。当我输入详细信息(生日和地址)并更新数据库时,什么都不会保存。当我仅输入地址变量时,没有任何内容保存到数据库中。但是,当我仅输入生日变量时,它将保存到数据库中。 该表具有正确的列名。

感谢您的帮助和时间。

<?php
session_start();

$user=$_SESSION['firstName'];

if (isset($_POST['submit'])){
    $connectDB = mysqli_connect("localhost","root","") 
                    or die("cant connect");  
    //proving the database connection details and saving it as a variable
    mysqli_select_db($connectDB, "registration"); //table name

    // BIRTHDAY

    $updateDBvalues=array();
    $updateArray=array();
    $dobday=$_POST['dobday'];
    $dobmonth=$_POST['dobmonth'];
    $dobyear=$_POST['dobyear'];

    //ADDRESS

    $addressLine=$_POST['addressLine'];
    $townCity=$_POST['townCity'];
    $postcode=$_POST['postcode'];
    $country=$_POST['country'];

    //ADDRESS BELOW

    if(!empty($addressLine))
        $updateArray[]="addressLine='".$addressLine."'";

    if(!empty($townCity))
        $updateArray[]="townCity='".$townCity."'";

    if(!empty($postcode))
        $updateArray[]="postcode='".$postcode."'";

    if(!empty($country))
        $updateArray[]="country='".$country."'";

    //BIRTHDAY BELOW    

    if(!empty($dobday))
        $updateArray[]="dobday='".$dobday."'";

    if(!empty($dobmonth))
        $updateArray[]="dobmonth='".$dobmonth."'";

    if(!empty($dobyear))
        $updateArray[]="dobyear='".$dobyear."'";

    $updateDBvalues=$updateArray;

    $updateDBvalues_imploded=implode(',',$updateDBvalues);

    if(!empty($updateDBvalues)){
        $query="UPDATE users SET $updateDBvalues_imploded WHERE firstName='$user'";

        $connQuery=mysqli_query($connectDB,$query);
        die("Succesfully updated, return to <a href='accountPage.php'>Accounts page</a>");
    }else{
        die ("query did not work");
    }   
}
?>

0 个答案:

没有答案