Jump to content

No errors, but the database isn't updating.


zombie

Recommended Posts

I've been following a tutorial on making a fairly simple login script. It's my first time really playing with php and sticking with it and so far so good. But I can't figure this one out.

 

It's using a fairly simple email activation thing but the activation link isn't working. It echos that it was successful, but the database hasn't been updated at all.  I double checked and the ID and activation code are both correct, but the 'group' is still set to 0.

 

Help?

 

<?php
$title = "Account Activation";

require_once('header.php');
$id = $_GET['id'];
$code = $_GET['code'];

if ($id&&$code)
{
$check = mysql_query("SELECT * FROM users WHERE id='$id' AND actcode='$code'");
$checknum = mysql_num_rows($check);

if ($checknum==1)
{

	//activate the account
	$activate = mysql_query("UPDATE users SET group='1' WHERE id='$id'");
	echo("Your account is activated. You may now log in.");
	require_once "footer.php";
}

else
	echo("Invalid ID or activation code.");
	require_once "footer.php";
	die();
}
else
echo("Data Missing!");
require_once "footer.php";
die();

require_once "footer.php";
?>

Link to comment
Share on other sites

If it is getting to the successful message then the first query is getting run successfully. So, if the record is not getting updated then the 2nd query is likely failing and you aren't checking if it failed or not.

 

But, the logic you have is flawed anyway. instead of running one query to check if the record exists and another to update the record, just run the query to update the record. Then check if any records were updated.

 

This fixes some of the logic and should tell you what the error is.

<?php

//Prepriocess GET data
$id   = isset($_GET['id'])   ? intval($_GET['id'])   : false;
$code = isset($_GET['code']) ? mysql_real_escape_string($_GET['code']) : false;

if (!$id || !$code)
{
    $output = "ID and/or Activation code not sent!";
}
else
{
    $query = "UPDATE users
              SET group='1'
              WHERE id='$id'
                AND actcode='$code'";
    $result = mysql_query($query);
    if(!$result)
    {
        $output = "Error running query";
        //This line for debugging only
        $output .= "<br>Query: {$query}<br>Error:<br>" . mysql_error();
    }
    elseif(mysql_affected_rows()==0)
    {
        $output = "Invalid ID or activation code.";
    }
    else
    {
        $output = "Your account is activated. You may now log in.";
    }
}

$title = "Account Activation";
require_once('header.php');
echo $output;
require_once "footer.php";

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.