如果数据库中存在PHP str_shuffle,请再次执行str_shuffle

时间:2015-05-18 05:20:26

标签: php mysql

我有一个表单中的shuffle字符串代码并将其插入数据库。但是在插入之前它并没有首先向数据库确认。我想确定数据库中是否存在shuffle字符串结果。这是代码:

    <?php 

            include "phpqrcode/qrlib.php";

            session_start();

            /* Connect to Database*/
            $dbc = mysqli_connect('localhost', 'root', '', 'delivery');

            $id_transaksi = $_SESSION['idtransaksi'];
            $alamat = $_POST['alamat'];
            $kurir = $_POST['kurir'];
            $penerima = $_POST['penerima'];

            $count = "SELECT MAX(id_pengiriman) as max_id_pengiriman FROM pengiriman";

            $result = mysqli_query($dbc, $count)
                or die('Error select query');

            if (empty($result)) {
                $id_pengiriman = 1;
            }
            else {
                $data = mysqli_fetch_assoc($result);
                $id_pengiriman = $data['max_id_pengiriman'] + 1;
            }

            $hash = md5('$id_transaksi');

/* Shuffle a string */
            $length = 5;
            $kode_transaksi = substr(str_shuffle($hash), 0, $length);
/* End of shuffle */

            $query = "INSERT INTO pengiriman (id_pengiriman, id_transaksi, kode_transaksi, id_kurir, alamat, penerima) ".
                "VALUE ('$id_pengiriman' ,'$id_transaksi', '$kode_transaksi', '$kurir', '$alamat', '$penerima')";

            /* Insert into database */
            $result = mysqli_query($dbc, $query)
                or die('Error insert into database');

            $upd_query = "UPDATE transaksi SET  status =  'Transit' WHERE  id_transaksi = $id_transaksi";

            /*Update Status in table transaksi*/
            $result = mysqli_query($dbc, $upd_query)
                or die('Error Update Table');

            /*Enkripsi kode transaksi*/

            QRcode::png("$kode_transaksi", "images/konfirmasi.png", "L", 12, 4);

            $_SESSION['kode_transaksi'] = $kode_transaksi;

            header('location: sendmail.php');

            ?>

我想: 如果字符串存在于数据库中,则会再次填充字符串,如果存在,则添加$lenght并重试。

请帮帮我怎么做!

0 个答案:

没有答案