我试图让用户创建一个给定信息的数据库,所以为此,我有以下内容:
<?php
//ini_set('display_errors', 'Off');
if(isset($_POST['siteName'])) {
$DBS = htmlspecialchars($_POST['databaseServer']);
$DBN = htmlspecialchars($_POST['databaseName']);
$DBU = htmlspecialchars($_POST['databaseUser']);
$DBP = htmlspecialchars($_POST['databasePass']);
$SN = htmlspecialchars($_POST['siteName']);
$AU = htmlspecialchars($_POST['adminUser']);
$AP = htmlspecialchars($_POST['adminPass']);
$con = new Mysqli($DBS,$DBU,$DBP,$DBN);
if($con->connect_error) {
echo "Error : ".$con->connect_error;
}
else {
$sql = "CREAT DATABASE '$DBN'";
$sql .= "CREATE TABLE options (sitename VARCHAR(30),adminUser VARCHAR(30),adminPass VARCHAR(30),reg_date TIMESTAMP);";
$sql .= "CREATE TABLE users (ID int(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, userName VARCHAR(30),password VARCHAR(30),reg_date TIMESTAMP);";
$sql .= "INSERT INTO '$DBN' (sitename,adminUser,adminPass) VALUES ($SN,$AU,$AP);";
if($con->multi_query($sql) === true) {
echo " Enjoy .. ! ";
}
else {
echo "Error : ".$sql." : ".$con->error;
}
}
}
?>
但是,当我为数据库键入示例名称时,出现此错误:
Error : Unknown database 'name'
当手动创建数据库并在我的表单中键入它的名称时,请收到以下错误:
Error : CREAT DATABASE 'ex'CREATE TABLE options (sitename
VARCHAR(30),adminUser VARCHAR(30),adminPass VARCHAR(30),reg_date
TIMESTAMP);CREATE TABLE users (ID int(6) UNSIGNED AUTO_INCREMENT PRIMARY
KEY, userName VARCHAR(30),password VARCHAR(30),reg_date
TIMESTAMP);INSERT
INTO 'ex' (sitename,adminUser,adminPass) VALUES (My Phone
Book,admin,102030); : You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'CREAT DATABASE 'ex'CREATE TABLE options (sitename
VARCHAR(30),adminUser VARCHAR(' at line 1
我该如何解决这个问题?
答案 0 :(得分:0)
将行更改为
$sql="CREATE DATABASE ". $DBN.";";
$sql.="CREATE TABLE options (sitename VARCHAR(30),adminUser VARCHAR(30),adminPass VARCHAR(30),reg_date TIMESTAMP);";
$sql.="CREATE TABLE users (ID int(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, userName VARCHAR(30),password VARCHAR(30),reg_date TIMESTAMP);";
$sql.="INSERT INTO options (sitename,adminUser,adminPass) VALUES ($SN,$AU,$AP);";
答案 1 :(得分:0)
如果要使用PHP变量创建数据库,则需要使用USE
语句。之后,您可以在MySQL中创建表。
请尝试以下代码:
<?php
//ini_set('display_errors', 'Off');
if (isset($_POST['siteName'])) {
$DBS = htmlspecialchars($_POST['databaseServer']);
$DBN = htmlspecialchars($_POST['databaseName']);
$DBU = htmlspecialchars($_POST['databaseUser']);
$DBP = htmlspecialchars($_POST['databasePass']);
$SN = htmlspecialchars($_POST['siteName']);
$AU = htmlspecialchars($_POST['adminUser']);
$AP = htmlspecialchars($_POST['adminPass']);
if ($con->connect_error) {
echo "Error : " . $con->connect_error;
} else {
$sql = "CREATE DATABASE $DBN";
$con->query($sql);
$con->query("use `$DBN`");
$sql = "CREATE TABLE options (sitename VARCHAR(30),adminUser VARCHAR(30),
adminPass VARCHAR(30),reg_date TIMESTAMP);";
$con->query($sql);
$sql = "CREATE TABLE users (ID int(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
userName VARCHAR(30),password VARCHAR(30),reg_date TIMESTAMP);"
$con->query($sql);
}
if ($con->query($sql) === true) {
echo " Enjoy .. ! ";
} else {
echo "Error : " . $sql . " : " . $con->error;
}
答案 2 :(得分:0)
最后,我借助亲爱的@MagnusEriksson解决了我的问题:
1.我应该从连接到数据库的代码中删除@DBN
2. sql="CREAT DATABASE '$DBN'";
应更改为sql="CREAT DATABASE $DBN";