0

I just want to update my latest data inserted only.. but I'm sure I'm wrong at WHERE (at $query1 and $query2)

<?php
include "koneksi.php";
$kdj=$_POST['kdj'];
$id=$_POST['id'];
$qty = $_POST['qty'];
$kodelagi = mysql_query("Select kd_jual from nota ORDER BY id_nota DESC LIMIT 1");
$row = mysql_fetch_array($kodelagi);
$kodelagi1 = $row['kd_jual'];
if((!empty($id)) && (!empty($kdj)) && (!empty($qty)))
{
    $query = mysql_query("INSERT INTO nota (id_item,qty,kd_jual) values ('$id','$qty','$kdj');");
    $query1 = mysql_query("UPDATE nota,item SET nota.harga_item = item.harga_item WHERE nota.kd_jual = '$kodelagi1'");
    $query2 = mysql_query("UPDATE nota SET subtotal = harga_item * qty WHERE nota.kd_jual = '$kodelagi1'");
    ?><script language="Javascript">;
    document.location = 'transaksi2.php' </script><?php
}else 
{
    print "<script>alert('Maaf, tidak boleh ada field yang kosong !');
    javascript:history.go(-1);</script>";
}?> 

because when I run this, I just got this on my DB

| id_nota | id_item | harga_item | qty | subtotal | Kd_jual
|  57     |   11    |  0         | 23  |  0       |13-10-201323:32:20
wogsland
  • 9,106
  • 19
  • 57
  • 93
DaneSuegi
  • 7
  • 4
  • What appears to be the problem? However, shouldn't you have a join condition on the first UPDATE query, or is there only a single record on the item table? – Kickstart Oct 13 '13 at 19:49

1 Answers1

0

You are trying to get the latest insert record before even inserting. Do Select Query after Inserting the record. Also one more point, you are getting "kd_jual" value for last inserted record. It is the datetime column. There can be multiple records with same datetime. So in certain use case this will fail & in turns updates more than one record(Not just latest inserted). Better use the ID column(id_nota)

I have tried to modify your code as below. I didn't tested it. Correct it if required.

<?php
include "koneksi.php";
$kdj=$_POST['kdj'];
$id=$_POST['id'];
$qty = $_POST['qty'];

if((!empty($id)) && (!empty($kdj)) && (!empty($qty)))
{
$query = mysql_query("INSERT INTO nota (id_item,qty,kd_jual) values ('$id','$qty','$kdj');");

$kodelagi = mysql_query("Select id_nota from nota ORDER BY id_nota DESC LIMIT 1");
$row = mysql_fetch_array($kodelagi);
$max_id_nota= $row['id_nota'];

$query1 = mysql_query("UPDATE nota,item SET nota.harga_item = item.harga_item WHERE nota.id_nota = '$max_id_nota'");

$query2 = mysql_query("UPDATE nota SET subtotal = harga_item * qty WHERE nota.id_nota = '$max_id_nota'");

?><script language="Javascript">;
document.location = 'transaksi2.php' </script><?php
}else 
{
print "<script>alert('Maaf, tidak boleh ada field yang kosong !');
javascript:history.go(-1);</script>";
}?>