0

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 12 行的“CREATE TABLE IF NOT EXISTS administracia(userID INT(255) NOT NULL AUTO_INCREME”附近使用正确的语法

这是查询或多或少也更具可读性的版本

$data = "
CREATE TABLE IF NOT EXISTS zamestnanci(
userID INT(255) NOT NULL AUTO_INCREMENT,
osobnecislo VARCHAR(50) NOT NULL,
meno VARCHAR(50) NOT NULL,
priezvisko VARCHAR(50) NOT NULL,
nick VARCHAR(50) NOT NULL,
heslo CHAR(128) NOT NULL,
previerka VARCHAR(15) NOT NULL,
PRIMARY KEY (userID,nick)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS administracia(
userID INT(255) NOT NULL AUTO_INCREMENT,
osobnecislo VARCHAR(50) NOT NULL,
meno VARCHAR(50) NOT NULL,
priezvisko VARCHAR(50) NOT NULL,
nick VARCHAR(50) NOT NULL,
heslo CHAR(128) NOT NULL,
previerka VARCHAR(15) NOT NULL,
PRIMARY KEY (userID,nick)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS navrhy(
navrhID INT(255) NOT NULL AUTO_INCREMENT,
nazov VARCHAR(50) NOT NULL,
text VARCHAR(505) NOT NULL,
schvalenie VARCHAR(15) NOT NULL,
datum VARCHAR(50)  NOT NULL,
pridal VARCHAR(50) NOT NULL,
hlasovali VARCHAR(8000) NOT NULL,
kategoria VARCHAR(50) NOT NULL,
stroj VARCHAR(50) NOT NULL,
ano VARCHAR(1000) NOT NULL,
nie VARCHAR(1000) NOT NULL default '0',
PRIMARY KEY (navrhID)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS problemovyartikel(
artikelID INT(255) NOT NULL AUTO_INCREMENT,
artikelcisloVARCHAR(50) NOT NULL,
text VARCHAR(255) NOT NULL,
datum VARCHAR(50) NOT NULL,
pridal tinytext NOT NULL,
problem_cislo VARCHAR(100) NOT NULL,
PRIMARY KEY (artikelID)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS kategorie(
kategoriaID INT(255) NOT NULL AUTO_INCREMENT,
Nkategoria VARCHAR(50) NOT NULL,
Pkategoria VARCHAR(50) NOT NULL,
PRIMARY KEY (kategoriaID)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS novinky(
novinkaID INT(255) NOT NULL AUTO_INCREMENT,
novinka mediumtext NOT NULL,
PRIMARY KEY (novinkaID)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS stroje(
strojID INT(255) NOT NULL AUTO_INCREMENT,
stroj text NOT NULL,
PRIMARY KEY (strojID)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS kontakt(
kontaktID INT(255) NOT NULL AUTO_INCREMENT,
pole1 text NOT NULL,
pole2 text NOT NULL,
PRIMARY KEY (kontaktID)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS problem_cislo(
problem_cisloID INT(255) NOT NULL AUTO_INCREMENT,
problem_cislo VARCHAR(100) NOT NULL,
PRIMARY KEY (problem_cisloID)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS stranky(
strankaid INT(255) NOT NULL AUTO_INCREMENT,
stranka mediumtext NOT NULL,
stranka_obsah longtext NOT NULL,
subor mediumtext NOT NULL,
PRIMARY KEY (strankaid)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

CREATE TABLE IF NOT EXISTS chyby(
strankaid INT(255) NOT NULL AUTO_INCREMENT,
stranka CHAR(128) NOT NULL,
stranka_obsah VARCHAR(15) NOT NULL,
PRIMARY KEY (strankaid)) 
ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;

INSERT INTO administracia (userID,osobnecislo,meno,priezvisko,nick,heslo,previerka) VALUES('1','admin','admin','admin','admin','59db9e61d015935901442385d992dd34346dbe3439174fee700b56f27ad445ee42e052c0c014892a70cea63b912c7a3ee55c7c42e9e36e0f169ab7e6b886fd21','admin');
INSERT INTO kategorie (kategoriaID,Nkategoria) VALUES('1','kategoria1');
INSERT INTO novinky (novinkaID,novinka) VALUES('1','novinka1<br>novinka2');
INSERT INTO stroje (strojID,stroj) VALUES('1','stroj1');
INSERT INTO kontakt (kontaktID,pole1,pole2) VALUES('1','testificate','0912 345 678');
INSERT INTO problem_cislo (problem_cisloID,problem_cislo) VALUES('1','problem1');
INSERT INTO chyby (strankaid,stranka_obsah,stranka) VALUES('1','Registrácia prebehla úspešne!','úspech'),('2','Návrh bol úspešne pridaný!','úspech'),('3','Problémový artikel bol úspešne pridaný!','úspech'),('4','Aktivácia prebehne čo najskôr alebo ste zadali nesprávne údaje!,'totok')";

编辑:

使用以下函数拆分和执行查询:

$queries = preg_split("/;+(?=([^'|^\\\']*['|\\\'][^'|^\\\']*['|\\\'])*[^'|^\\\']*[^'|^\\\']$)/",$data); 
foreach ($queries as $query) 
  if(strlen(trim($query)) > 0) 
     mysql_query($query); { 
     $spracovat = mysql_query($data,$prepojenie) or die(mysql_error());
4

2 回答 2

6

您似乎将整个事情都塞进了一个 PHP 变量中。大多数 PHP MySQL API 每次执行只接受一个语句,所以你不能将所有这些组合成一个语句,除非你使用类似MySQLi::multi_query().

与其将所有这些打包到一个变量中并尝试使用 解析单个查询preg_split(),我建议创建一个查询数组并循环执行它们。这使得代码更清晰,更不容易出错。

$queries = array(

     'CREATE TABLE IF NOT EXISTS zamestnanci(
    userID INT(255) NOT NULL AUTO_INCREMENT,
    osobnecislo VARCHAR(50) NOT NULL,
    meno VARCHAR(50) NOT NULL,
    priezvisko VARCHAR(50) NOT NULL,
    nick VARCHAR(50) NOT NULL,
    heslo CHAR(128) NOT NULL,
    previerka VARCHAR(15) NOT NULL,
    PRIMARY KEY (userID,nick)) 
    ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;',

    'CREATE TABLE IF NOT EXISTS administracia(
    userID INT(255) NOT NULL AUTO_INCREMENT,
    osobnecislo VARCHAR(50) NOT NULL,
    meno VARCHAR(50) NOT NULL,
    priezvisko VARCHAR(50) NOT NULL,
    nick VARCHAR(50) NOT NULL,
    heslo CHAR(128) NOT NULL,
    previerka VARCHAR(15) NOT NULL,
    PRIMARY KEY (userID,nick)) 
    ENGINE=INNODB DEFAULT CHARSET=UTF8 AUTO_INCREMENT=1;',
    ...
);

foreach ($queries as $query) {
   // Execute the query, test for errors, etc.
}
于 2012-08-19T22:31:16.897 回答
4

您可能会使用 发出这个mysql_query,它只能发送一个查询,而不是包含 18 的字符串。

于 2012-08-19T22:31:20.143 回答