问题:我正在向表logs
中插入数据,但重要的是我需要停止在表中重复名称。
这是我的桌子长什么样
table: logs
-------------------
id | name | amount
-------------------
1 | John | 500
2 | John | 500
-------------------
此表的问题是它重复了数据,我需要停止重复名称
解决方案:避免数据重复
在我的代码中,我叫此名称(如果存在),则数据将不会被插入,否则将被删除,请参见下面的代码
$connection = mysqli_connect("localhost","servername","");
$db = mysqli_select_db($connection, 'dbname');
$qrQuery = "SELECT * FROM scratch_cards WHERE code='$qrid' ";
$qrQuery_run = mysqli_query($connection,$qrQuery);
if (isset($_POST['qrname'])) {
$txtQrvalue = $qrRow['amount'];
while($qrRow = mysqli_fetch_array($qrQuery_run)){
//Check if the data exists
$exist = $num['firstname'];
if ($exist == 0) {
//DELETE THE INSERT QR INFO
$qrQuery5 = "DELETE FROM qrlogs WHERE id = $id";
$qrQuery_run5 = mysqli_query($connection,$qrQuery5);
echo 'Data already exists';
}else{
//INSERT THE INFO WHEN THE NAME IS NOT DUPLICATE
$qrQuery4 = "INSERT INTO qrlogs (cabin_crew,time_scan,qr_amount) VALUES ('".$num['firstname']."',NOW(),'$txtQrvalue')";
$qrQuery_run4 = mysqli_query($connection,$qrQuery4);
}
}
}
答案 0 :(得分:0)
这解决了您的代码中的问题-但是在您的代码中没有理由明确说明重复的原因-但是-如果仍然存在问题,请发布更多代码
if ($exist == 0) {
//DELETE THE INSERT QR INFO
$qrQuery5 = "DELETE FROM qrlogs WHERE id = $id";
// .. TAKE OUT .. $qrQuery_run5 = mysqli_query($connection,$qrQuery5);
echo 'Data already exists';
}else{
//INSERT THE INFO WHEN THE NAME IS NOT DUPLICATE
$qrQuery4 = "INSERT INTO qrlogs (cabin_crew,time_scan,qr_amount) VALUES ('".$num['firstname']."',NOW(),'$txtQrvalue')";
$qrQuery_run4 = mysqli_query($connection,$qrQuery4);
}
您的唯一性测试如下
$exist = $num['firstname'];
看起来不太可靠-但是如果没有更多代码,就无法说出问题所在