[英]How to get multiple parameters with same name from a URL in PHP and insert all records into table
I'm using MySQL query to insert multiple records into table. 我正在使用MySQL查询将多个记录插入表中。 In my url i get all records that i have entered but in database it only updates my last record.
在我的url中,我得到了我输入的所有记录,但是在数据库中,它仅更新了我的最后一条记录。 I am using here onclick function to add new table rows.
我在这里使用onclick函数来添加新的表行。 Any help.
任何帮助。 Here is my code
这是我的代码
if (isset($_GET['submit']))
{
require_once("shine_class.php");
$s = new shine;
$s->connection();
$date1 = date_default_timezone_set('Asia/Kolkata');
$date1= time() ;
$newdate1 = date("d-m-Y", $date1);
for ($i=0; $i < count($_GET['finished_product_name']); $i++ )
{
$product =$_GET['finished_product_name'];
$material = $_GET['material_name'];
$quantity = $_GET['product_quantity'];
// mysql_query("INSERT INTO material_used (product_name, material_name, product_quantity, date) VALUES ('$a', '$b', '$c','$newdate1')") or die(mysql_error());
$insert ="insert into material_used set `product_name` = '".$product."', `material_name` = '".$material."', `product_quantity` = '".$quantity."',`date` = '".$newdate1."' ";
$select = mysql_query($insert) or die(mysql_error());
}
}
You try to assign a value with same name.so your last value replace with the existing value. 您尝试分配一个具有相同名称的值。因此,最后一个值将替换为现有值。
for example :your URL look like, 例如:您的网址如下所示:
http://www.example.com/index.php?finished_product_name=abc&material_name=xxx&finished_product_name=pqr&material_name=yyy
so your $_GET['finished_product_name']
has value is pqr
not abc
. 因此您的
$_GET['finished_product_name']
值为pqr
而不是abc
。
If you can change the field name with include []
, then PHP will create an array containing all of the matching values: 如果可以使用include
[]
更改字段名称,则PHP将创建一个包含所有匹配值的数组:
http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4
your URL example like, 您的网址示例如下:
http://www.example.com/index.php?finished_product_name[]=abc&material_name[]=xxx&finished_product_name[]=pqr&material_name[]=yyy
your for loop is : 您的for循环是:
for ($i=0; $i < count($_POST['finished_product_name']); $i++ )
{
$product =$_POST['finished_product_name'][$i];
$material = $_POST['material_name'][$i];
$quantity = $_POST'product_quantity'][$i];
}
$insert ="insert into material_used(product_name,material_name,product_quantity,date) VALUES( '$product', '$material','.$quantiy','$newdate1')
use following function to insert data in DB 使用以下功能在DB中插入数据
$data['col1']='value1';
$data['col2']='value2';
.
.
$data['coln']='valuen';
insert('table_name',$data);
function Insert( $table, $condition )
{
$sql = "INSERT INTO `$table` SET ";
$content = null;
foreach ( $condition as $k => $v )
{
$v_str = null;
if ( is_numeric($v) )
$v_str = "'{$v}'";
else if ( is_null($v) )
$v_str = 'NULL';
else
$v_str = "'" . mysql_real_escape_string($v) . "'";
$content .= "`$k`=$v_str,";
}
$content = trim($content, ',');
$sql .= $content;
return $result = mysql_query($sql);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.