-2

This my code

$_SESSION['poraw'] = substr(md5(time()), 0, 16);
$userid= $_SESSION["userid"];
$today = date('Y-m-d');
$month = $_POST['month'];
for($i = 0; $i < count($_POST['id']); $i++){
    $id = $_POST['id'][$i];
    $resin = $_POST['resin'][$i];
    $hasil = $_POST['hasil'][$i];

    $sql="INSERT INTO po_supplier_temp (unique_code,po_customer_month,resin,qty,username)
        VALUES ('".$_SESSION['poraw']."','$month','$resin','$hasil','$userid')";
    $sql = $conn->query($sql);
}

Current results:

current results

if $resin same , i want $hasil plus it (+) , before save to dabatase and only one name $resin to database in same time

Like this:

like this

thanks

  • Are you inserting all those records at the same time? If not, and you have already existing records that you want to update … well then you need an UPDATE sql query. – misorude Aug 23 '19 at 12:16
  • @misorude yes same time. – Agusto Nice Aug 23 '19 at 12:33
  • Well then you should loop over your data first, to aggregate and sum it up in the necessary form - use the `resin` value as key, and sum up the corresponding `hasil` under that. Afterwards you loop over that new array, and insert the data. – misorude Aug 23 '19 at 12:38
  • @misorude teach me – Agusto Nice Aug 23 '19 at 13:15
  • `$foo = []; for(…){ $resin = $_POST['resin'][$i]; $hasil = $_POST['hasil'][$i]; if(isset($foo[$resin])) { $foo[$resin] += $hasil; } else { $foo[$resin] = $hasil; } } var_dump($foo);` - see what that gets you, take it from there. – misorude Aug 23 '19 at 13:24
  • array(1) { [6]=> string(3) "103" } array(2) { [6]=> string(3) "103" [5]=> string(3) "203" } array(2) { [6]=> int(408) [5]=> string(3) "203" } array(2) { [6]=> int(408) [5]=> int(607) } – Agusto Nice Aug 23 '19 at 14:43
  • @misorude how to get only [6]=> int(408) [5]=> int(607) **resin 6 with qty 408 and resin 5 with qty 607** – Agusto Nice Aug 23 '19 at 16:43
  • how to get only [6]=> int(408) [5]=> int(607) **resin 6 with qty 408 and resin 5 with qty 607** – Agusto Nice Aug 25 '19 at 14:37

1 Answers1

0

You have two options: Create a stored procedure to handle the custom logic, or integrate the logic inside your php.

I'll only show you how you can make a stored procedure to do the task you wish to complete. If you need help on how to use a stored procedure, this has already been answered somewhere around here.

--- Transact-SQL Syntax
CREATE PROCEDURE [dbo].[InsertOrUpdateMyThing]
(
   @SessionId int,
   @Month date,
   @Resin int,
   @Hasil int,
   @UserId int

)
AS
BEGIN
 SET NOCOUNT ON

 IF EXISTS(SELECT 1 FROM po_supplier_temp WHERE resin = @Resin)
 BEGIN
   UPDATE p
      SET p.resin = p.resin + 1
   FROM po_supplier_temp p
   WHERE p.resin = @Resin
 END
 ELSE
 BEGIN
    INSERT INTO po_supplier_temp (unique_code, po_customer_month, resin,qty, username)
    VALUES (@SessionId, @Month, @Resin, @Hasil, @UserId)
 END
END

EDIT: If you're passing multiple values at the same time, you'll need to edit this to accept a table-valued parameter with all the necessary information, as well as modify the code inside the procedure to handle a set of data. It'd be better to do that, rather than invoke this procedure X amount of times.

SpiritBob
  • 2,355
  • 3
  • 24
  • 62