Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

<?php 
    include('database_connection.php');

    $email = $_SESSION['Email'];

    $q = 'SELECT * 
        FROM  `'.$email.'` 
        LIMIT 0 , 30';

    $r = mysqli_query ($dbc, $q);

    if( $r )
    {
         echo '<table class="responsive">';
     echo '<tr><td><strong>Name</strong></td><td><strong>Description</strong></td><td><strong>Date</strong></td><td><strong>Shared?</strong></rd><td><strong>Edit:</strong></td><td><strong>View:</strong></td></tr>';

            while($row = mysqli_fetch_array($r, MYSQLI_BOTH)) {

                echo'<tr><td>'.$row[6].'</td><td>'.$row[7].'</td><td>'.$row[8].'</td><td>'.$row[2].'</td><td><a href="https://daccaa.com/beta/edits/editor.php?file='.$row[5].'" class="select">Edit</a></td><td><a href="https://daccaa.com/beta/edits/view.php?file='.$row[5].'" class="select">View</a></td></tr>';
            }
            mysqli_free_result($r);

            echo "</table>";

    } else {



        $mysqlcode = "CREATE TABLE  `".$email."` (
         `id` INT( 8 ) NOT NULL ,
         `Note` text NOT NULL ,
         `Share` VARCHAR( 200 ) DEFAULT  'No' NOT NULL ,
         `Share Url` VARCHAR( 200 ) NOT NULL ,
         `Short Url` VARCHAR( 200 ) NOT NULL ,
         `Location` VARCHAR( 200 ) NOT NULL ,
         `Name` VARCHAR( 200 ) NOT NULL ,
         `Description` VARCHAR( 200 ) NOT NULL ,
         `Date` VARCHAR( 40 ) NOT NULL ,
        PRIMARY KEY (  `id` )
        ) TYPE = MYISAM ;";

        $d = mysql_query($dbc, $mysqlcode);


    if($d) {
        echo 'Created Database For your notes.';

        $sql = 'INSERT INTO `example` (`id`, `Note`, `Share`, `Share Url`, `Short Url`, `location`, `Name`, `Description`, `Date`) VALUES (\'0\', \'Welcome to Daccaa Edits, here you can make and edit notes and pictures.\', \'No\', \'\', \'\', \'welcome.txt\', \'Welcome\', \'This is a welcome note.\', \'07/06/2014\')';
        $i = mysql_query($dbc, $sql);

        if($i) {
            echo '<h2>Created a first note.</h2>';
        } else {
            echo '<h2>Failed to create a note for you.</h2>';
        }
    } else {
        echo 'Failed to create database for you notes.';
    }
    }

    mysqli_close($dbc);
?>

This is my code, I am trying to make it so if the Table in a database does not exist then it will create a Table for a certain database with the table name being the Users Email. I have tested the MYSQL code directly in PHPmyadmin and it seems to work. I have come to the point now where I am unsure what is wrong.

The first bit works where it displays the information from the database and table when I have already manually created the database.

I will be adding more security prevention methods along the way but if anyone does have any suggestions I will try and implement them.

All ideas help.

share|improve this question

marked as duplicate by hakre Jun 7 at 10:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
It would be generally a good idea not to put whitespaces, or any kind of special characters in a column/table name. –  bali182 Jun 7 at 10:04
    
You are missing to do proper error checking with the database client API you're using. For the outdated mysql_* extension you have in your question, please see php.net/mysql_query which contains information how to treat return values and handle errors. –  hakre Jun 7 at 10:31

3 Answers 3

up vote 1 down vote accepted

Usage of TYPE has become obsolete and no longer supported. That is the reason why your create table did not work.

Use ENGINE instead.

create table table-name(
  ...
) ENGINE=InnoDB;

OR

create table table-name(
  ...
) ENGINE=MyISAM;

And I suggest you to go with 'MySQLi' or 'PDO' functions in PHP as 'mysql_*' are deprecated.

share|improve this answer

Replace

TYPE = MYISAM 

with

ENGINE=MyISAM

OR

$mysqlcode = "CREATE TABLE  `".$email."` (
     `id` INT( 8 ) NOT NULL ,
     `Note` text NOT NULL ,
     `Share` VARCHAR( 200 ) DEFAULT  'No' NOT NULL ,
     `Share Url` VARCHAR( 200 ) NOT NULL ,
     `Short Url` VARCHAR( 200 ) NOT NULL ,
     `Location` VARCHAR( 200 ) NOT NULL ,
     `Name` VARCHAR( 200 ) NOT NULL ,
     `Description` VARCHAR( 200 ) NOT NULL ,
     `Date` VARCHAR( 40 ) NOT NULL ,
    PRIMARY KEY (  `id` )
    )  ENGINE=MyISAM ;";
share|improve this answer

Instead of doing some manually checking of query, you could simply do this:

$query = "CREATE TABLE IF NOT EXISTS `{tablename from email-session}` (
        `id` INT( 8 ) NOT NULL ,
        `Note` text NOT NULL ,
        `Share` VARCHAR( 200 ) DEFAULT  'No' NOT NULL ,
        `Share Url` VARCHAR( 200 ) NOT NULL ,
        `Short Url` VARCHAR( 200 ) NOT NULL ,
        `Location` VARCHAR( 200 ) NOT NULL ,
        `Name` VARCHAR( 200 ) NOT NULL ,
        `Description` VARCHAR( 200 ) NOT NULL ,
        `Date` VARCHAR( 40 ) NOT NULL ,
         PRIMARY KEY (  `id` )
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;";

//Execute query $query

It's also a good practice to defne default charset like above. Auto Increment indicates with which value to start the autoincrement with (usually 1)

As stated by previous answers, you should also user PDO or mysq1i with prepared statements and change TYPE to ENGINE.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.