0

How can insert a array into my MySQL database?

I have managed to insert some of this data into the database already.

Below is part of my code:

$depart=serialize($_POST['departure']); 
$sql="INSERT INTO bookings VALUES('$depart');

I am trying to insert

[departure] => Array ( [0] => 30 [1] => 05 [2] => 2011 [3] => 17 [4] => 41 )

into the database field 'depart'

your help will be much appreciated.

4
  • I forgot to mention the data I am trying to insert is from a form, the values in the above array was received from var_dump after form was submitted. Commented May 31, 2011 at 9:52
  • You might want to learn about Little Bobby Tables. Commented May 31, 2011 at 9:58
  • 2
    You'd better to convert the array to date-time and save it in a datetime field. Commented May 31, 2011 at 9:58
  • note you can format lines as code by indenting them four spaces. The "{}" button in the editor toolbar does this for you. Someone did it for you this time, but next time try it yourself. Click the orange question mark in the editor toolbar for more information and tips on formatting. Commented May 31, 2011 at 10:02

2 Answers 2

1

Assuming 'depart' is a datetime field:

$timestamp = mktime($depart[3], $depart[4], 0, $depart[2], $depart[1], $depart[0]);

$sql = "insert into bookings(depart) values (from_unixtime($timestamp))";

// Execute the sql as normal
Sign up to request clarification or add additional context in comments.

Comments

1

You should escape your serialized array string.

$depart=mysql_real_secape_string(serialize($_POST['departure']));

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.