我想在名为" preise"的talbe中创建新数据。如果我这样做,我想在名为" tische"的表中添加一列。我在" preise"中创建的数据的标题 我希望你能理解我的意思(我的英语不是很好") 这是我的PHP:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$name = $_POST['name'];
$ebene = $_POST['ebene'];
$preis = $_POST['preis'];
//Creating an sql query
$sql = "INSERT INTO preise (name,ebene,preis) VALUES ('$name','$ebene','$preis')";
$add = "ALTER TABLE tische ADD COLUMN $name INT(5) NOT NULL AFTER id";
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
if(mysqli_query($con,$sql,$add)){
echo 'Element erfolgreich hinzugefügt';
}else{
echo 'Konnte Element nicht hinzufügen';
}
//Closing the database
mysqli_close($con);
}
如果我在$ add ...处删除该行,那么脚本将按原样执行$ sql。但是如何执行$ add查询呢?
答案 0 :(得分:3)
不同的查询应该单独执行。
一个简单的例子:
//Creating an sql query
$insert_query = "INSERT INTO preise (name,ebene,preis) VALUES ('$name','$ebene','$preis')";
$alter_query = "ALTER TABLE tische ADD COLUMN $name INT(5) NOT NULL AFTER id";
//Importing our db connection script
require_once('dbConnect.php');
//Executing query to database
// mysqli_query takes TWO or THREE arguments only
// and third argument is an INT value
// so passing another query as third argument is USELESS
if (mysqli_query($con, $insert_query)){
// if first query is okay
// we can run second one
$r = mysqli_query($con, $alter_query);
if ($r) {
// both queries are OK
echo 'Element erfolgreich hinzugefügt';
} else {
echo 'Some error message here';
}
} else {
echo 'Konnte Element nicht hinzufügen';
}
//Closing the database
mysqli_close($con);
进一步阅读: