Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php将拆分字符串插入mysql数据库_Php_Mysql_Arrays_Sockets_Split - Fatal编程技术网

如何使用php将拆分字符串插入mysql数据库

如何使用php将拆分字符串插入mysql数据库,php,mysql,arrays,sockets,split,Php,Mysql,Arrays,Sockets,Split,如何使用php将字符串拆分数据插入数据库 $client=split("\|", $input); $n = trim($input); // insert into Mysql $output = "OK ... $client[0] $client[1] $client[2] $client[3]"; 检查上面的代码,让我们了解如何将输出插入到带有相关文件的数据库中。 我正在使用下面的代码 / loop through array

如何使用php将字符串拆分数据插入数据库

$client=split("\|", $input);

        $n = trim($input);

        // insert into Mysql 

        $output = "OK ... $client[0] $client[1] $client[2] $client[3]"; 
检查上面的代码,让我们了解如何将输出插入到带有相关文件的数据库中。 我正在使用下面的代码

/ loop through array 
$number = count($items); 
for ($i=0; $i<=$number; $i++) 
{ 
    // store a single item number and quantity in local variables 
    $itno = $items[$i]; 
    $quant = $quantities[$i]; 

    if ($items[$i] <> '') { 
    mysql_query('INSERT INTO reservation_items (reservationID,productID,productQuantity) VALUES($theReservationID,$itno,$quant)'); 
    } 
} 

使用explode函数拆分字符串。阅读PHP手册

$chunk\u arr=explode(“”,$string);
打印($chunk\u arr);

因此,首先,请从mysql_*函数(已弃用,不再受支持,可能导致安全问题)转到PDO或mysqli(和)

现在,考虑到你的问题,你做了大部分工作

首先,只需检索提供的信息片段,并使用preg_split或explode进行拆分(preg_split需要正则表达式作为第一个参数,explode则不需要)

然后是最后一部分(如果您没有字符串的7部分,那么您将得到一个错误):

那你的最后一部分呢

$req = $PDO->prepare("INSERT INTO billing (bill_no, bill_amount, item_name, quantity, rate, amount, discount) VALUES (:bill_no, :bill_amount, :item_name, :quantity, :rate, :amount, :discount)");
$req->execute(array(
            "bill_no" => $bill_no, 
            "bill_amount" => $bill_amount,
            "item_name" => $item_name,
            "quantity" => $quantity,
            "rate" => $rate,
            "amount" => $amount,
            "discount" => $discount
            ));

但是你能解释一下第二部分中的
$items
变量是什么吗?这样我就可以完整地回答你的问题了,你的问题是?你好。请注意split函数,它自PHP5.3以来就被弃用了(请参阅)。使用preg_split代替,这会产生相同的结果。我不一定理解你的问题,因为如果你唯一想要的是如何在数据库中保存分割字符串的每一部分,那么你应该告诉我们你尝试了什么:)但是preg_split没有显示put…这并没有更改为pre_split/loop through array$number=count($items);对于($i=0;$iMy)我的问题是如何使用phpsplit将输出插入数据库工作,是否要将输出插入数据库检查此问题我已采取一些示例,我必须将$items指定为我的$inputWell我真的不明白…您的输入是什么?有关您账单的全局信息或详细信息(每个输入确实包含每个账单的每个项目的信息)?我使用tcp/ip套接字通信$input是客户端机器输入..我使用分隔器拆分输入,我们需要将该数据插入数据库。
$chunk_arr = explode('<char>', $string);
print_r($chunk_arr);
$client=preg_split("\|", $input);
$bill_no = $client[0];
$bill_amount = $client[1];
$item_name = $client[2];
$quantity = $client[3];
$rate = $client[4];
$amount = $client[5];
$discount = $client[6];
$req = $PDO->prepare("INSERT INTO billing (bill_no, bill_amount, item_name, quantity, rate, amount, discount) VALUES (:bill_no, :bill_amount, :item_name, :quantity, :rate, :amount, :discount)");
$req->execute(array(
            "bill_no" => $bill_no, 
            "bill_amount" => $bill_amount,
            "item_name" => $item_name,
            "quantity" => $quantity,
            "rate" => $rate,
            "amount" => $amount,
            "discount" => $discount
            ));