0

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
1
  • Date might be a reserved keyword, try enclosing it within ticks (`..`) Commented Sep 7, 2014 at 15:19

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.