是否可以将alter table add列用于两个表?

时间:2017-03-09 11:03:58

标签: php mysql

这是我对第一张表的查询。

date    type    value
01-01-2017  A   1231
02-01-2017  A   51
03-01-2017  B   124
04-01-2017  C   2134
05-01-2017  A   214
06-01-2017  B   214
07-01-2017  B   123
08-01-2017  B   123
09-01-2017  C   124
10-01-2017  A   1
11-01-2017  A   24

这是我对第二张表的查询。

$category=$_POST['category']; 
$sql = "ALTER TABLE men ADD $category INT BEFORE TOTAL";

第一个查询仅有效。

3 个答案:

答案 0 :(得分:0)

Id DDL ..你不能将var用于表或列名..但是你使用php做fatc ...你可以使用字符串连接..(确保正确的内容检查,以避免sql注入

 $sql = "ALTER TABLE men
  ADD COLUMN  " . $category  . " INT BEFORE TOTAL";

并且是的,你可以使用alter table两次或更多次..但是在separeated ..query或usinng multiquery函数..

答案 1 :(得分:0)

请试试下面的代码。在这里,我修改了你的代码。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorCodeStyle>
<!-- Written by QtCreator 4.2.1, 2017-03-09T12:29:05. -->
<qtcreator>
 <data>
  <variable>CodeStyleData</variable>
  <valuemap type="QVariantMap">
   <value type="bool" key="AlignAssignments">false</value>
   <value type="bool" key="AutoSpacesForTabs">false</value>
   <value type="bool" key="BindStarToIdentifier">false</value>
   <value type="bool" key="BindStarToLeftSpecifier">true</value>
   <value type="bool" key="BindStarToRightSpecifier">false</value>
   <value type="bool" key="BindStarToTypeName">true</value>
   <value type="bool" key="ExtraPaddingForConditionsIfConfusingAlign">true</value>
   <value type="bool" key="IndentAccessSpecifiers">false</value>
   <value type="bool" key="IndentBlockBody">true</value>
   <value type="bool" key="IndentBlockBraces">false</value>
   <value type="bool" key="IndentBlocksRelativeToSwitchLabels">true</value>
   <value type="bool" key="IndentClassBraces">false</value>
   <value type="bool" key="IndentControlFlowRelativeToSwitchLabels">true</value>
   <value type="bool" key="IndentDeclarationsRelativeToAccessSpecifiers">true</value>
   <value type="bool" key="IndentEnumBraces">false</value>
   <value type="bool" key="IndentFunctionBody">true</value>
   <value type="bool" key="IndentFunctionBraces">false</value>
   <value type="bool" key="IndentNamespaceBody">false</value>
   <value type="bool" key="IndentNamespaceBraces">false</value>
   <value type="int" key="IndentSize">2</value>
   <value type="bool" key="IndentStatementsRelativeToSwitchLabels">true</value>
   <value type="bool" key="IndentSwitchLabels">true</value>
   <value type="int" key="PaddingMode">2</value>
   <value type="bool" key="ShortGetterName">true</value>
   <value type="bool" key="SpacesForTabs">false</value>
   <value type="int" key="TabSize">2</value>
  </valuemap>
 </data>
 <data>
  <variable>DisplayName</variable>
  <value type="QString">Google</value>
 </data>
</qtcreator>

答案 2 :(得分:-1)

<?php
$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "pageant"; //database 

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname); //open ng connection 
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(isset($_POST['submit'])){
    $category=$_POST['category'];

 $sql = "ALTER TABLE men
  ADD " . $category  . " INT BEFORE TOTAL";

if ($conn->query($sql) === TRUE) {
    header('Location:welcome_admin.php');
     echo "New record created successfully";
    exit();


}else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
<?php
$servername = "localhost:3307";
$username = "root";
$password = "";
$dbname = "pageant"; //database 

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname); //open ng connection 
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(isset($_POST['submit'])){
    $category=$_POST['category'];

 $sql = "ALTER TABLE women
  ADD " . $category  . " INT BEFORE TOTAL";

if ($conn->query($sql) === TRUE) {
    header('Location:welcome_admin.php');
     echo "New record created successfully";
    exit();


}else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>

This is my code.