i'm creating php script insert rows database called orders based on shopping cart stored in associative array using sessional array $_session['cart']. database looks this:
orders ----------+--------------+-------------+-------------+-------------+ id | username | item1id | item2id | item3id | ----------+--------------+-------------+-------------+-------------+ 1 | a@aa.com | 8000001 | 8000002 | 800003 | ----------+--------------+-------------+-------------+-------------+ 5 | a@aa.com | 7000001 | 6000002 | 700003 | ----------+--------------+-------------+-------------+-------------+ 7 | b@bb.com | 8000001 | 8000002 | null | ----------+--------------+-------------+-------------+-------------+ 10 | a@aa.com | 3000001 | 1000002 | 800009 | ----------+--------------+-------------+-------------+-------------+
id column type char(20) may choose use letters later on.
as part of inserting row, need assign id (primary key) order row set 1 higher current highest id number found.
the whole script works perfectly; query finds highest id in table , increment 1 , assign variable use part of insert query. problem "select max(id) orders" can't seem find higher 9. there condition prevents select max(id) identifying in double digits?
i've got written like:
$highestid = mysqli_query($conn, "select max(id) orders"); $orderid = $highestid +1;
i've emptied database except id numbers1 , 2. running php script inserts new rows id numbers 3, 4, 5 except when gets 10, script unable produces error of having duplicate primary key of '10' (from $orderid's value). when manually entering row database id of '25', $orderid still returns '10' when echo out result.
i have not set specific limits amount of rows can entered or that.
you not need go through trouble auto-incremental pk. here's how can go it.
step 1 : in phpmyadmin, edit table, , check a_i
checkbox pk column.
step 2 : while inserting php, leave field blank. automatically assign value of current max + 1
pk.
eg,
$query = "insert mytable (id, name) values ('', 'name1'), ('', 'name2')";
edit : cannot have char(20)
pk , expect increment work btw.
Comments
Post a Comment