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

时间:2018-08-02 14:59:27

标签: php mysql foreach

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

当我尝试使用foreach循环将新表插入数据库im时,请确保未插入任何空白条目(并防止这些空白条目引起问题)。

但是,我遇到了语法问题,但我对此进行了充分的研究,没有成功。

  

您的SQL语法有错误;在第1行的“ entry_date DEFAULT CURRENT_TIMESTAMP)”附近检查与MySQL服务器版本相对应的手册以使用正确的语法

我的代码:

// 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注入,我想在使其安全之前使其正常运行!

谢谢。 斯坦

工作代码:

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

2 个答案:

答案 0 :(得分:2)

您缺少类型:

entry_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP

entry_date DATETIME DEFAULT CURRENT_TIMESTAMP

答案 1 :(得分:0)

您覆盖变量$ query

使用=。如果要累积字符串中的数据

$query =. "entry_date DEFAULT CURRENT_TIMESTAMP)";

以这种方式存储不是很好的样式