sql语法错误

2g32fytz  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(428)

我尝试在一个表之间使用两个insert查询将数据插入到多个表中 BEGIN; 以及 COMMIT; 但我得到一个语法错误如下:
sql语法有错误;似乎错误就在第2行:“insert into company(name,street,zip,place,phone)”
我的问题是:

BEGIN;
INSERT INTO company (name, street, zip, place, phone, contact, email, logo)
  VALUES('".$name."', '".$street."', '".$zip."', '".$place."', '".$phone."', '".$contact."', '".$email."', '".$file."');
INSERT INTO users (username, password)
  VALUES('".$username."', '".$pass."');
COMMIT;

我不确定错误在哪里。。
我在评论中要求的php查询:

$addcompany = "
    BEGIN;
  INSERT INTO company (name, street, zip, place, phone, contact, email, logo)
      VALUES('".$name."', '".$street."', '".$zip."', '".$place."', '".$phone."', '".$contact."', '".$email."', '".$file."');
  INSERT INTO users (username, password)
      VALUES('".$username."', '".$pass."');
  COMMIT;";
  $addcompany = $conn->query($addcompany);

回应我的问题时:

BEGIN;
INSERT INTO company (name, street, zip, place, phone, contact, email, logo)
VALUES('test', 'test street 123', '3235DE', 'Rotterdam', '2356235235', 'testcontact', 'test@test.nl', '/home/website/public_html/logos/test.jpg');
INSERT INTO users (username, password)
VALUES('testuser', '$2y$10$M2vO60OuvmNxsmzbCIGhB.4EgQ55Qid/DPGAC3BYs/I2Twj8Afo5q');
COMMIT;
aydmsdu9

aydmsdu91#

你需要转义符 ' 要使php变量将值视为字符串,请通过传递sql参数和 print @sql 在执行之前

$addcompany = 
    'BEGIN
  INSERT INTO company (name, street, zip, place, phone, contact, email, logo)
      VALUES( '''+ ".$name." +''', '''+".$street."+''', '''+".$zip."+''', 
              '''+".$place."+''', '''+".$phone."+''', '''+".$contact."+''', 
              '''+".$email."+''', '''+".$file."+''');
  INSERT INTO users (username, password)
      VALUES('''+".$username."+''', '''+".$pass."+''');
  COMMIT'
  $addcompany = $conn->query($addcompany);
ia2d9nvy

ia2d9nvy2#

似乎$conn->query()只支持一个查询。multi\u query()是一种可以尝试的方法。我发现:https://www.w3schools.com/php/php_mysql_insert_multiple.asp

snvhrwxg

snvhrwxg3#

在一个表中插入多条记录的sql语法如下:,

INSERT INTO MyTable (Column1, Column2) VALUES(Value1, Value2), (Value1, Value2)

试试这个

相关问题