简体   繁体   中英

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.

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)".

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. 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

depending on the API used

If PDO, add $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); right after the connection is opened. $pdo being the connection variable used.

If not PDO, use the equivalent in mysqli_ or mysql_ it's unclear which MySQL API you are using.

or die(mysqli_error($con)) to mysqli_query()
or die(mysql_error()) to mysql_query()

Either way use:

error_reporting(E_ALL);
ini_set('display_errors', 1);

at the top of your files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM