Help with simple code

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
22eragon22
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Jul 09, 2020 8:05 am

Thu Jul 09, 2020 8:18 am

Yeah soo i dont know whats wrong with my code.
I've been trying to create simple registration mechanism but something went wrong.
I dont know how to take a row from my database and simply display it, ive been trying to do it for a while but im going crazy it just doesnt work.
Here i post my code and just in case all other files like connecting to database or posting the variables are working just fetching a row from database doesnt work.

Code: Select all


<?php

    session_start();
    
    if((isset($_POST['log'])) && (isset($_POST['pass'])))
    {
        if((!empty($_POST['log'])) && (!empty($_POST['pass'])))
        {
            require_once 'connect.php';

            $login = $_POST['log'];
            $password = $_POST['pass'];

            //echo $login.' '.$password;

            $q1 = $db->prepare("SELECT * FROM test_table WHERE 'login' = :login AND 'password' = :password");
            $q1->bindParam(':login', $login);
            $q1->bindParam(':password', $password);
            $q1->execute();

            $a1 = $q1->fetch(PDO::FETCH_ASSOC))
            {
                echo $a1['nick'];
                echo $a1['login'];
                echo $a1['password'];
            }

        }
        else
        {
            header('Location: index.html');
        }
    }
    else
    {
        header('Location: index.html');
    }


if you could point out in witch places in code i made mistakes i will be truly grateful
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 90
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Mon Jul 13, 2020 11:33 pm

First up, your SQL statement has incorrect quotes in it:

Code: Select all

"SELECT * FROM test_table WHERE login = :login AND password = :password"
The column names - login and password - should not have quotes around them. the statement:

Code: Select all

'login' = :login
will test if the value you pass for $login is equal to the actual word "login", rather than the value of the login column.
Post Reply