php 如何向MYSQL表添加新列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16113570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add new column to MYSQL table?
提问by Steven Trainor
I am trying to add a new column to my MYSQL table using PHP. I am unsure how to alter my table so that the new column is created. In my assessment table I have:
我正在尝试使用 PHP 向我的 MYSQL 表中添加一个新列。我不确定如何更改我的表以创建新列。在我的评估表中,我有:
assessmentid | q1 | q2 | q3 | q4 | q5
Say I have a page with a textbox and I type q6
in to the textbox and press a button then the table is updated to:
假设我有一个带有文本框的页面,我q6
在文本框中输入内容并按下一个按钮,然后表格将更新为:
assessmentid | q1 | q2 | q3 | q4 | q5 | q6
My code:
我的代码:
<?php
include 'core/init.php';
include 'core/admininit.php';
include 'includes/overall/overall_header.php';
adminprotect_page();
include 'includes/adminmenu.php';
?>
<?php
mysql_query("ALTER TABLE `assessment` ADD newq INT(1) NOT NULL AFTER `q10`");
?>
<h1>Input Career Name</h1>
<form method="post" action="">
Career Name
<input type="text" name="newq" size="20">
<input type="submit"
name="submit" value="Submit">
</body>
</html>
回答by Dima
your table:
你的桌子:
q1 | q2 | q3 | q4 | q5
you can also do
你也可以这样做
ALTER TABLE yourtable ADD q6 VARCHAR( 255 ) after q5
回答by Abdullah Salma
$table = 'your table name';
$column = 'q6'
$add = mysql_query("ALTER TABLE $table ADD $column VARCHAR( 255 ) NOT NULL");
you can change VARCHAR( 255 ) NOT NULL
into what ever datatype
you want.
你可以VARCHAR( 255 ) NOT NULL
变成datatype
你想要的任何东西。
回答by amarnath
You can add a new column at the end of your table
ALTER TABLE assessment ADD q6 VARCHAR( 255 )
Add column to the begining of table
ALTER TABLE assessment ADD q6 VARCHAR( 255 ) FIRST
Add column next to a specified column
ALTER TABLE assessment ADD q6 VARCHAR( 255 ) after q5
您可以在表格末尾添加一个新列
ALTER TABLE assessment ADD q6 VARCHAR( 255 )
将列添加到表的开头
ALTER TABLE assessment ADD q6 VARCHAR( 255 ) FIRST
在指定列旁边添加列
ALTER TABLE assessment ADD q6 VARCHAR( 255 ) after q5
and more options here
以及更多选项在这里
回答by Glitch Desire
Something like:
就像是:
$db = mysqli_connect("localhost", "user", "password", "database");
$name = $db->mysqli_real_escape_string($name);
$query = 'ALTER TABLE assesment ADD ' . $name . ' TINYINT NOT NULL DEFAULT \'0\'';
if($db->query($query)) {
echo "It worked";
}
Haven't tested it but should work.
还没有测试它,但应该工作。
回答by Ryan Epp
Based on your comment it looks like your'e only adding the new column if: mysql_query("SELECT * FROM assessment");
returns false. That's probably not what you wanted. Try removing the '!' on front of $sql in the first 'if' statement. So your code will look like:
根据您的评论,您似乎只在以下情况下添加新列:mysql_query("SELECT * FROM assessment");
返回 false。那可能不是你想要的。尝试删除“!” 在第一个“if”语句中的 $sql 前面。所以你的代码看起来像:
$sql=mysql_query("SELECT * FROM assessment");
if ($sql) {
mysql_query("ALTER TABLE assessment ADD q6 INT(1) NOT NULL AFTER q5");
echo 'Q6 created';
}else...
回答by T.Todua
for WORDPRESS:
对于 WordPress:
global $wpdb;
$your_table = $wpdb->prefix. 'My_Table_Name';
$your_column = 'My_Column_Name';
if (!in_array($your_column, $wpdb->get_col( "DESC " . $your_table, 0 ) )){ $result= $wpdb->query(
"ALTER TABLE $your_table ADD $your_column VARCHAR(100) CHARACTER SET utf8 NOT NULL " //you can add positioning phraze: "AFTER My_another_column"
);}
回答by Shinwar ismail
ALTER TABLE `stor` ADD `buy_price` INT(20) NOT NULL ;
回答by Erik van Velzen
You should look into normalizing your database to avoid creating columns at runtime.
您应该考虑规范化您的数据库以避免在运行时创建列。
Make 3 tables:
制作3张表:
- assessment
- question
- assessment_question (columns assessmentId, questionId)
- 评估
- 题
- 评估问题(列评估 ID,问题 ID)
Put questions and assessments in their respective tables and link them together through assessment_question using foreign keys.
将问题和评估放在各自的表中,并使用外键通过评估问题将它们链接在一起。