我有变量whos值是0到1000之间的随机数,我想在创建新表时使用它作为名称。我试图通过将我的sql与存储随机数的变量连接来做到这一点,这没有用,有没有办法做到这一点?谢谢
include 'includes/db_connect_ssg.php';
if (isset($_POST['new_user_name'])&&isset($_POST['new_user_password'])) {
$username = $_POST['new_user_name'];
$password = $_POST['new_user_password'];
$randID = rand(0,1000);
$sql = "INSERT INTO `Users`(`id`, `username`, `password`, `admin`, `href`) VALUES ('$randID','$username','$password','0','ssgprofile.php?id=$randID')";
$query = mysqli_query($dbc, $sql);
$id = (string)$randID;
$q = "CREATE TABLE CONCAT('userTable_',$id) (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
$qquery = mysqli_query($dbc, $q);
if ($query&&$qquery) {
include 'admin_loadUsers.php';
}else{
echo "Could not connect sorry please try again later, for more info please contact BB Smithy at 0838100085";
}
}
答案 0 :(得分:2)
你可以使用:
$q = "CREATE TABLE `userTable_".$id."` (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
但要注意,创建一个名称中包含数字的表几乎总是数据库设计不良的标志。
只需将firstname,lastname,email和reg_date列添加到表Users即可,而不是只创建一行。另外,通过调用rand(0,1000)生成用户ID的方法将导致冲突(rand将返回一个已在Users表中用作ID的值)。使用AUTO_INCREMENT生成用户ID。
答案 1 :(得分:0)
您没有有效的表名
来自Mysql文档:
Identifiers may begin with a digit but unless quoted may not consist solely of digits