首页 > 解决方案 > 将数据保存到数据库时出现问题,PHP 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");
    }   
}
?>

标签: phphtmlmysql

解决方案


推荐阅读