为每个注册的成员创建一个新表

时间:2017-03-19 23:03:02

标签: java php

我怎么能在include.php文件中包含每当设置代码时还添加了另一个函数,如下所示:

 $query = "CREATE TABLE IF NOT EXISTS (here would be post parameter email from java file) (
        id INT NOT NULL AUTO_INCREMENT,
        PRIMARY KEY(id),
        DATE    DATETIME NOT NULL,
        PAIN INT(11) UNSIGNED NOT NULL
    )" 

所以这意味着当用户点击注册时,他会被添加到users表中,并且还会创建一个新表,其中表的名称将是他的电子邮件!

include.php:

<?php

require_once 'include/DB_Functions.php';
$db = new DB_Functions();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['password'])) {

    // receiving the post params
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];



    // check if user is already existed with the same email
    if ($db->isUserExisted($email)) {
        // user already existed
        $response["error"] = TRUE;
        $response["error_msg"] = "Uporabnik že obstaja " . $email;
        echo json_encode($response);
    } else {
        // create a new user
        $user = $db->storeUser($name, $email, $password);
        if ($user) {
            // user stored successfully
            $response["error"] = FALSE;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = TRUE;
            $response["error_msg"] = "Neznana napaka!";
            echo json_encode($response);
        }
    }
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Manjkajo paramatri (name, email or password)!";
    echo json_encode($response);
}
?>

有关如何实施的任何帮助或想法都非常有用。

1 个答案:

答案 0 :(得分:1)

您需要使用反引号来包含表名,因为@ - 符号将导致非符合语法的表名:

$query = "
    CREATE TABLE IF NOT EXISTS `{$_POST["email"]}` (
    id INT NOT NULL AUTO_INCREMENT,
    PRIMARY KEY(id),
    DATE    DATETIME NOT NULL,
    PAIN INT(11) UNSIGNED NOT NULL
)" 

我优雅地假设,$ _POST [“email”]已经事先检查了理智。