$con=mysql_connect("localhost","root","123456");
if(!$con)
{
die("Could not connect: ".mysql_error());
}
if(!mysql_select_db("my_db",$con))
{
die("database is not exist".mysql_error());
}
else
{
$list=$_POST["newlist"];
$data=$_POST["datatype"];
$sequence=$_POST["Nsequence"];
if(mysql_query("select $list from persons"))
{
echo "The $list is exist";
}
else
{
if(!mysql_query("alter table persons add $list $data $sequence",$con));
{
die("Error: ".mysql_error());
}
}
/* $sql="alter table persons add number int first";
if(!mysql_query($sql,$con))
{
die("Error: ".mysql_error());
}
*/
}
mysql_close($con);
?>
为什么在 if(mysql_query("select $list from persons"))中可以用到变量$list
而在if(!mysql_query("alter table persons add $list $data $sequence",$con));中,却不能用变量。
会提示:Error: You have an error in your SQL syntax; check the manual that corresponds
to your MariaDB server version for the right syntax to use near '' at line 1
而我用/* */里面的语句,可以插入列(number)。
难道在alter table 语句中,是不能带有变量的?那要怎样处理在网页中想要插入列这个问题?
展开