gpt4 book ai didi

php - 使用 PHP foreach 循环创建 MySQL 表 - 语法错误

转载 作者:行者123 更新时间:2023-11-29 06:40:54 26 4
gpt4 key购买 nike

构建我自己的电子商务网站,并在后端创建了一个表单,管理员可以在其中创建某些产品过滤器组(例如颜色)。

当我尝试将新表插入数据库时​​,我使用 foreach 循环来确保没有插入空白条目(并防止这些空白条目导致问题)。

但是我遇到了语法问题,并且我对此进行了充分研究,但没有取得任何成功。

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 'entry_date DEFAULT CURRENT_TIMESTAMP)' at line 1

我的代码:

// GROUP NAME PROVIDED

$group_name = $_POST['g_name'];

// FILTER OPTIONS PROVIDED

$filter_1 = $_POST['filter_1'];
$filter_2 = $_POST['filter_2'];
$filter_3 = $_POST['filter_3'];
$filter_4 = $_POST['filter_4'];
$filter_5 = $_POST['filter_5'];
$filter_6 = $_POST['filter_6'];
$filter_7 = $_POST['filter_7'];
$filter_8 = $_POST['filter_8'];
$filter_9 = $_POST['filter_9'];
$filter_10 = $_POST['filter_10'];
$filter_11 = $_POST['filter_11'];

include '../connection.php';

$query = "CREATE TABLE $group_name (
entry_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
product_id VARCHAR(255) NOT NULL,";

$newarray = array($filter_1,$filter_2,$filter_3,$filter_4,$filter_5,$filter_6,$filter_7,$filter_8,$filter_9,$filter_10,$filter_11);

// For each value that is not NULL
foreach ($newarray as $key => $value) {
if (is_null($value) === false) {
$new_array[$key] = $value;
echo $value;

$query = "$value VARCHAR(255) NOT NULL,";

}

}

$query= "entry_date DEFAULT CURRENT_TIMESTAMP)";

// Checking if query was successful or not

if (mysqli_query($connection, $query )) {
echo "Table created successfully";
} else {
echo "Error creating table: " . mysqli_error($connection);
}

mysqli_close($connection);

我尝试了时间戳插入的不同变体,但没有成功。

值得注意的是,我还拆分了查询,以便可以包含 foreach 循环。

如有任何帮助,我们将不胜感激!

P.S 我知道这段代码对 SQL 注入(inject)是开放的,我希望在保证其安全之前让它发挥作用!

谢谢。斯坦.

工作代码:

// GROUP NAME PROVIDED

$group_name = $_POST['g_name'];

// FILTER OPTIONS PROVIDED

$filter_1 = $_POST['filter_1'];
$filter_2 = $_POST['filter_2'];
$filter_3 = $_POST['filter_3'];
$filter_4 = $_POST['filter_4'];
$filter_5 = $_POST['filter_5'];
$filter_6 = $_POST['filter_6'];
$filter_7 = $_POST['filter_7'];
$filter_8 = $_POST['filter_8'];
$filter_9 = $_POST['filter_9'];
$filter_10 = $_POST['filter_10'];
$filter_11 = $_POST['filter_11'];

include '../connection.php';

$query = "CREATE TABLE $group_name (
entry_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
product_id VARCHAR(255) NOT NULL,";

$newarray = array($filter_1,$filter_2,$filter_3,$filter_4,$filter_5,$filter_6,$filter_7,$filter_8,$filter_9,$filter_10,$filter_11);

$filtered_array = array_filter($newarray);

// For each value that is not NULL
foreach ($filtered_array as $key => $value) {

$filtered_array[$key] = $value;
echo $value;

$query .= "$value VARCHAR(255) NOT NULL,";


}

$query .= "entry_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP)";

echo "<br><br>".$query;

// Checking if query was successful or not

if (mysqli_query($connection, $query )) {
echo "Table created successfully";
} else {
echo "Error creating table: " . mysqli_error($connection);
}

mysqli_close($connection);

最佳答案

您缺少类型:

entry_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP

entry_date DATETIME DEFAULT CURRENT_TIMESTAMP

关于php - 使用 PHP foreach 循环创建 MySQL 表 - 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51656723/

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