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
2010-06-29 06:58:51
whoa, I never know that we can use SET in INSERT command!
silent
2010-06-29 07:05:58
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
2010-06-29 07:15:26