[英]Creating MySQL table with PHP foreach loop - error with syntax
Building my own ecommerce website and within the back-end ive created a form where admins can create certain product filter groups (colours for example). 建立我自己的电子商务网站并在后端ive中创建一个表单,管理员可以在其中创建某些产品过滤器组(例如颜色)。
When im trying to insert the new table into the database im using a foreach loop to make sure no blank entries are inserted (and to prevent these blank entries causing issues). 当我尝试使用foreach循环将新表插入数据库时,im确保未插入任何空白条目(并防止这些空白条目引起问题)。
However I have encountered a syntax issue and i've researched this fully without any success. 但是,我遇到了语法问题,对此进行了充分的研究,但未取得任何成功。
You have an error in your SQL syntax;
您的SQL语法有误; 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
在第1行的“ entry_date DEFAULT CURRENT_TIMESTAMP)”附近检查与MySQL服务器版本相对应的手册以使用正确的语法
My code: 我的代码:
// 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);
I've tried different variations to the timestamp insertion but had no luck. 我尝试过对时间戳插入进行不同的更改,但是没有运气。
It's worth noting that I have also split my query up so that the foreach loop can be included. 值得注意的是,我还对查询进行了拆分,以便可以包含foreach循环。
Any help would be appreciated! 任何帮助,将不胜感激!
PS I know this code is open to SQL injection, i want to get it functioning before making it secure! PS:我知道这段代码可以进行SQL注入,因此我想在使其安全之前使其正常运行!
Thanks. 谢谢。 Stan.
斯坦
Working code: 工作代码:
// 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);
you are missing the type: 您缺少类型:
entry_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
or 要么
entry_date DATETIME DEFAULT CURRENT_TIMESTAMP
you overwrite your variable $query 您覆盖变量$ query
use =. 使用=。 if you want to accumulate data in a string
如果要累积字符串中的数据
$query =. "entry_date DEFAULT CURRENT_TIMESTAMP)";
buy the way it's not a really good style to store it like this 购买这种方式并不是很好的存储方式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.