gpt4 book ai didi

php - 在 PHP 中为特定变量创建一个表 (MySQLi)

转载 作者:行者123 更新时间:2023-11-29 10:40:50 24 4
gpt4 key购买 nike

此代码应检查用户是否在表中,如果不在,则为用户创建表。现在它只是将表命名为 $MyServer。我该如何使其名称成为变量 $MyServer.

<?php
require "conn.php";
echo "debug 1";
$stmt = $conn->prepare("SELECT * FROM UserData WHERE username = ?");
$stmt->bind_param('s',$username);
$username = $_POST["username"];
$stmt->execute();
$stmt->store_result();

echo "debug 2";
if ($stmt->num_rows == 0){ // username not taken
echo "debug 3";
$stmt2 = $conn->prepare("INSERT INTO UserData (username, password) VALUES (?, ?)");
$password =($_POST["password"]);
$username =($_POST["username"]);
$stmt2->bind_param('ss', $username, $password);
$stmt2->execute();
$MyServer =($_POST["username"]);
$sql = ('CREATE TABLE $MyServer (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL
)');
if($conn->query($sql) === TRUE){
echo "Table created successfully";
} else {
echo "Table is not created successfully ";
}

if ($stmt2->affected_rows == 1){
echo 'Insert was successful.';

}else{ echo 'Insert failed.';
var_dump($stmt2);
}
}else{ echo 'That username exists already.';}

?>

最佳答案

为每个用户创建表不是一个好主意。您需要创建一个表来存储所有用户记录

但如果你真的想这样做。您需要将 ' 更改为 " 您的变量是否可以取值

$sql = "CREATE TABLE $MyServer (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL
)";

看看区别:

http://docs.php.net/manual/en/language.types.string.php

关于php - 在 PHP 中为特定变量创建一个表 (MySQLi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45520521/

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