2017-07-17 41 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