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

时间:2017-11-23 06:24:25

标签: php mysql 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');

2 个答案:

答案 0 :(得分:2)

下面的代码将帮助您插入唯一数据:

$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')");
} 

答案 1 :(得分:0)

tambahkan kolom dengan主键

ALTER TABLE gathering ADD COLUMN `id` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST