[英]Insert data to database from the multi-dimensional array in PHP
I needed to insert some data to mysql database which is stored in a multi-dimensional array. 我需要将一些数据插入存储在多维数组中的mysql数据库中。
I have separate values and ready to put the data to database. 我有单独的值,并准备将数据放入数据库。 It caused the problem that it reported that the part of statement "chccheung.BookingDate(Room,Date,From,To)" is not a correct format/syntax, also it has error nearby "From,To)".
它引起了一个问题,即它报告语句“ chccheung.BookingDate(Room,Date,From,To)”的部分格式/语法不正确,并且在“ From,To)”附近也有错误。
After testing and debugging, I could not find out any solution to solve this problem, can anyone help me, thanks in advance. 经过测试和调试后,我找不到解决该问题的任何解决方案,任何人都可以帮助我,谢谢。
open the database connection
.......
foreach($Booking as $key => $value){
$rmID = $key;
foreach($value as $format => $array){
foreach($array as $date => $detail){
$bookDate = $date;
foreach($detail as $period =>$fromTo){
if($period=="user"){
$user = $fromTo;//$query = "INSERT INTO RmBooking_Applicant(user) VALUES ($fromTo)";
}
if($period=="username"){
$userID = $fromTo;//$query = "INSERT INTO RmBooking_Applicant(username) VALUES ($fromTo)";
}
if($period=="from"){
$fromTime = $fromTo;//$query = "INSERT INTO BookingDate(From) VALUES ($fromTo)";
}
if($period=="to"){
$toTime = $fromTo;//$query = "INSERT INTO BookingDate(To) VALUES ($fromTo)";
}
}
}
}
}
$bookingInformation = "INSERT INTO testingData.BookingDate(Room,Date,From,To) VALUES($rmID,$date,";
$bookingInformation .= implode(',', $fromTo);
$bookingInformation .= ")";
$applicantDetails = "INSERT INTO testingData.RmBooking_Applicant(username,user) VALUES(";
$applicantDetails .= implode(',', $userID);
$applicantDetails .= implode(',', $user);;
$applicantDetails .= ")";
......
close database connection
from
and to
are MySQL reserved keywords. from
和to
是MySQL保留的关键字。 Either wrap them in backticks or use another name for those columns. 可以将它们包装在反引号中,也可以为这些列使用其他名称。
"INSERT INTO testingData.BookingDate(Room,Date,`From`,`To`)...
//$query = "INSERT INTO BookingDate(`From`)
//$query = INSERT INTO BookingDate(`To`)...
Add error reporting to your files http://php.net/manual/en/function.error-reporting.php 将错误报告添加到您的文件中http://php.net/manual/en/function.error-reporting.php
depending on the API used 取决于使用的API
If PDO, add $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
如果是PDO,则添加
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
right after the connection is opened. 在连接打开之后。
$pdo
being the connection variable used. $pdo
是使用的连接变量。
If not PDO, use the equivalent in mysqli_
or mysql_
it's unclear which MySQL API you are using. 如果不是PDO,则在
mysqli_
或mysql_
使用等效项,目前尚不清楚您正在使用哪个MySQL API。
or die(mysqli_error($con))
to mysqli_query()
or die(mysqli_error($con))
到mysqli_query()
or die(mysql_error())
to mysql_query()
or die(mysql_error())
到mysql_query()
Either way use: 两种方式的使用:
error_reporting(E_ALL);
ini_set('display_errors', 1);
at the top of your files. 在文件顶部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.