如何避免在php mysqli中插入重复记录?

如何避免在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')");
} 
上一篇:略过 Mysql 5.7的密码策略


下一篇:TP5:使用了INPUT函数来接收参数了,还需再过滤SQL注入吗