1
我有一些PHP代碼將數據插入到從html表單收集的表中,它在查詢成功提交時自動分配一個唯一的ID。儘快從表中提取ID
目前我生成一個引用,但這是一個糟糕的解決方案,因爲它將最終有長期重複的條目...我如何拉唯一ID並將其發佈到echo語句中,我將不得不運行另一個查詢來獲取它?或者可以在這一塊代碼中完成?
if($fnameErr === null) {
$ref = rand(1, 1000);
$query = "INSERT INTO Taxi(fname, lname, contact, unit, snumber, sname, suburb, dsuburb, dt, ref, status) VALUES('{$fname}','{$lname}','{$contact}','{$unit}','{$snumber}','{$sname}','{$suburb}','{$dsuburb}','{$dt}','{$ref}','{$status}')";
if (mysqli_query($conn, $query)) {
$date = date_create($dt);
echo "Thank you! Your booking reference number is {$ref} you will be picked up in front of your provided address at " . date_format($date, 'g:ia \o\n l jS F Y') . ".";
}else{
echo "<br>Failed<br>";
echo mysqli_error($conn);
}
}
else {
echo "<br>Unable to add your booking.<br>";
}
可能重複的[mysqli最後插入ID](http://stackoverflow.com/questions/19738283/mysqli-last-insert-id) – Dekel
爲什麼你不只是自動增量,並使其獨特的表? –