-4

I am trying to make a script that will a table to the database('alphacrm') that has already been created.My error is that the echo line near the very bottom will not display, and no table is created to my database. I have already verified that $dbSuccess is true, not sure where I went wrong after that.I did try to see if it was correctly concatenated, but that did not seem to be the issue. If you spot my mistake it would be greatly appreciated!

if ($dbSuccess) {

  $createCoyTable_SQL = "CREATE TABLE alphacrm.tCompany ( ";
  $createCoyTable_SQL .= "ID INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY ";
  $createCoyTable_SQL .= "preName VARCHAR( 50 ) , ";
  $createCoyTable_SQL .= "Name VARCHAR( 250 ) NOT NULL, ";
  $createCoyTable_SQL .= "RegType VARCHAR( 50 ) NULL, ";
  $createCoyTable_SQL .= "SreetA VARCHAR( 150 ) NULL, ";
  $createCoyTable_SQL .= "SreetB VARCHAR( 150 ) NULL, ";
  $createCoyTable_SQL .= "SreetC VARCHAR( 150 ) NULL, ";
  $createCoyTable_SQL .= "Town VARCHAR( 150 ) NULL, ";
  $createCoyTable_SQL .= "County VARCHAR( 150 ) NULL, ";
  $createCoyTable_SQL .= "Postcode VARCHAR( 150 ) NULL, ";

  $createCoyTable_SQL .= "COUNTRY VARCHAR( 250 ) NOT NULL ";
  $createCoyTable_SQL .= ")";

  if (mysql_query($createCoyTable_SQL)) {
    echo "Creation of TABLE tCompany -- Succesful <br />";
  }

  $createPersonTable_SQL = "CREATE TABLE alphacrm.tPerson ( ";
  $createPersonTable_SQL .= "ID INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY ";
  $createPersonTable_SQL .= "Salutation VARCHAR( 20 ) , ";
  $createPersonTable_SQL .= "FirstName VARCHAR( 50 ) , ";
  $createPersonTable_SQL .= "LastName VARCHAR( 50 ) NOT NULL, ";
  $createPersonTable_SQL .= "CompanyID VARCHAR( 11 ) NOT NULL ";
  $createPersonTable_SQL .= ")";

  if (mysql_query($createPersonTable_SQL)) {
    echo "Creation of tPerson table was succesful <br />";
  }
}
  • If you are getting an error, it's best to say what it is – rjdown May 07 '16 at 01:27
  • *Classic, not using* => [`else{ http://php.net/manual/en/function.mysql-error.php }`](http://php.net/manual/en/function.mysql-error.php) – Funk Forty Niner May 07 '16 at 01:33
  • *"My error is that the echo line near the very bottom will not display, and no table is created to my database."* See comment #2. *Which I take it you're ignoring*. Yep. – Funk Forty Niner May 07 '16 at 01:34
  • This would be a whole lot less verbose if you used [multi-line strings](http://stackoverflow.com/questions/9744192/multi-line-strings-in-php). – tadman May 07 '16 at 03:10
  • **WARNING**: If you're just learning PHP, please, do not learn the obsolete [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It's awful and has been removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) helps explain best practices. Make **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will end up with severe [SQL injection bugs](http://bobby-tables.com/). – tadman May 07 '16 at 03:10

1 Answers1

0

Found an error, change this line:

   $createCoyTable_SQL .= "ID INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY ";

to this line:

   $createCoyTable_SQL .= "ID INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,";

Let me know if that fixed it, meanwhile I am searching for more.

Webeng
  • 7,050
  • 4
  • 31
  • 59
  • That was it!, the echo line is now successful and the code is working. Thanks a lot! – Cody Elhard May 07 '16 at 01:35
  • @CodyElhard No problem :). If my answer is the one that worked for you, please accept it (with a green checkmark) so that future users that read this thread know it's the one that worked for you. – Webeng May 07 '16 at 01:37