PHP脚本继续向MySQL添加相同的ID

时间:2017-08-28 20:53:36

标签: php mysql

所以我正在尝试使用下面的PHP脚本向MySQL添加新的临时事务数据。

if ($mod == "penjualan" AND $act == "add") {
    //check data from table barang detail where id from the form 
    $cekBarang = mysqli_query($conn, "SELECT * FROM tbbarangdetail
                            WHERE idBarangDetail = '$_GET[id]'") or die(mysqli_error($conn));
    $assoc = mysqli_fetch_assoc($cekBarang);

    if ($assoc != NULL) {
        $disc = 0;
        //check discount
        if(!empty($_GET['disc']) AND is_numeric($_GET['disc'])) {
            $disc = $_GET['disc'];
        }
        //get hargaPerKg and hargaPerBarang
        $a = mysqli_query($conn, "SELECT hargaPerKg, hargaPerBarang FROM tbbarang where idBarang = '$assoc[idBarang]'");
        $b = mysqli_fetch_assoc($a);
        //get jumlah Kg from detail barang
        $jumlahKg = $assoc['jumlahKg'];
        //set id transaksi detail automatically
        $idTransaksiDetailtmp = noTransaksiDetail();
        date_default_timezone_set('Asia/Kuala_Lumpur');
        $tgl = date('Y-m-d');
        $timestmp = date('Y-m-d H:i');
        //check data from table transaksi detail tmp
        $cekDetBarang = mysqli_query($conn, "SELECT * FROM tbtransaksidetail_tmp 
                                    WHERE idBarangDetail = '$_GET[id]' 
                                    AND createdBy = '$_SESSION[loginUser]'") or die(mysqli_error($conn));
        $sqlDB = mysqli_fetch_assoc($cekDetBarang);

这是将新数据插入MySQL

的代码部分
if($sqlDB == TRUE) {
            if (empty($_GET['jumlahKgYgInginDibeli'])) {
                $jumlahKgYgDibeli = $sqlDB['jumlahKgYgDibeli'];
                if ($jumlahKgYgDibeli > $assoc['jumlahKg']) {
                    mysqli_query($conn, "UPDATE tbtransaksidetail_tmp SET jumlahKgYgDibeli = jumlahKgYgDibeli + '$_GET[jumlahKgYgInginDibeli]' WHERE idBarangDetail = '$_GET[id]' AND createdBy = '$_SESSION[loginUser]'") or die(mysqli_error($conn));
                } else {
                    flash('example_message', '<p>Jumlah Kg yang Ingin Dibeli tidak Boleh Melebihi Jumlah Max Kg Barang.</p>', 'w3-red');
                }
            } elseif (!empty($_GET['jumlahKgYgInginDibeli'])) {
                $jumlahKgYgInginDibeli = $_GET['jumlahKgYgInginDibeli'];
                $jumlahKgYgDibeli = $sqlDB['jumlahKgYgDibeli'] + $jumlahKgYgInginDibeli;
                echo $jumlahKgYgDibeli;
                echo $assoc['jumlahKg'];
                if ($jumlahKgYgDibeli < $assoc['jumlahKg']) {
                    mysqli_query($conn, "UPDATE tbtransaksidetail_tmp SET jumlahKgYgDibeli = jumlahKgYgDibeli + '$_GET[jumlahKgYgInginDibeli]' WHERE idBarangDetail = '$_GET[id]' AND createdBy = '$_SESSION[loginUser]'") or die(mysqli_error($conn));
                } else {
                    flash('example_message', '<p>Jumlah Kg yang Ingin Dibeli tidak Boleh Melebihi Jumlah Max Kg Barang.</p>', 'w3-red');
                }
            }
        }
        else
        {
            if (!empty($_GET['jumlahKgYgInginDibeli'])) {
                if($_GET['jumlahKgYgInginDibeli'] != $assoc['KgPerBarang']) {
                    mysqli_query($conn, "INSERT INTO tbtransaksidetail_tmp (idTransaksiDetailtmp,
                                                                idBarangDetail,
                                                                jumlahKgYgDibeli,
                                                                hargaPerKg,
                                                                diskon,
                                                                createdBy,
                                                                dateCreated,
                                                                timestmp)
                                                        VALUES('$idTransaksiDetailtmp',
                                                                '$_GET[id]',
                                                                '$_GET[jumlahKgYgInginDibeli]',
                                                                '$b[hargaPerKg]',
                                                                '$disc',
                                                                '$_SESSION[loginUser]',
                                                                '$tgl',
                                                                '$timestmp')") or die(mysqli_error($conn));
                } elseif ($_GET['jumlahKgYgInginDibeli'] == $assoc['KgPerBarang']) {
                    mysqli_query($conn, "INSERT INTO tbtransaksidetail_tmp (idTransaksiDetailtmp,
                                                                idBarangDetail,
                                                                jumlahKgYgDibeli,
                                                                hargaPerBarang,
                                                                diskon,
                                                                createdBy,
                                                                dateCreated,
                                                                timestmp)
                                                        VALUES('$idTransaksiDetailtmp',
                                                                '$_GET[id]',
                                                                '$_GET[jumlahKgYgInginDibeli]',
                                                                '$b[hargaPerBarang]',
                                                                '$disc',
                                                                '$_SESSION[loginUser]',
                                                                '$tgl',
                                                                '$timestmp')") or die(mysqli_error($conn));
                }

            } else {
                if ($assoc['jumlahKg'] == $assoc['KgPerBarang']) {
                    mysqli_query($conn, "INSERT INTO tbtransaksidetail_tmp (idTransaksiDetailtmp,
                                                                idBarangDetail,
                                                                jumlahKgYgDibeli,
                                                                hargaPerBarang,
                                                                diskon,
                                                                createdBy,
                                                                dateCreated,
                                                                timestmp)
                                                        VALUES('$idTransaksiDetailtmp',
                                                                '$_GET[id]',
                                                                '$assoc[jumlahKg]',
                                                                '$b[hargaPerBarang]',
                                                                '$disc',
                                                                '$_SESSION[loginUser]',
                                                                '$tgl',
                                                                '$timestmp')") or die(mysqli_error($conn)); 
                } elseif ($assoc['jumlahKg'] != $assoc['KgPerBarang']) {
                    mysqli_query($conn, "INSERT INTO tbtransaksidetail_tmp (idTransaksiDetailtmp,
                                                                idBarangDetail,
                                                                jumlahKgYgDibeli,
                                                                hargaPerKg,
                                                                diskon,
                                                                createdBy,
                                                                dateCreated,
                                                                timestmp)
                                                        VALUES('$idTransaksiDetailtmp',
                                                                '$_GET[id]',
                                                                '$assoc[jumlahKg]',
                                                                '$b[hargaPerKg]',
                                                                '$disc',
                                                                '$_SESSION[loginUser]',
                                                                '$tgl',
                                                                '$timestmp')") or die(mysqli_error($conn));

                }
            }
        }
        header("location:../../../med.php?mod=penjualan");

    }
    else
    {
        echo"Tidak ada barang!";
    }

}

我使用noTransaksiDetail()函数我使用此代码:

function noTransaksiDetail()
{
    global $conn;
    $sql = mysqli_query($conn, "SELECT MAX(RIGHT(idTransaksiDetail,5)) AS notrans FROM tbtransaksidetail WHERE dateCreated = '".date('Y-m-d')."'");
    $m = mysqli_fetch_assoc($sql);

    $no = 0;
    if($m['notrans'] <> NULL)
    {
        $kd = number_format($m['notrans'],0) + 1;
        if(strlen($kd) == 1)
        {
            $no = "BDTD".date('dmy')."0000".$kd;
        }
        elseif (strlen($kd) == 2) {
            $no = "BDTD".date('dmy')."000".$kd;
        }
        elseif (strlen($kd) == 3) {
            $no = "BDTD".date('dmy')."00".$kd;
        }
        elseif (strlen($kd) == 4) {
            $no = "BDTD".date('dmy')."0".$kd;
        }
        else {
            $no = "BDTD".date('dmy').$kd;
        }
    }
    else
    {
        $no = "BDTD".date('dmy')."00001";
    }

    return $no;
}
有人可以帮帮我吗?非常感谢你

有人可以帮我解决此案吗?非常感谢你

1 个答案:

答案 0 :(得分:0)

这里有一些问题,而且我没有时间讨论SQL最佳做法,但这个错误看起来像是$timestmp = date('Y-m-d H:i');中的拼写错误我认为你的意思{{ 1}}

在您的函数中,您正在寻找$timestmp = date('Y-m-d');

的时间戳

无论哪种方式,我都会检查date('Y-m-d');的返回值。我怀疑这是你遇到问题的地方。