2017-07-17 68 views
0

我正在創建一個安裝頁面,新用戶在其中輸入數據庫主機,用戶名,主機和密碼。我已經完成了這個工作。PHP - 創建多個表

我需要使它創建一個表。

我知道如何做到這一點,但不是用我的整個sql導出。

我需要插入我的整個SQL導出到他們的數據庫。

我收到了一個與online_users一行的錯誤,但我不知道爲什麼。

我的SQL出口是:

  $sql = " 
      CREATE TABLE IF NOT EXISTS failed (
      cpukey varchar(32) NOT NULL, 
      ip varchar(30) NOT NULL, 
      kvdata blob 
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 


      CREATE TABLE IF NOT EXISTS online_users (
      session char(100) NOT NULL DEFAULT '', 
      time int(11) NOT NULL DEFAULT '0', 
      username varchar(255) NOT NULL 
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

      INSERT INTO online_users (session, time, username) VALUES 
      ('s9p65g909gg9l6qh7dtr947hp4', 1500315177, 'BenzaHdd'); 


      CREATE TABLE IF NOT EXISTS serversettings (
      freemode tinyint(1) NOT NULL DEFAULT '0', 
      bypasses tinyint(1) NOT NULL DEFAULT '1', 
      id int(255) NOT NULL, 
      port int(10) NOT NULL, 
      token varchar(255) NOT NULL, 
      listener_ip text NOT NULL, 
      LogLocation varchar(255) NOT NULL 
     ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

      INSERT INTO serversettings (freemode, bypasses, id, port, token, listener_ip, LogLocation) VALUES 
      (1, 1, 1, 52, '675DFE06DC49999AE153815BD9A8818B', '192.168.0.1', 'C:\\Program Files (x86)\\Ampps\\www\\configuration\\log\\console.log'); 

      CREATE TABLE IF NOT EXISTS sitesettings (
      site_name varchar(50) NOT NULL DEFAULT 'TeamXerone', 
      team_motto varchar(100) NOT NULL DEFAULT 'The Art of Modding', 
      site_url varchar(999) NOT NULL DEFAULT 'http://localhost/XBL/', 
      id int(255) NOT NULL 
     ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; 

      INSERT INTO sitesettings (site_name, team_motto, site_url, id) VALUES 
      ('ProjectBenza', 'Generation Infinite', 'http://localhost', 1); 

      CREATE TABLE IF NOT EXISTS tokens (
      id int(11) NOT NULL, 
      user_id varchar(999) NOT NULL, 
      token varchar(300) NOT NULL, 
      generate_by varchar(99) NOT NULL, 
      used int(1) NOT NULL DEFAULT '0', 
      used_by varchar(99) DEFAULT NULL, 
      time_generated datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 
      durability int(11) NOT NULL COMMENT '0 = 1 day | 1 = 7 days | 2 = 14 days | 3 = 31 days | 4 = 3 months | 5 = 6 months | 6 = year | 7 = lifetime' 
     ) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=latin1; 

      INSERT INTO tokens (id, user_id, token, generate_by, used, used_by, time_generated, durability) VALUES 
      (71, 'BenzaHdd', 'ABC-123-45X', 'BenzaHdd', 0, NULL, '2017-07-15 17:54:01', 99), 
      (83, 'BenzaHdd', '58PN-756MV0-0YFN', 'BenzaHdd', 0, NULL, '2017-07-15 19:49:32', 1), 
      (89, 'Client', 'LEJU-GAR008-EBOJ', 'BenzaHdd', 0, NULL, '2017-07-15 20:11:38', 99); 

      CREATE TABLE IF NOT EXISTS updates (
      id int(99) NOT NULL, 
      title varchar(99) DEFAULT NULL, 
      description varchar(999) DEFAULT NULL, 
      postedBy varchar(99) DEFAULT NULL, 
      timePosted datetime NOT NULL DEFAULT CURRENT_TIMESTAMP 
     ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; 

      INSERT INTO updates (id, title, description, postedBy, timePosted) VALUES 
      (1, 'Launch of backend', 'This is the launch of my super advanced backend I have been working on for many, many months. I have finally decided to launch a stable and advanced version of this backend and will continue to add more and more as I think of new ideas. Prices will be shown in my skype mood.', 'Admin', '2016-12-14 21:05:54'); 

      CREATE TABLE IF NOT EXISTS users (
      id int(11) NOT NULL, 
      username varchar(50) NOT NULL DEFAULT 'User', 
      password varchar(255) DEFAULT 'Client', 
      email varchar(100) DEFAULT NULL, 
      cpukey varchar(32) DEFAULT NULL, 
      salt varchar(32) DEFAULT NULL, 
      ip varchar(32) NOT NULL DEFAULT '127.0.0.1', 
      time datetime DEFAULT NULL, 
      titleid text NOT NULL, 
      enabled tinyint(1) NOT NULL DEFAULT '0', 
      kvdata blob, 
      lastonline datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 
      profile_picture varchar(999) NOT NULL DEFAULT 'images/default.jpg', 
      userLevel int(1) NOT NULL DEFAULT '0', 
      register_time datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, 
      customlifetimenotify varchar(40) DEFAULT 'XboxPal - Change your notify on our site', 
      gamertag varchar(16) NOT NULL DEFAULT 'None' 
     ) ENGINE=InnoDB AUTO_INCREMENT=88 DEFAULT CHARSET=latin1; 

      INSERT INTO users (id, username, password, email, cpukey, salt, ip, time, titleid, enabled, kvdata, lastonline, profile_picture, userLevel, register_time, customlifetimenotify, gamertag) VALUES 
      (71, 'BenzaHdd', '$2y$10$Of8OZoXuZOwYIN65Yr1vPOQlCigAByxXmqvDd.GZQvWppBlQckq5W', '[email protected]', 'D31E025FF263E0B8AED87052BA86E51D', NULL, '94.9.19.252', '2018-08-29 00:00:02', '', 1, NULL, '2017-07-09 18:24:11', '../uploads/635933476972091420917241652_bl.jpg', 3, '2017-04-06 12:53:28', 'Ben is a fucking boss', 'None'), 
      (72, 'Benny', '$2y$10$2ojCm39w45NAONn0.XqeLeHyElC/7yyRVCnvxjKVo1kAkPNQ3I6XG', '[email protected]', '99999999999999999999999999999999', NULL, '204.13.204.125', '2018-07-06 00:00:00', '', 1, NULL, '2017-07-08 19:27:57', '../uploads/myAvatar black back.png', 0, '2017-07-18 00:00:00', 'Ben is a fucking god.', 'None'), 
      (73, 'TomGydey', '$2y$10$qnPbhfH0RbXmRi8RZ1RNKedVdqiYTPdUsg4GDK/5vhhA.DI2lmer2', '[email protected]', '99999999999999999999999999999999', NULL, '79.67.114.16', '2018-07-01 00:00:00', '', 1, NULL, '2017-07-07 13:24:25', 'http://ya-techno.com/forum/images/icons/myIcons/red-xbox-256.png', 0, '2017-07-07 11:54:49', 'XboxPal - Change your notify on our site', 'None'), 
      (85, '', 'Client', '', '', NULL, '127.0.0.1', '0000-00-00 00:00:00', '', 0, NULL, '2017-07-17 18:37:11', 'images/default.jpg', 0, '2017-07-17 18:37:11', '', 'None'); 
     "; 

     if(mysqli_query($con, $sql)){ 
      echo "Table created successfully."; 
     } else{ 
      echo "ERROR: Could not able to execute $sql. " . mysqli_error($con); 
     } 
+1

你得到了什麼錯誤? – Adam

+0

@AdamA'你的SQL語法有錯誤;查看與您的MySQL服務器版本相對應的手冊,以便在'CREATE TABLE IF NOT EXISTS online_users(session char(100)NOT''line 8'附近使用正確的語法。 – Benza

+0

因此您登錄到某人elses數據庫並在其數據庫上運行查詢創建你的表?這聽起來像是一個可怕的安全實踐。另外,更不用說主機不應允許遠程訪問mysql(或者只能通過本地主機或私有內部局域網訪問,所以它只能被服務器訪問他們的網絡),這意味着你的腳本不能在許多主機上工作(或者實際上不應該) –

回答

3

你的問題是,你有你的$sql串多個查詢。

mysqli_query($con, $sql)更改爲mysqli_multi_query($con, $sql),一切都會好起來的。

+0

優秀。謝謝。 – Benza