PHP help with mysql please

revin

New Member
Messages
11
Reaction score
0
Points
0
gah, it feels like i've been trying forever to do this

the table looks like this
65392250wq9.png


and the php code is here
PHP:
################################################################################
#------------------------------------------------------------------------------#
#  Activate Function
#------------------------------------------------------------------------------#
################################################################################
function Activate(){
DBconn();
$code=$_REQUEST['code'];
            $result = mysql_query("SELECT userid FROM users WHERE code='$code'");
             if(mysql_numrows($result) == 1){
             $row=mysql_fetch_row($result);
                   $query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid='$row[userid]'") or die('Could not connect: ' . mysql_error());
                   echo 'Your account is now active, feel free to login now';
             }else{
                   echo 'You are already activated or error in the database';
       
}
}

i'm not sure what i'm doing wrong here, i am pretty sure i got everything right

can i get another pair of eyes to look over my code because i'm at the end of my rope here

its supposed to check whatever variable is placed in my activation url

xxxx/users.php?maa=Activate&code=xxx

if it finds a match for xxx then it sets isactive to 1 and code to 0

but it isn't updating the mysql record
 

xmakina

New Member
Messages
264
Reaction score
0
Points
0
Here's a good rule when you're working with SQL.

Set the SQL query into a variable first.
If there is an error, echo that variable.
Use phpMyAdmin to debug the SQL error.

I've invaribly found that the answer was obvious once I'd seen the SQL output. A fine example is an error I was getting being cause by there being no space between the end of one AND and another. Would *never* have spotted that if I hadn't seen the final SQL that was going to the database.
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
try making
PHP:
$query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid='$row[userid]'") or die('Could not connect: ' . mysql_error());
into
PHP:
$query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid='".$row['userid']."'") or die('Could not connect: ' . mysql_error());
 

balaji2u

New Member
Messages
410
Reaction score
2
Points
0
Xplozion i think it would be like this isn't it!

PHP:
$query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid=$row['userid']") or die('Could not connect: ' . mysql_error());
 

revin

New Member
Messages
11
Reaction score
0
Points
0
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/revin/public_html/users.php on line 193
thats the error that comes up now

that was with this line

PHP:
$query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid=$row['userid']") or die('Could not connect: ' . mysql_error());

i looked at the database and

isactive is a TINYINT

and code is a VARCHAR
 

mephis

New Member
Messages
39
Reaction score
0
Points
0
Xplozion i think it would be like this isn't it!
PHP:
$query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid=$row['userid']") or die('Could not connect: ' . mysql_error());

nope, xplozion got it right the first time:
try making
PHP:
$query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid='$row[userid]'") or die('Could not connect: ' . mysql_error());
into
PHP:
$query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid='".$row['userid']."'") or die('Could not connect: ' . mysql_error());

you can't call an array element directly from within a string. you have to concatenate the array element like xplozion said.
alternatively, you can just use: "{$row['userid']}"

summarizing:
PHP:
// sample variables
$a = 1;
$b['id'] = 1;

echo "value of a is: $a"; // WORKS
echo "value of b['id'] is: ".$b['id']; // WORKS
echo "value of b['id'] is: {$b['id']}"; // WORKS
echo "value of b['id'] is: $b['id']"; // DOESN'T WORK!
 

revin

New Member
Messages
11
Reaction score
0
Points
0
PHP:
################################################################################
#------------------------------------------------------------------------------#
#  Activate Function
#------------------------------------------------------------------------------#
################################################################################
function Activate(){
DBconn();
$code=$_GET["code"];

            $result = mysql_query("SELECT userid FROM users WHERE code='".$code."'") or die('select failed' . mysql_error());
            $check= mysql_numrows($result) or die('Activation code is invalid');
             if($check == 1){
             $row=mysql_fetch_row($result);
                   $query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid='".$row['userid']."'") or die('Could not connect: ' . mysql_error());    
                   echo 'Your account is now active, feel free to login now, your user id is '.$row['userid'];
             }else{
                   echo 'You are already activated or error in the database';
       
}
}

code i'm using now shows that its not reading userid from the row :dunno:
 

mephis

New Member
Messages
39
Reaction score
0
Points
0
PHP:
################################################################################
#------------------------------------------------------------------------------#
#  Activate Function
#------------------------------------------------------------------------------#
################################################################################
function Activate(){
DBconn();
$code=$_GET["code"];

            $result = mysql_query("SELECT userid FROM users WHERE code='".$code."'") or die('select failed' . mysql_error());
            $check= mysql_numrows($result) or die('Activation code is invalid');
             if($check == 1){
             $row=mysql_fetch_row($result);
                   $query = mysql_query("UPDATE users SET isactive='1',code='0' WHERE userid='".$row['userid']."'") or die('Could not connect: ' . mysql_error());    
                   echo 'Your account is now active, feel free to login now, your user id is '.$row['userid'];
             }else{
                   echo 'You are already activated or error in the database';
       
}
}

code i'm using now shows that its not reading userid from the row :dunno:

hmmmm... I think that if you want to access the $row elements by their field name you need to use mysql_fetch_assoc() instead of mysql_fetch_row().
mysql_fetch_row() returns an indexed array, whereas mysql_fetch_assoc() returns an associative array.
read this:
http://www.php.net/manual/en/function.mysql-fetch-assoc.php
 

revin

New Member
Messages
11
Reaction score
0
Points
0
Woo!

thanks, that worked for me ^^

coding is frustrating sometimes x3
 

mephis

New Member
Messages
39
Reaction score
0
Points
0
not a problem... glad I could help :)
(and yes, it can be *very* frustrating sometimes)
 
Top