gpt4 book ai didi

php - 根据给定的输入创建数据库和表

转载 作者:行者123 更新时间:2023-11-29 21:03:31 25 4
gpt4 key购买 nike

嗨,我想根据给定的输入创建一个数据库和一个表,同时检查数据库和表是否已经存在,当我运行下面的代码时,仅创建数据库而不是表。有人可以帮我吗?

<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;
}
}
?>

最佳答案

您正在尝试通过重新建立连接来选择数据库:

$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);

关于php - 根据给定的输入创建数据库和表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37019163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com