Android Question help with php

tufanv

Expert
Licensed User
Longtime User
Hello

I need help for a basic php command because i don't understand php well. Currectly in my php code i have

B4X:
    case "1":

        $cihaz = clean($_GET["girdi3"]);
        $uname = clean($_GET["girdi1"]);
        $passw = clean($_GET["girdi2"]);
       
        $q = "SELECT username FROM tblusers WHERE username = '".$uname."'";
        $r = mysql_query($q);
       
        if ( mysql_num_rows($r) > 0 )
        {
            print json_encode('This username already exists');
        } else {
            $q = mysql_query("INSERT INTO tblusers (username,password,cihazid) VALUES ('$uname',  '$passw', '$cihaz')");
            print json_encode("Inserted");
        }
        break;

i send the parameters from my app as girdi1,girdi2 and girdi3. This works well for a registeration process. What I want to add is , I have a different table called tblblocked . I also want to check if the "girdi3" exists in the tblblocked. ( girdi 3 is the device id , so if the device id is in block list i dont want to accept the registration.) tblblocked has only 1 coloums and it is deviceid. So basicly i have to add after -username already exist check - if girdi3 is in the tblblocked under coloumn deviceid. Can you help me add it to code.

TY
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
something like

case "1":

$cihaz = clean($_GET[
"girdi3"]);
$uname = clean($_GET[
"girdi1"]);
$passw = clean($_GET[
"girdi2"]);

$q =
"SELECT username FROM tblusers WHERE username = '".$uname."'";
$r = mysql_query($q);

if ( mysql_num_rows($r) > 0 )
{
print json_encode(
'This username already exists');
} else {

$q = "SELECT deviceid FROM tblblocked WHERE deviceid = '".$cihaz."'";

$block = mysql_query($q);

if ( mysql_num_rows($block ) > 0 )
{
print json_encode('This id is blocked!');
} else {
$q = mysql_query("INSERT INTO tblusers (username,password,cihazid) VALUES ('$uname', '$passw', '$cihaz')");
print json_encode("Inserted");
}

}
break;
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Add here a query to check if id is blocked. Only if it is not blocked you write the registration
I tried to add it before seeing your example but iam sure i forgot some )'s or some charachters. . it is like :

B4X:
    case "1":

        $cihaz = clean($_GET["girdi3"]);
        $uname = clean($_GET["girdi1"]);
        $passw = clean($_GET["girdi2"]);

        $q = "SELECT username FROM tblusers WHERE username = '".$uname."'";
        $r = mysql_query($q);

        if ( mysql_num_rows($r) > 0 )
        {
            print json_encode('This username already exists');
        } else {
            $q = "SELECT deviceid FROM tblblocked WHERE deviceid = '".$cihaz."'";
            $r = mysql_query($q);
            if ( mysql_num_rows($r) > 0 )
            {
                print json_encode('id blocked');
            } else {


            $q = mysql_query("INSERT INTO iddaausers (username,password,cihazid) VALUES ('$uname',  '$passw', '$cihaz')");
            print json_encode("Inserted");
        }
        break;

if it is tottaly wrong i will try to see your example and implement it
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
atfer this TWO } are expected

Now , this code is giving internal error 500 after i added the two } .

B4X:
    case "1":

        $cihaz = clean($_GET["girdi3"]);
        $uname = clean($_GET["girdi1"]);
        $passw = clean($_GET["girdi2"]);

        $q = "SELECT username FROM tblusers WHERE username = '".$uname."'";
        $r = mysql_query($q);

        if ( mysql_num_rows($r) > 0 )
        {
            print json_encode('This username already exists');
        } else {
            $q = "SELECT deviceid FROM tblblocked WHERE deviceid = '".$cihaz."'";
            $r = mysql_query($q);
            if ( mysql_num_rows($r) > 0 )
            {
                print json_encode('blocked');
            } else {


            $q = mysql_query("INSERT INTO tblusers (username,password,cihazid) VALUES ('$uname',  '$passw', '$cihaz')");
            print json_encode("Inserted");
        }
         }
           }
        break;

the one i did not modify works ok still. which is :

B4X:
    case "1":

        $cihaz = clean($_GET["girdi3"]);
        $uname = clean($_GET["girdi1"]);
        $passw = clean($_GET["girdi2"]);

        $q = "SELECT username FROM tblusers WHERE username = '".$uname."'";
        $r = mysql_query($q);

        if ( mysql_num_rows($r) > 0 )
        {
            print json_encode('This username already exists');
        } else {
            $q = mysql_query("INSERT INTO tblusers (username,password,cihazid) VALUES ('$uname',  '$passw', '$cihaz')");
            print json_encode("Inserted");
        }
        break;

can there be a type mistake or stg like that in the first one ?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
are you sure
PHP:
mysql_query("INSERT INTO tblusers (username,password,cihazid) VALUES ('$uname',  '$passw', '$cihaz')");
works???
it is not working now but it worked one time when i add the post in this forum as "this works" obviously i am not seeing a big mistake . You are right it is not working now. What is the problem.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
is cihazid a numeric or text field? drop the single quotes if it is a number.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
is cihazid a numeric or text field? drop the single quotes if it is a number.
it is a number, i tried to remove them still no luck. I also use the same field within another app and it works with the single quotes. I think there is another problem
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I have other coloumns in the table but they all have default values so can it be the problem. ? Because now i tried to remove the columns with the default values which i am not using in the php , and it works now
 
Upvote 0

Descartex

Well-Known Member
Licensed User
Longtime User
try making echo of the sql statement and paste it on the phpMyAdmin to see where the mysql engine points
PHP:
mysql_query("INSERT INTO tblusers (username,password,cihazid) VALUES ('$uname',  '$passw', '$cihaz')");
PHP:
echo "INSERT INTO tblusers (username,password,cihazid) VALUES ('".$uname."', '".$passw."', '".$cihaz".')";
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
try making echo of the sql statement and paste it on the phpMyAdmin to see where the mysql engine points
PHP:
mysql_query("INSERT INTO tblusers (username,password,cihazid) VALUES ('$uname',  '$passw', '$cihaz')");
PHP:
echo "INSERT INTO tblusers (username,password,cihazid) VALUES ('".$uname."', '".$passw."', '".$cihaz".')";
I think i found the problem. I have a userid coloumn. set to primary. It makes the problem. I just send paramters for useranem password and cihazid. other coloumns have default values so they are addded automaticly. but my primary key (userid) is not incremented and added automaticly and i think taht makes the problem.
 
Upvote 0

Descartex

Well-Known Member
Licensed User
Longtime User
You have to be careful with primary keys values, it can blast a sql statement and make you get much time lost finding the fault. At this time, the sql is simple (inserting 3 values) but if you work with huge statements, with cross selects, unions, etc it can be almost impossible to fix :D
See you!
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You have to be careful with primary keys values, it can blast a sql statement and make you get much time lost finding the fault. At this time, the sql is simple (inserting 3 values) but if you work with huge statements, with cross selects, unions, etc it can be almost impossible to fix :D
See you!
Thanks. the mistake was i did not select the Auto increment while adding the coloumn. now works perfect !
 
Upvote 0
Top