PHP, 创建一个 mySQL table 变量名包含下划线
PHP, Create a mySQL table with a variable name that includes an underscore
我正在尝试创建包含 php 个名称变量的 mySQL table。
$papername=$_POST['txtpname'];
$papertype=$_POST['txtptype'];
$searchForValue = ',';
if (strpos($papertype, $searchForValue) !== false) {
$str_arr = explode (",", $papertype);
foreach ($str_arr as $value) {
$value = trim($value);
$value = preg_replace('/\s+/', '_', $value);
$value = $papername.'_'.$value;
$create=$pdo->prepare("CREATE TABLE ".$value." (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
product_type VARCHAR(30) NOT NULL, gsm VARCHAR(500) NOT NULL) ;");
$create->execute();
}
}
但是当我将两个变量与下划线组合在一起时(行:$value = $papername.'_'.$value;)并将其作为 table 名称,我得到一个 SQL语法错误。
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, product_type VAR...' at line 1 in C:\xampp\htdocs\designshoppos\addpapertype.php:36 Stack trace: #0 C:\xampp\htdocs\designshoppos\addpapertype.php(36): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\designshoppos\addpapertype.php on line 36
但是,当我删除行:$value = $papername.'_'.$value; 时,查询运行正常。有人可以帮我解决这个问题吗?
我认为您只是遗漏了引号。它应该使用 ticks 进行转义。
$create=$pdo->prepare("CREATE TABLE `".$value."` (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, product_type VARCHAR(30) NOT NULL, gsm VARCHAR(500) NOT NULL) ;");
通过在 table 名称周围添加引号可以解决此问题。如果您 trim 并从 $papername 中删除所有空格,这将是一个很好的做法,这也将使这项工作有效。
$papername = preg_replace('/\s+/', '_', trim($papername));
我正在尝试创建包含 php 个名称变量的 mySQL table。
$papername=$_POST['txtpname'];
$papertype=$_POST['txtptype'];
$searchForValue = ',';
if (strpos($papertype, $searchForValue) !== false) {
$str_arr = explode (",", $papertype);
foreach ($str_arr as $value) {
$value = trim($value);
$value = preg_replace('/\s+/', '_', $value);
$value = $papername.'_'.$value;
$create=$pdo->prepare("CREATE TABLE ".$value." (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
product_type VARCHAR(30) NOT NULL, gsm VARCHAR(500) NOT NULL) ;");
$create->execute();
}
}
但是当我将两个变量与下划线组合在一起时(行:$value = $papername.'_'.$value;)并将其作为 table 名称,我得到一个 SQL语法错误。
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, product_type VAR...' at line 1 in C:\xampp\htdocs\designshoppos\addpapertype.php:36 Stack trace: #0 C:\xampp\htdocs\designshoppos\addpapertype.php(36): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\designshoppos\addpapertype.php on line 36
但是,当我删除行:$value = $papername.'_'.$value; 时,查询运行正常。有人可以帮我解决这个问题吗?
我认为您只是遗漏了引号。它应该使用 ticks 进行转义。
$create=$pdo->prepare("CREATE TABLE `".$value."` (id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, product_type VARCHAR(30) NOT NULL, gsm VARCHAR(500) NOT NULL) ;");
通过在 table 名称周围添加引号可以解决此问题。如果您 trim 并从 $papername 中删除所有空格,这将是一个很好的做法,这也将使这项工作有效。
$papername = preg_replace('/\s+/', '_', trim($papername));