简体   繁体   English

更改表SQL PHP

[英]Alter table SQL PHP

I'm just trying to insert two variables into a table to create two new columns. 我只是想在表中插入两个变量以创建两个新列。 I can get one variable inserting fine and creating a new column, but struggling to get the right syntax for the second one. 我可以让一个变量很好地插入并创建一个新列,但是努力为第二个变量获取正确的语法。 This currently works: 目前适用:

mysqli_query($link,"ALTER TABLE importantdetails.extracols ADD  `" . $newCol1 . "` VARCHAR(100)");

However I can't get $newCol2 to insert. 但是我无法插入$newCol2 I've tried everything I can think of. 我已经尝试了所有我能想到的。

Any ideas? 有任何想法吗?

Thanks 谢谢

To add multiple columns to an existing table, the SQL ALTER TABLE syntax is: 要将多个列添加到现有表中,SQL ALTER TABLE语法为:

ALTER TABLE table_name
  ADD (column_1 column-definition,
    column_2 column-definition,
    ...
    column_n column_definition);

 Ex. ALTER TABLE supplier
    ADD (supplier_name varchar(50),
       city varchar(45));

如果您有$link无需添加数据库名称importantdetails尝试

mysqli_query($link,"ALTER TABLE extracols ADD (".$newCol1." VARCHAR(100),  ".$newCol2." VARCHAR(100))");

Make sure $newCol2 is not a MySQL reserved word and that you set its type correctly. 确保$newCol2不是MySQL保留字 ,并且您正确设置了它的类型。 If it's also a VARCHAR(100), the only problem I can think of is that $newCol2 is a reserved word. 如果它也是VARCHAR(100),我唯一想到的问题是$newCol2是保留字。

If you're trying to add multiple columns in one ALTER command, you could do it like this: 如果您试图在一个ALTER命令中添加多列,则可以这样做:

mysqli_query($link,"ALTER TABLE importantdetails.extracols ADD ('$col1' col1_def, '$col2' col2_def));

The syntax should be 语法应为

mysqli_query($link,"ALTER TABLE importantdetails.extracols ADD  ('".$newCol1."' VARCHAR(100),  '".$newCol2."' VARCHAR(100))");

Assuming $newCol2 contains the string name of your new column and you want this column to be of type varchar(100) 假设$ newCol2包含新列的字符串名称,并且您希望此列的类型为varchar(100)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM