Ajax & PHP & Database

Get help using Construct 2

Post » Sat May 04, 2013 10:34 am

[QUOTE=lanceal] try without adding those ' characters to the ajax string. I have a rather complex login using ajax and get. What kept messing me up was exactly that.
[/QUOTE]

I did that, but still not working
B
18
S
6
G
3
Posts: 197
Reputation: 4,129

Post » Sat May 04, 2013 2:05 pm

Really silly question, but this line here
$con=mysqli_connect("localhost","dbuser","dbpass","database");

Those are not your actual values correct?
B
68
S
21
G
15
Posts: 701
Reputation: 15,604

Post » Sat May 04, 2013 2:10 pm

[QUOTE=lanceal] Really silly question, but this line here
$con=mysqli_connect("localhost","dbuser","dbpass","database");

Those are not your actual values correct?[/QUOTE]

Correct! you wouldn't post them in a forum ;-)
B
18
S
6
G
3
Posts: 197
Reputation: 4,129

Post » Sat May 04, 2013 2:35 pm

Maybe your query statement is wrong? Here is a copy of one of the functions in the library I'm using.

public function getUserNotes($suids, $CID, $page, $limit){
    //$result = $this->mysqli->prepare("SELECT * FROM `Contacts` WHERE `uid` = $suids ORDER BY `Next_Contact` ASC ");
    $result = $this->mysqli->query("SELECT * FROM `Notes` WHERE `UID` = $suids AND `Contact_ID` = $CID ORDER BY `Date` DESC LIMIT $page , $limit");
    $rows = array();
    while($row = $result->fetch_assoc()){
             $rows[] = $row;
    }
    return $rows;
}

Notice I'm not using any ' or seperating variable from the string with " and . marks. Although for good measure I am using backticks ` to wrap around the table and reference points. Here is a copy of my login function as well. Maybe you can pull some ideas from it.

public function login($username, $password)
     {
          $return = array();
          
          if($this->isBlocked($_SERVER['REMOTE_ADDR']))
          {
               $return['code'] = 0;
               return $return;
          }
          else
          {
               if(strlen($username) == 0) { $return['code'] = 1; $this->addAttempt($_SERVER['REMOTE_ADDR']); return $return; }
               elseif(strlen($username) > 30) { $return['code'] = 1; $this->addAttempt($_SERVER['REMOTE_ADDR']); return $return; }
               elseif(strlen($username) < 3) { $return['code'] = 1; $this->addAttempt($_SERVER['REMOTE_ADDR']); return $return; }
               elseif(strlen($password) == 0) { $return['code'] = 1; $this->addAttempt($_SERVER['REMOTE_ADDR']); return $return; }
               elseif(strlen($password) != 40) { $return['code'] = 1; $this->addAttempt($_SERVER['REMOTE_ADDR']); return $return; }
               else
               {
                    $plainpass = $password;
                    $password = $this->getHash($password);
                    
                    if($userdata = $this->getUserData($username))
                    {
                         if($password === $userdata['password'])
                         {
                              if($userdata['isactive'] == 1)
                              {
                                   $sessiondata = $this->addNewSession($userdata['uid']);

                                   $return['code'] = 4;
                                   $return['session_hash'] = $sessiondata['hash'];
                                   
                                   $this->addNewLog($userdata['uid'], "LOGIN_SUCCESS", "User logged in. Session hash : " . $sessiondata['hash']);
                                   
                                   return $return;
                              }
                              else
                              {
                                   $this->addAttempt($_SERVER['REMOTE_ADDR']);
                              
                                   $this->addNewLog($userdata['uid'], "LOGIN_FAIL_NONACTIVE", "Account inactive");
                              
                                   $return['code'] = 3;
                                   
                                   return $return;
                              }
                         }
                         else
                         {
                              $this->addAttempt($_SERVER['REMOTE_ADDR']);
                         
                              $this->addNewLog($userdata['uid'], "LOGIN_FAIL_PASSWORD", "Password incorrect : {$plainpass}");
                         
                              $return['code'] = 2;
                              
                              return $return;
                         }
                    }
                    else
                    {
                         $this->addAttempt($_SERVER['REMOTE_ADDR']);
                    
                         $this->addNewLog("", "LOGIN_FAIL_USERNAME", "Attempted login with the username : {$username} -> Username doesn't exist in DB");
                    
                         $return['code'] = 2;
                         
                         return $return;
                    }
               }
          }
     }
B
68
S
21
G
15
Posts: 701
Reputation: 15,604

Post » Sat May 04, 2013 6:58 pm

[QUOTE=yonda]
My login.php

<?php
$username = $_GET['fname'];
$password = $_GET['fpass'];
$con=mysqli_connect("localhost","dbuser","dbpass","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
<br>    $qz = "SELECT id FROM members where username='".$username."' and password='".$password."'" ;
$qz = str_replace("\'","",$qz);
$result = mysqli_query($con,$qz);
<br>    while($row = mysqli_fetch_array($result))
{
echo $row['id'];
}
<br>    mysqli_close($con);
?>

plz help[/QUOTE]

Those <br> tags shouldn't be there anyway. I see they're in the tutorial code too - must be a copy-paste error.
ramones2013-05-04 18:58:47
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Sat May 04, 2013 7:37 pm

[QUOTE=ramones] [QUOTE=yonda]
My login.php

<?php
$username = $_GET['fname'];
$password = $_GET['fpass'];
$con=mysqli_connect("localhost","dbuser","dbpass","database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
<br>    $qz = "SELECT id FROM members where username='".$username."' and password='".$password."'" ;
$qz = str_replace("\'","",$qz);
$result = mysqli_query($con,$qz);
<br>    while($row = mysqli_fetch_array($result))
{
echo $row['id'];
}
<br>    mysqli_close($con);
?>

plz help[/QUOTE]

Those <br> tags shouldn't be there anyway. I see they're in the tutorial code too - must be a copy-paste error.
[/QUOTE]

Nice that was the Error

Thx you are my hero for today ;-)
B
18
S
6
G
3
Posts: 197
Reputation: 4,129

Previous

Return to How do I....?

Who is online

Users browsing this forum: theosciandra and 25 guests