1

What I am wondering is how you can use INSERT INTO if you don't know how many fields there will be in advance. My project involves adding columns if required. Therefore my code will look like this:

$query = "INSERT INTO table (could be any amount) VALUES (could be any amount)";

Is it possible to populate the 'could be any amount' parts from arrays? These could be as follows:

$array1 = (fieldname, fieldname, fieldname);
$array2 = (value, value, value);

Thanks in advance for any help you can give.

1 Answer 1

7
$array2 = array_map('mysql_real_escape_string', $array2);
$query = "INSERT INTO table (`".implode("`,`", $array1)."`)"
         ." VALUES ('".implode("','", $array2)."')";

But You should not use such bad practices.

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.