如何从PHP中的URL获取具有相同名称的多个参数并将所有记录插入表中 [英] How to get multiple parameters with same name from a URL in PHP and insert all records into table

查看:82
本文介绍了如何从PHP中的URL获取具有相同名称的多个参数并将所有记录插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MySQL查询将多个记录插入表中. 在我的url中,我得到了我输入的所有记录,但是在数据库中,它仅更新了我的最后一条记录.我在这里使用onclick函数来添加新的表行.任何帮助. 这是我的代码

I'm using MySQL query to insert multiple records into table. In my url i get all records that i have entered but in database it only updates my last record. I am using here onclick function to add new table rows. 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.

例如:您的URL看起来像

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

所以您的$_GET['finished_product_name']的值是pqr而不是abc.

so your $_GET['finished_product_name'] has value is pqr not abc.

如果您可以使用include []更改字段名称,则PHP将创建一个包含所有匹配值的数组:

If you can change the field name with include [], then PHP will create an array containing all of the matching values:

http://www.example.com/index.php?id[]=123&version[]=3&id[]=234&version[]=4

您的网址示例,

http://www.example.com/index.php?finished_product_name[]=abc&material_name[]=xxx&finished_product_name[]=pqr&material_name[]=yyy

您的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]; 
}

这篇关于如何从PHP中的URL获取具有相同名称的多个参数并将所有记录插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆