gpt4 book ai didi

php - 如何通过表单输入添加新表和列

转载 作者:可可西里 更新时间:2023-11-01 08:39:50 25 4
gpt4 key购买 nike

我想在我的数据库中添加新表,该表应该有 5 列,但应该通过在网站 form 中填写表格来完成假设我要添加包含五列的 Travel Order 表。现在我的表单中有这段代码,它给了我这个错误:调用未定义的方法 mysqli::mysqli_real_escape_string()

<input type="text" class="form-control1" onKeyPress= "return lettersOnly(event)" name= "categoryname" id="categoryname" placeholder="Category Name..." maxlength="30" reqiured>这是表名。

<input type="text" class="form-control1" onKeyPress= "return lettersOnly(event)" name="firstattrib" id="firstattrib" placeholder="..." maxlength="30">这是专栏之一。 (请不要介意我对变量的命名)

`

$connect = mysqli_connect("localhost","root","","doctrack_db");

// Check connection
if ($connect->connect_error) {
die("Connection failed: " . $connect->connect_error);
}

// sql to create table
$sql = "CREATE TABLE ".$categoryname." (
id INT(6) UNSIGNED AUTO_INCREMENT NOT NULL,
file LONGBLOB(30) NOT NULL,
)";

$firstattrib = $connect->mysqli_real_escape_string($_POST['firstattrib']);

//query to add columns to table
$query = 'ALTER TABLE ' .$categoryname . '
ADD COLUMN '. $firstattrib .' VARCHAR(30) TINYINT NOT NULL DEFAULT \'0\' AFTER file';

if ($connect->query($sql)&$connect->query($query) === TRUE) { echo "alert('类别创建成功!!')"; } 别的 { echo "alert('创建类别时出错!!')". $连接->错误;

$connect->close();?>`

你能告诉我我哪里错了还是整个错了?

最佳答案

试试这个

    <?php
// Create connection
mysql_connect("localhost","root","","test") OR die("Server Connection error");
mysql_select_db("test") OR die("DB error");

$category = "category";

// sql to create table
$sql = "CREATE TABLE ".$category." (
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(mysql_query($sql)){
$sql1 = "INSERT INTO ".$category." (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
mysql_query($sql1);
}
?>

创建表的时间并不重要,重要的是向表中插入数据的时间必须存在。

关于php - 如何通过表单输入添加新表和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37044306/

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