CodeIgniter Forums
Problems with mysql syntax? I don't think so... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problems with mysql syntax? I don't think so... (/thread-48592.html)



Problems with mysql syntax? I don't think so... - El Forum - 01-22-2012

[eluser]selman555[/eluser]
Hi everyone,

I'm having some trouble making a login form for my site.

I already have a fully working registration form (data goes to the database and back successfully (included in code))
I have a user controller "user" that looks like this: (read the comments in the code for explenations).
Code:
function register()
{
    //form validations ... not needed for my question, except this one:
    $this->form_validation->set_rules('gebruikersnaam', 'Gebruikersnaam', 'xss_clean|trim|required|alpha_numeric|min_length[5]|max_length[30]|callback_usernameNotExists');
    //notice the callback_usernameNotExists.
    //also, my password has xss_clean enabled (so it's hashed in the database)

    $activatieCode = $this->randomString(10); //generating activationcode here

    $this->User_model->registerUser($gebruikersnaam, $paswoord, $naam, $voornaam, $email, $activatieCode); //registerUser
}

function usernameNotExists($gebruikersnaam) {
    $this->form_validation->set_message('usernameNotExists', "De gebruikersnaam '$gebruikersnaam' bestaat al, probeer een andere naam.");
    if ($this->User_model->checkExistsUsername($gebruikersnaam)) { //checkExistingUsername
        return false;
    } else {
        return true;
    }
}

//function for the login procedure
function log() {
    $this->form_validation->set_rules('paswoord', 'Paswoord', 'xss_clean|trim|required|alpha_numeric|min_length[6]|max_length[30]');
    if ($this->User_model->checkLogin($gebruikersnaam, $paswoord)) { //checkLogin
        $queryStr = "update Gebruikers set logged = 1 where Gebruikersnaam = ?";
$this->db->query($queryStr, $gebruikersnaam);
$this->index();
    } else {
$this->load->view('view_login');
    }
}
That's it for my controller
Now here's the usermodel "user_model".
Code:
function registerUser($gebruikersnaam, $paswoord, $naam, $voornaam, $email, $activatieCode) {
$secpas = sha1($paswoord);
$queryStr = "insert into gebruikers (Gebruikersnaam, Paswoord, Naam, Voornaam, Email, Activatiecode) VALUES (?, ?, ?, ?, ?, ?)";
$result = $this->db->query($queryStr, array($gebruikersnaam, $secpas, $naam, $voornaam, $email, $activatieCode));
}

function checkExistsUsername($gebruikersnaam) {
    $queryStr = "select Gebruikersnaam from gebruikers where Gebruikersnaam = ?";
    if ($this->db->query($queryStr, $gebruikersnaam)) {
return true;
    } else {
return false;
    }
}

function checkLogin($gebruikersnaam, $paswoord) {
    $queryStr = "select Gebruikersnaam from gebruikers where Gebruikersnaam = ? and Paswoord = ?";
    if ($this->db->query($queryStr, $gebruikersnaam, $paswoord)) {
return false;
    } else {
return true;
    }
}

So, the registration part is working (almost the same mysql syntax as login(???)).
I get this standard error message (1064):
Code:
A database error occurred
Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? and Paswoord = ?' at line 1

select Gebruikersnaam from gebruikers where Gebruikersnaam = ? and Paswoord = ?

Filename: C:\xampp\htdocs\CodeIgniter\E-Cards\system\database\DB_driver.php

Line Number: 330



Problems with mysql syntax? I don't think so... - El Forum - 01-22-2012

[eluser]scottwire[/eluser]
You have to pass them in as an array like you did in the registerUsers function

Code:
if ($this->db->query($queryStr, array($gebruikersnaam, $paswoord))) {



Problems with mysql syntax? I don't think so... - El Forum - 01-23-2012

[eluser]selman555[/eluser]
[quote author="scottwire" date="1327248325"]You have to pass them in as an array like you did in the registerUsers function

Code:
if ($this->db->query($queryStr, array($gebruikersnaam, $paswoord))) {
[/quote]

Thanks for your reply, unfortunetely, that's didn't solve it either.
I've made an array just like you said, but still, the data is not being processed by my phpmyadmin mysql database (it does work when registering)

Any more ideas?


Problems with mysql syntax? I don't think so... - El Forum - 01-28-2012

[eluser]TheFuzzy0ne[/eluser]
Can you repost your code and the error please? I find it odd that it should work sometimes as opposed to not at all.