-1

How to insert string split data into database using php

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

        $n = trim($input);

        // insert into Mysql 

        $output = "OK ... $client[0] $client[1] $client[2] $client[3]"; 

check above code and let get some ideas how to insert output into database with related fileds. I am using below code...

/ 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)'); 
    } 
} 

This also I have tried..

$bill_no = $client[0];
            $bill_amount = $client[1];
            $item_name = $client[2];
            $quantity = $client[3];
            $rate = $client[4];
            $amount = $client[5];
            $discount = $client[6];

            $query = mysql_query("INSERT INTO billing (bill_no, bill_amount, item_name, quantity, rate, amount, discount) VALUES ('$client[0]', '$client[1]', '$client[2]', '$client[3]', '$client[4]', '$client[5]', '$client[6]')");
krishna
  • 37
  • 8
  • 1
    And your question is? – Rizier123 May 15 '15 at 07:40
  • Hello there. Be careful with the split function which is deprecated since php 5.3 (see http://php.net/split). Use preg_split instead which produce the same result. And I don't necessarily understand your question because if the only thing that you want is how to save each part of the splitted string in the database, then you should tell us what you've tried :) – Cr3aHal0 May 15 '15 at 07:40
  • But preg_split not showing out put ...that's didn't changed the to pre_split – krishna May 15 '15 at 07:49
  • / 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)'); } } I have tried this way – krishna May 15 '15 at 07:50
  • My question is how to insert output into database using php – krishna May 15 '15 at 07:54
  • You should edit the first post to include these explanations so that everyone can see them and because it's not funny to see code without syntax coloration and indentation :) – Cr3aHal0 May 15 '15 at 07:58

2 Answers2

1

So, first of all, please go from mysql_* functions (which are deprecated, not supported anymore and can cause security issues) to PDO or mysqli (http://php.net/PDO and http://php.net/mysqli)

Now, considering your problem there, you did most of the job.

First of all, just retrieve the pieces of information provided and split them thanks to preg_split or explode (preg_split needs regular expressions as first parameter while explode does not).

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

Then your last part (In case you do not have the 7 parts of your string, then you will get an error) :

$bill_no = $client[0];
$bill_amount = $client[1];
$item_name = $client[2];
$quantity = $client[3];
$rate = $client[4];
$amount = $client[5];
$discount = $client[6];

then your last part

$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
            ));

But could you explain a bit what is the $items variable in the 2nd part please ? This way I would be able to fullfy my answer

Cr3aHal0
  • 809
  • 4
  • 11
  • I have taken some sample I have to assign $items as My $input – krishna May 15 '15 at 08:38
  • Well I don't really understand... What is your input ? global information about your bill or the details of it (each input does contain information about each item of each bill) ? – Cr3aHal0 May 15 '15 at 08:42
  • I am Using tcp/ip socket communication $input is client machine input..and I am spliting input by seperator, we need to insert that data to database. – krishna May 15 '15 at 09:42
0

Use explode function to split the string. Read the PHP Manual here

$chunk_arr = explode('<char>', $string);
print_r($chunk_arr);