2

How do insert multiple rows into a mysql table, with one column remaining constant, and the other as an array.

//inserted profession into professions table, return id
$new_profession_id = mysql_insert_id(); 

$qualification_array = array();

foreach ($_POST['qualification'] as $qual){
    array_push($qualification_array, $qual);
}

$query = "???

now how would I insert this into the profession_has_qualification table? its got me stumped...

1 Answer 1

2

You can do like this:

$new_profession_id = mysql_insert_id(); 

foreach ($_POST['qualification'] as $qual){
   mysql_query("insert into TableName set pid = $new_profession_id, qualification = '" . mysql_real_escape_string($qual) . "'");
}
Sign up to request clarification or add additional context in comments.

2 Comments

great thanks. I was wondering though - is it OK to make many queries like this? or should it be put into one?
@davivid: Query will run multiple times no matter what after all you want to add more than one record which is not possible with single query :)

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.