您好我想从给定的输入创建一个数据库和一个表,同时检查数据库和表是否已经存在,当我运行代码时,只创建数据库而不是表。请有人帮帮我。
<HTML>
<form action="index.php" method="post">
Project No.:<input type="text" name="name"><br>
Question: <input type="text" name="email"><br>
<input type="submit" name="submit" value="Submit">
</form>
</html>
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
If(isset($_POST['submit']))
{
$projno = $_POST['name'];
$question = $_POST['email'];
$_SESSION['proj'] = $projno;
$_SESSION['QA'] = $question;
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$checkdb = "CREATE DATABASE IF NOT EXISTS ".$projno;
if($conn->query($checkdb)===TRUE)
{
$dbname = $projno;
$conn = new mysqli($servername, $username, $password, $dbname);
if ($tableExists = $conn->query("SHOW TABLES LIKE ".$question) > 0){
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
}
}
else{
// Create database
$sql = "CREATE DATABASE " . $projno;
if ($conn->query($sql)===TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
}
}
?>
嗨,这是我的更新代码。谢谢你的帮助它已经有效了。但我有另一个问题有这个错误。 创建错误您的SQL语法有错误;检查与MariaDB服务器版本对应的手册,以便在第1行的“23992550”附近使用正确的语法。它不接受数据库名称的数字输入。希望你能再次帮助我。提前谢谢
<HTML>
<form action="index.php" method="post">
Project No.:<input type="text" name="name"><br>
Question: <input type="text" name="email"><br>
<input type="submit" name="submit" value="Submit">
</form>
</html>
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
If(isset($_POST['submit']))
{
$projno = $_POST['name'];
$question = $_POST['email'];
$_SESSION['proj'] = $projno;
$_SESSION['QA'] = $question;
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$checkdb = "CREATE DATABASE IF NOT EXISTS ".$projno;
if($conn->query($checkdb)===True)
{
$conn->query("USE $projno");
if ($conn->query("DESCRIBE " . $question))
{
//Table exist
header('location:exp.php');
}
else
{
$sql = "CREATE TABLE ".$question."(LIST VARCHAR(150) NOT NULL)";
if ($conn->query($sql) === TRUE)
{
header('location:exp.php');
}
else
{
echo "Error creating table: " . $conn->error;
}
}
}
else
{
echo "Error creating " . $conn->error;
}
}
?>
答案 0 :(得分:0)
您正尝试通过重新建立连接来选择数据库:
$conn = new mysqli($servername, $username, $password);
...
$conn = new mysqli($servername, $username, $password, $dbname);
但它不起作用,因为你实际上得到了相同的变量。您应该重新使用原始连接:
$conn = new mysqli($servername, $username, $password);
...
// After CREATE DATABASE
$conn->query("USE $dbname");
或
// ...
$conn->select_db($dbname);
答案 1 :(得分:0)
使用DESCRIBE
关键字检查表是否存在。
使用以下代码。
if ($conn->query("DESCRIBE " . $question))
{
//Table exist
echo "Table exist";
}
else
{
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE)
{
echo "Table MyGuests created successfully";
}
else
{
echo "Error creating table: " . $conn->error;
}
}