php - 将数据保存到数据库时出现问题,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");
}
}
?>
解决方案
推荐阅读
- javascript - 为 Here Maps API v3.1 使用新的 apikey,我收到错误“apikey invalid. apikey not found”
- javascript - 在闪亮的仪表板中从 R 访问 javascript 对象
- angular - httpClient 获取请求得到 404 未找到响应
- mongodb - 如何在 MongoDB 中合并更新字段
- excel - 从 Q 列获取多个值,添加一个“。” 并在 A 列中搜索(如果存在),然后删除
- mysql - 查找客户多次订购的产品
- c++ - Boost C++ 规范化字符串重音
- python - groupby 并替换某些值
- c# - 如何使用 xunit 测试发布请求
- excel - 使用文本框中的文本自动/合并来自许多 Excel 工作簿的数据