如何避免在PHP MYSQLi中插入重复记录?
这是我的脚本:
$nama=$_POST['nama'];
$member=$_POST['member'];
$peserta=$_POST['peserta'];
$tour=$_POST['tour'];
mysqli_query($conn,"insert into gathering (nama, member, peserta, tour) values ('$nama', '$member', '$peserta', '$tour')");
header('location:index.php');
解决方法:
以下代码将帮助您插入唯一数据:
$result = mysqli_query($conn, "SELECT nama, member, peserta, tour FROM gathering where nama = '$nama' and member = '$member' and peserta='$peserta' and tour='$tour'")
$total = mysqli_num_rows($result);
if($total==0)
{
mysqli_query($conn,"insert into gathering (nama, member, peserta, tour)
values ('$nama', '$member', '$peserta', '$tour')");
}