0

I have a code for shuffle string from form and insert it to database. But it doesn't confirm to database first before insert. i want to make sure if the shuffle string result is not exist in database. This is the code:

    <?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');

            ?>

i want: if string exist in database it suffle string again and if it is exist then add $lenght and try again.

Please, help me how to do it!

Mahfud Muhammad
  • 169
  • 1
  • 11
  • 1
    Just run a query to check. – Sougata Bose May 18 '15 at 05:22
  • [this may help you](http://stackoverflow.com/questions/30136457/how-do-you-make-sure-two-rows-selected-at-random-are-different-from-one-another/30136556#30136556) – pala_ May 18 '15 at 05:23
  • I knew how to do it if it is not exist. But i need to shuffle string again if exist and if all combination is already exist the length string is +1, you know what i mean? how to do the second condition? – Mahfud Muhammad May 18 '15 at 06:22

0 Answers0