0

我只想更新我最新插入的数据.. 但我确定我在WHERE(at $query1and $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>";
}?> 

因为当我运行这个时,我刚刚在我的数据库中得到了这个

| id_nota | id_item | harga_item | qty | subtotal | Kd_jual
|  57     |   11    |  0         | 23  |  0       |13-10-201323:32:20
4

1 回答 1

0

您正在尝试在插入之前获取最新的插入记录。插入记录后选择查询。还有一点,您将获得最后插入记录的“kd_jual”值。它是日期时间列。可以有多个具有相同日期时间的记录。因此,在某些用例中,这将失败并依次更新多个记录(不仅仅是最新插入的)。更好地使用 ID 列(id_nota)

我已尝试修改您的代码,如下所示。我没有测试它。如果需要,请更正它。

<?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>";
}?> 
于 2013-10-13T19:48:38.023 回答