0
I have an error when consulting the id in a table, I am developing a system of friendship between users and the error is as follows:
Pdoexception: SQLSTATE[23000]: Integrity Constraint Violation: 1062 Duplicata du Champ '0' pour la clef 'PRIMARY' in C: wamp www sllap php amisade.php on line 14
Behold:
$is_my_friend = DB::getConn()->prepare('SELECT * FROM `friends` WHERE (de=? AND para=?) OR (para=? AND de=?) LIMIT 1');
$is_my_friend->execute(array($idDaSessao,$idExtrangeiro,$idDaSessao,$idExtrangeiro));
if($is_my_friend->rowcount()==0){
echo'<a class="addFriend" href="php/amisade.php?ac=addfriend&de='.$idDaSessao.'¶='.$idExtrangeiro.'">Adicionar aos amigos</a>';
}else{
$astatusfriend = $is_my_friend->fetch(PDO::FETCH_ASSOC);
if($astatusfriend['status']==0){
echo'<a class="deleteFriend" href="php/amisade.php?ac=cancel&id='.$astatusfriend['id'].'&de='.$idDaSessao.'¶='.$idExtrangeiro.'">cancelar solicitação enviada</a>';
}
}
Page amisade.php:
include('../classes/DB.class.php');
if(''<>$_GET['ac']){
if($_GET['ac']=='addfriend'){
$is_my_friend = DB::getConn()->prepare('SELECT * FROM `friends` WHERE (de=? AND para=?) OR (para=? AND de=?)');
$is_my_friend->execute(array($_GET['de'],$_GET['para'],$_GET['de'],$_GET['para']));
if($is_my_friend->rowCount()==0){
$addfriend = DB::getConn()->prepare('INSERT INTO `friends` SET `de`=?,`para`=?');
$addfriend->execute(array($_GET['de'],$_GET['para']));
header('Location: ../profile.php?uid='.$_GET['para']);
}
}
}
if($_GET['ac']=='cancel'){
$is_my_friend = DB::getConn()->prepare('SELECT * FROM `friends` WHERE (de=? AND para=?)OR(para=? AND de=?) LIMIT 1');
$is_my_friend->execute(array($_GET['de'],$_GET['para'],$_GET['de'],$_GET['para']));
if($is_my_friend->rowCount()==1){
$addfriend = DB::getConn()->prepare('DELETE FROM `friends` WHERE `id`=?');
$addfriend->execute(array($_GET['id']));
header('Location: ../profile.php?uid='.$_GET['para']);
}
}
check if PK is auto increment, the message is that you are with a duplicated PK or FK key in the database
– SneepS NinjA
@Renan was that the problem was worth you are the master :)
– David