如何防止在一个表的两列中插入相同的值?

时间:2019-05-30 17:00:33

标签: php mysql mysqli

我正在尝试解决以下问题:在插入前将 插入前 列值partnerCodepartnerCodeOwner1username进行比较下表中,如果partnerCode and partnerCodeOwner1的值与username相同,则会出现错误消息,并且插入操作不应完成! 我的尝试低于但不正确!

$db = mysqli_connect('localhost', '*******', '********', '***********');


      $user_check_query = "SELECT partnerCodeOwner1 FROM refer where  randomfield='$partnerCode'";
  $result = mysqli_query($db, $user_check_query);
  $row = mysqli_fetch_array($result, MYSQLI_ASSOC);



// --------------------HERE I'm trying to check for the same value in columns--------
//But incorrect! Syntax error?

    if ($row) { // compare columns But incorrect
    if ($row['username'] === ['partnerCodeOwner1']) {
      array_push($errors, "U can't invest to yourself");
    }




    if(empty($row['partnerCodeOwner1'])) {
        $query = "update refer 
                    set partnerCodeOwner1= '" . $_SESSION['username'] . "'  
                where randomfield='$partnerCode' ;";
    }else {
        $query = "update refer 
                    set partnerCode= '" . $_SESSION['username'] . "'  
                where randomfield='$partnerCode'  ";        
    }

    mysqli_query($db, $query);
}       
?>

表中的示例

表: refer

  ID      randomfield    username      partnerCode    partnerCodeOwner1  
+-------+ ------------+-------------+-----------------+-----------+
|   1   | A45scdv     |  John       |  Not same       |    John   |
+-------+-------------+-------------+-----------------+-----------+
|   2   | das654d     | Alex        |  Alex           | Not same  |
+-------+-------------+-------------+-----------------+-----------+

0 个答案:

没有答案