0

i have a php code that read from text file an compare the user input with its content.

the problem is that the system read from the text file but doesn't compare

what is the error and is their another method?

testfile.txt

test admin people bob321 danyjd ajb

code:

<?php

if(isset($_POST["Search"]))
{
    $data= file('testfile.txt');
    $accessData = array();
    foreach($data as $line){
        list($dataFile1) = explode(',', $line);

    }

    $dataInput = isset($_POST['name'])? $_POST['name']:'';

    if(array_key_exists($dataInput, $accessData)){
        echo "text exist";
    }
    else{
        echo "text doesn't exist";
    }
}
?>
<html>
    <head>

    </head>
    <body>
     <form action="test2.php" method="post">
          <p>enter your string <input type ="text"  id = "idName"  name="name" /></p>
          <p><input type ="Submit" name="Search" /></p>
    </form>
    </body>
</html>
10
  • 4
    $accessData is an empty array... Commented Nov 24, 2017 at 9:49
  • 2
    list($dataFile1) shouldn't that be list($accessData)? Commented Nov 24, 2017 at 9:50
  • @chade_ i don't think so be cause the $dataFile1 is the variable that handle the compared string Commented Nov 24, 2017 at 9:53
  • Shouldn't it be $accessData[] instead of list($dataFile1)? Using list here feels redundent Commented Nov 24, 2017 at 9:53
  • @WillParky93 still does't compare correctly Commented Nov 24, 2017 at 9:54

1 Answer 1

2

You can do it like this, using array_search(), reducing the check to 1 line :/

<?php
if (isset($_POST["Search"]) && !empty($_POST['name']))
{
    if (array_search($_POST['name'], file('testfile.txt')) !== false){
        echo "text exist";
    }
    else{
        echo "text doesn't exist";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.