使用php将多行插入mysql数据库的最佳方法是什么? [英] What's the best way to insert multiple rows into a mysql database using php?
本文介绍了使用php将多行插入mysql数据库的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的 php 代码从一个表单中获取结果并将它们插入到一个表中.
The php code below get's the results from a form and inserts them into a table.
我必须使用这个表结构,其中每一行都对应一个不同的值,例如名字.
I have to used this table structure where each row corresponds to a different value from the form eg First Name.
我已经编写了下面的代码,但它很麻烦.你能用更好的方法帮助我吗?谢谢堆!
I've written the code below but it's cumbersome. Can you help me with a better way? Thanks heaps!
$lists = $_POST['form']['lists'][0];
$first_name = $_POST['form']['first_name'];
$last_name = $_POST['form']['last_name'];
$idu = $db->insertid();
$db->setQuery("INSERT INTO #__rsmail_subscriber_details (`IdList`, `FieldName`,
`FieldValue`, `IdSubscriber`) VALUES ('" . $db->getEscaped($lists) . "', 'First Name'
, '" . $db->getEscaped($first_name) . "', '" . $db->getEscaped($idu) . "')");
$db->query();
$db->setQuery("INSERT INTO #__rsmail_subscriber_details (`IdList`, `FieldName`,
`FieldValue`, `IdSubscriber`) VALUES ('" . $db->getEscaped($lists) . "', 'Last Name'
, '" . $db->getEscaped($last_name) . "', '" . $db->getEscaped($idu) . "')");
$db->query();
推荐答案
可以执行批量插入:
INSERT INTO table (field1, field2) VALUES ('val1', 'val2'), ('val3', 'val4'), ...
在你的情况下是这样的:
In your case it is something like:
$db->setQuery("INSERT INTO #__rsmail_subscriber_details (`IdList`, `FieldName`,
`FieldValue`, `IdSubscriber`) VALUES ('".$db->getEscaped($lists)."', 'First Name'
, '".$db->getEscaped($first_name)."', '".$db->getEscaped($idu)."'), ('".$db->getEscaped($lists)."', 'Last Name'
, '".$db->getEscaped($last_name)."', '".$db->getEscaped($idu)."')");
这篇关于使用php将多行插入mysql数据库的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文