Search code examples
phpdatabasepdoprepared-statementprepare

PHP SELECT * FROM not working correctly


I am having trouble with the following code:

# Connection:
      require('config.php');
      $MyConnection = new PDO('mysql:host=x;dbname=x', $dbusername, $dbpassword);
      $MyConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      # Collecting:
      $username = htmlspecialchars($_POST['username']);
      $password = htmlspecialchars($_POST['password']);
      $lastname = htmlspecialchars($_POST['lastname']);
      $email = htmlspecialchars($_POST['email']);

      # Checking:
      $findUser = $MyConnection->prepare("SELECT * FROM Users WHERE 
        Username = :username OR Email = :email LIMIT 2");
      $findUser->bindParam(':username', $username);
      $findUser->bindParam(':email', $email);
      $findUser->execute();
      $foundUser = $findUser->fetch(PDO::FETCH_OBJ);

      if($foundUser->Username == $username) {
        echo '
          <div id="pop-up" class="error">
          This username is already in use. Please choose another one.
          </div>
          ';
      }
      elseif ($foundUser->Email == $email) {
        echo '
          <div id="pop-up" class="error">
          This email address is already in use. Please choose another one. <br />
          If you think this is impossible, please
          <a href="http://www.askmephilosophy.co.nf/contact.php">contact us</a>.
          </div>
          ';

Whenever I try to register a new account I get sent to an error page, pre-set by the host. So I get a 404 error. I'm very sure the problem lays in the

$findUser = $MyConnection->prepare("SELECT * FROM Users WHERE Username = :username OR Email = :email LIMIT 2");

What am I doing wrong in that bit of code?


Solution

  • The 404 error is from a file not found.

    Such as

    <a href="http://www.askmephilosophy.co.nf/contact.php">contact us</a>.
    

    File http://www.askmephilosophy.co.nf/contact.php does not exist.

    There must be some other file that does not exist in some part of the code you don't show (in the else after elseif ?) . Could be called if both Username and Email are free.

    As most of links in http://www.askmephilosophy.co.nf cause 404 error you are heaping up trouble for yourself.

    All links should be to real files. Until the content is available these Stub files should have a message like "Under Construction" and name of file etc.