If ANY $ _POST is equal to another $ _POST die ('error') ;?

I am trying to get this form:

if any $ _POST vars is equal to any other of $ _POST vars, throws an error.

If it was just a few, this is not a problem, but I have about 20 or so therefore, if I wanted to do this, I would have to go like

    <?php 
    if($_POST['input1']==$_POST['input2'] || $_POST['input1']==$_POST['input3']){
die('whatever');
}

    ?>

But this is not good coding (and it will take forever) I thought about arrays and in different ways ...

but I'm a dead brain, so I thought I could help. thanks in advance

ps it would be nice to do this in PHP (server side), but jQuery is always an option.

+3
source share
8 answers
function testPost(){
foreach ($_POST as $keya=>$vala){
    foreach($_POST as $keyb=>$valb){
        if ($keya==$keyb){
            continue;
        } else {
            if ($vala == $valb){
                 return FALSE;
            }
        }
    }
}
return TRUE;
}
+1
source

array_unique() , :

if($_POST != array_unique($_POST))
    die("...");
+16

if ($ _POST == array_unique ($ _POST)) {}

+2

zebediah49. :

$post = array_value($_POST);
$count = count($post);
for ($i = 0; $i < $count; ++$i) {
    for ($j = $i + 1; $j < $count; ++$j) {
        if ($post[$i] == $post[$j]) {
            die();
        }
    }
}

. , O (2 * n) O (n ^ 2) ( O). , array_values.

+1
$postValues = array_values($_POST);

if (array_unique($postValues)) != $postValues) {
    die('error!');
}

,

0

, ; O (n ^ 2),

foreach($_POST as $keya=>$vala) {
    foreach($_POST as $keyb => $valb) {
        if($vala == $valb && $keya != $keyb) {
            die('whatever');
        }
    }
}
0

:

$count_array = array_count_values($_POST);
foreach($_POST as $post) {
  if($count_array[$post] >1 ) {
    die();
 }
}
0

... ,

loans will be sent to @akellehe because it was closest to my end result ....

works great

$titles=array();
        $num=1;
        while($num!=15){
        $set1='title'.$num;
        $set2=$_REQUEST["title$num"];
        $titles[$set1]=$set2;
        unset($set1);
        unset($set2);
        $num+=1;
        }
        foreach($titles as $d => $p){
            foreach($titles as $e =>$q){
            if($p==$q && $d!=$e){
            if(!empty($p)){
            $_SESSION['error']='Duplicates not allowed!';
            }
            }
            }
        }
0
source

Source: https://habr.com/ru/post/1761786/


All Articles