简体   繁体   English

如何合并来自表单和来自三个mysql表的数据,以使用php将数据插入另一个表中?

[英]How can I combine data from form and from three mysql tables to insert data in another table with php?

sql="INSERT INTO maticna
   (formCompany,formPlace,formDate,formPerson,formCollect
   ,formOffer,formDescribe,formBarkod,formSubject,formWorking
   ,formStatus,formRevision) 
SELECT 
  company.formCompany, company.formPlace
  , $formDate AS formDate
  , $formPerson AS formPerson
  , $formCollect AS formCollect
  , $formOffer AS formOffer
  , $formDescribe AS formDescribe
  , $formBarkod AS formBarkod
  , inv.formSubject, work.rnNo
  , $formStatus AS formStatus
  , $formRevision AS formRevision)"
 ."FROM company, inv, work 
 WHERE inv.formBarkod='$formBarkod'";

Something doesn't work with this code? 这段代码不起作用吗? Could anyone help with this code or some better method? 任何人都可以帮助使用此代码或更好的方法吗?

$formDate = mysql_real_escape_string($formdate);
$formPerson = mysql_real_escape_string($formPerson);
....
//For each and every $var you inject in the SQL statement.

$sql="INSERT INTO maticna
   (formCompany,formPlace,formDate,formPerson,formCollect
   ,formOffer,formDescribe,formBarkod,formSubject,formWorking
   ,formStatus,formRevision) 
SELECT 
  c.formCompany, c.formPlace
  , '$formDate' AS formDate
  , '$formPerson' AS formPerson
  , '$formCollect' AS formCollect
  , '$formOffer' AS formOffer
  , '$formDescribe' AS formDescribe
  , '$formBarkod' AS formBarkod
  , i.formSubject, w.rnNo
  , '$formStatus' AS formStatus
  , '$formRevision' AS formRevision
FROM company c
INNER JOIN work w ON (w.id = i.work_id)
INNER JOIN inv i ON (i.company_id = c.id)
WHERE i.formBarkod= '$formBarkod' ";

Mistakes you make 你犯的错误
1. Don't use implicit join syntax, it leads to cross joins; 1.不要使用隐式联接语法,它会导致交叉联接; always use explicit join syntax instead. 始终改用显式联接语法。
2. You did not have any join criteria. 2.您没有任何加入条件。
3. All $vars need to be escaped always. 3.所有$ vars必须始终被转义。
4. All $vars, be they numbers or not need to be quoted in the SQL-statement. 4.所有$ vars,无论是否为数字,都需要在SQL语句中引用。
5. Minor syntax error in the insert statement. 5. insert语句中的次要语法错误。

您不应该在选择之前插入VALUES吗?

INSERT INTO maticna (...) VALUES (SELECT ...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个表以及MYSQL和PHP中的表单插入数据 - Insert data from another table and also from form in MYSQL and PHP 如何使用SESSION数据来选择1个数据库条目,然后使用PHP和MySQL的表单中的数据插入到另一个表中? - How do I use SESSION data to SELECT 1 database entry and then INSERT into another table with data from a form as well with PHP and MySQL? 如何使用php将来自mysql的选定数据插入mysql中的另一个表? - How to insert selected data from mysql with php to another table in mysql? 无法从 PHP 表格将数据插入 MySQL - Can not Insert data into MySQL from PHP form MySQL / PHP:如何将登录的用户ID插入另一个表,该表从用户输入的表单收集数据 - MySQL/PHP: How to insert logged in user id into another table that is gathering data from a form that the user is inputting 如何使用MySQL PHP将两个表中的数据插入到一个表中 - How do I Insert data from two tables into one table using MySQL PHP 从PHP中的三个MySQL表中选择数据 - Selecting data from three MySQL tables in PHP 我想将表单中的数据插入表中,然后从另一个表中选择另一个数据并使用PHP插入到第三张表中 - I want to insert a data from a form into a table, and select another data from another table and insert into third table using PHP 如何在mysql中将数据从一个表插入到另一个表? - How do I insert data from one table to another in mysql? PHP:从表单和同一表中的另一个表插入数据 - PHP: Insert data from form and another table in the same table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM