-1

Can someone help me with that code, any directions welcome I want every user who passed session to create table(every user creates own table -others cant see it).

<?php
session_start();
if($_SESSION['user']==''){
 header("Location:login.php");
}else{
 $dbh=new PDO('mysql:dbname=something;host=127.0.0.1', 'something', 'something');
 $sql=$dbh->prepare("SELECT * FROM users WHERE id=?");
 $sql->execute(array($_SESSION['user']));
 while($r=$sql->fetch()){
    $sql = "CREATE TABLE .'$r['username'].'" (
    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    firstname VARCHAR(30) NOT NULL,
    lastname VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    reg_date TIMESTAMP
    )";
    $conn->exec($sql);
    echo " created successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
 }
}
?>
hichris123
  • 10,145
  • 15
  • 56
  • 70
OverHeat
  • 129
  • 2
  • 12

2 Answers2

0

You have some error in your query string. You have one double quote you should remove, and the table name should be wrapped with ` instead of '. So your code should look like:

        $sql = "CREATE TABLE `{$r['username']}` (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            firstname VARCHAR(30) NOT NULL,
            lastname VARCHAR(30) NOT NULL,
            email VARCHAR(50),
            reg_date TIMESTAMP
        )";

And even if its not really related to the topic you should ask yourself if create a table per user is the best solution.

olibiaz
  • 2,551
  • 4
  • 29
  • 31
  • this is far as i know:/ any directions would be greatful.links or some example.... thanks – OverHeat Mar 02 '16 at 00:17
  • Why do you create one table per user? In order to "others cant see it"? If yes you can have a look there: http://stackoverflow.com/questions/17075424/get-the-data-of-specific-user-in-php http://stackoverflow.com/questions/34125900/filtering-mysql-data-in-php-to-only-show-data-of-the-current-sessions-user Does the code works with the fix? – olibiaz Mar 02 '16 at 00:22
  • thanks No code still dont work. and i dont get any syntax errors in dreanweaver.In Msql nothing isnt inserted. – OverHeat Mar 02 '16 at 00:49
0

You can try to fix your php code and get something like this:

<?php
session_start();
if($_SESSION['user']==''){
    header("Location:login.php");
}else{
    $dbh=new PDO('mysql:dbname=something;host=127.0.0.1', 'something', 'something');
    $sql=$dbh->prepare("SELECT * FROM users WHERE id=?");
    $sql->execute(array($_SESSION['user']));
    while($r=$sql->fetch()){
        $sql = "CREATE TABLE `".$r['username']."` (
            id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            firstname VARCHAR(30) NOT NULL,
            lastname VARCHAR(30) NOT NULL,
            email VARCHAR(50),
            reg_date TIMESTAMP
        )";
        try {
            $dbh->exec($sql);
            echo " created successfully";
        } catch(PDOException $e) {
            echo $sql . "<br>" . $e->getMessage();
        }
    }

    $conn = null;
}
?>
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46