tags:

views:

26

answers:

2

The problem is I only know the names of the columns but do not know how many columns in total. There will be other columns.

A: 

I assume you are talking about SQL, not PHP. You can use this syntax:

INSERT INTO table SET columnName = 'foo'

Or:

INSERT INTO table(columnName) VALUES('foo')

If you need to insert multiple values at the same time, you can use these:

INSERT INTO table SET columnName = 'foo', otherColumn = 'bar'

Or:

INSERT INTO table(columnName, otherColumn) VALUES('foo', 'bar')
Tatu Ulmanen
whoa, I never know that we can use SET in INSERT command!
silent
A: 

Or for speed and security:

$DATA = array("column"=>"data", "second"=>"...");

$keys = implode("`, `", array_keys($DATA));
$qm = str_repeat("?, ", count($DATA)-1);
$pdo->prepare("INSERT INTO table (`$keys`) VALUES ($qm ?)")
    ->execute(array_values($DATA));
mario