0

I have problems with the last line of the code:

if(isset($_POST['kolona']))
{
foreach($_POST['kolona'] as $vrednost)
mysql_query("ALTER TABLE tablica ADD $vrednost text NOT NULL");
mysql_query("INSERT INTO tablica ( ".(implode(',',($_POST['kolona']))).") SELECT ".(implode(',',($_POST['kolona'])))." FROM druga");
}            

First query is making columns in table 'tablica' and second query suppose to insert values in that columns from all tables from which are the columns, for now it's just hard coded, it's only from table 'druga', but i don't know how to go through the all tables, not just 'druga'. I tried with a loop and also with implode function but nothing seems to be working. Can anyone help?

4

1 回答 1

0

您可以使用以下功能解决这样的合并问题ON DUPLICATE KEY

INSERT INTO target (id, column1, column2)
  SELECT id, column1, column2 FROM source
  ON DUPLICATE KEY UPDATE column1=VALUES(column1), column2=VALUES(column2)

如果您有一PRIMARY KEY列在源表和目标表之间没有冲突,它就可以很好地工作。

于 2013-10-09T20:07:46.143 回答