创建PHP变量名不起作用的表

时间:2018-04-29 15:24:55

标签: php mysqli

我已经看过的内容

我是PHP的新手,我希望你们能指出我正确的方向。我已经阅读了以下主题并调整了答案,但我显然没有做正确的事情:

Creating table with variable name php mysql

Use a variable as table name when creating a table in mysql

Create mysql table with php variable not working

特别是,我的这段代码一直在努力适应我自己的需求,因为它是使用heredocs的少数几个例子之一,这是我们被教导使用的:

$create = <<<SQL
CREATE TABLE `$tableusername` (
    `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `please` VARCHAR(50) NOT NULL,
    `make` VARCHAR(50) NOT NULL,
    `this` VARCHAR(50) NOT NULL,
    `work` VARCHAR(50) NOT NULL
)

我的情况

我希望我的register.php使用用户选择的用户名创建一个新表。 (如果用户已经登录,则无法访问Register.php,因此永远不可能复制表。)此表稍后将用于绘制表中包含的数据,这就是为什么我没有设置一个PK,因为该表永远不会被连接或者其内容与另一个表共享。 (如果事实证明这不是它的工作原理,我将添加一个自动递增的ID)。

问题

表格未创建。

我的register.php编辑:根据建议,我已添加到代码中

我收到有关将每个列名称封装在引号内的语法的错误消息,因此我将其删除,然后收到以下错误消息:

Could not query database1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 11

<?php
include('template.php');
if (isset($_POST['username']) and isset($_POST['password'])) {
    $name = $mysqli->real_escape_string($_POST['username']);
    $pwd = $mysqli->real_escape_string($_POST['password']);
    $mail = $mysqli->real_escape_string($_POST['email']);
    $first = $mysqli->real_escape_string($_POST['fname']);
    $last = $mysqli->real_escape_string($_POST['lname']);
    $query = <<<END
    INSERT INTO p_users(username,password,email,fname,lname)
        VALUES('{$name}','{$pwd}','
            {$mail}','{$first}','{$last}')
END;
    $table = <<<END
        CREATE TABLE `$name` (
            steps_per_day int,
            energy_expenditure int,
            distance_walked_per_day int,
            bed_time int,
            waking_time int,
            hours_slept int,
            num_daytime_naps int,
            outdoor_temp int,
            indoor_temp int
        )
END;
    if ($mysqli->query($table) !== TRUE) {
            die("Could not query database" . $mysqli->errno . " : " . $mysqli->error);
                header('Location:index.php');
        }
    if ($mysqli->query($query) !== TRUE) {
        die("Could not query database" . $mysqli->errno . " : " . $mysqli->error);
            header('Location:index.php');
    }

}

$content = <<<END
<form method="post" action="register.php">
<input type="text" name="username" placeholder="username"><br>
<input type="password" name="password" placeholder="password"><br>
<input type="text" name="email" placeholder="email"><br>
<input type="text" name="fname" placeholder="first name"><br>
<input type="text" name="lname" placeholder="last name"><br>
<input type="submit" value="Register">
<input type="Reset" value="reset">
</form>
END;
echo $navigation;
echo $content;
?>

1 个答案:

答案 0 :(得分:-1)

indoor_temp int, - 最后一个逗号不应该存在,因为它是最后一列,因此导致... outdoor_temp int, indoor_temp int, )

图片的标题说明:

你应该真的看看Deadooshka的评论。将值用作表名!!!时,real_escape_string不会阻止您进行SQL注入。

可能一个包含所有用户读数的表和一个额外的列username(或userid)是一个更好,更安全的数据库设计。