SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    UK
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating a database with php and mysql

    --------------------------------------------------------------------------------

    Hi, i was wondering if you might be able to help me. I have made a file called db.php. This connects to my database. I just include this at the top of my script. It works fine and it works with my insert script.

    I have the following code that supply's a link that you click to activate an account.

    <?
    //openpopup.php
    $title = $_GET['title'];
    $firstname = $_GET['fname'];
    $surname = $_GET['lname'];
    $userid = $_GET['id'];
    $db_password = $_GET['code'];
    $username = $_GET['username'];
    $random_password = $_GET['password'];

    echo "Dear $title $firstname $surname,<br>

    Thank you for registering at our website!<br>

    You are two steps away from logging in and accessing our exclusive members area!<br>

    To activate your membership, please click the link at the bottom of the pagebr>

    Once you activate your memebership, you will be able to login with the following informationbr>

    Username: $username <br>
    Password: $random_password <br>


    Thanks!<br>
    The Webmaster";
    ?>
    <?
    echo "<br><br>";
    echo "<a href=http://localhost/Master%20Accommodation/activate.php?id=$userid&code=$db_password>Activate Now!</a>";
    ?>

    The script send the variables to active.php, where it activates the account and updates activate in the database from 0 to 1. But when i run the script is always says can't activate the account. The variables are being sent as there in the URL when i checked.

    <?
    //activate.php
    // Get database connection
    include 'db.php';

    // Create variables from URL
    $userid = $_REQUEST['id'];
    $db_password = $_REQUEST['code'];

    $sql = mysql_query("UPDATE users SET activated='1' WHERE userid='$userid' AND password='$db_password'");


    $sql_doublecheck = mysql_query("SELECT * FROM users WHERE userid='$userid' AND password='$db_password' AND activated='1'");
    $doublecheck = mysql_num_rows($sql_doublecheck);

    if($doublecheck == 0){
    echo "<strong><font color=red>Your account could not be activated!</font></strong>";
    } elseif ($doublecheck > 0) {
    echo "<strong>Your account has been activated!</strong><br />";
    }

    ?>

    Any idea's would be great.

    Many thanks

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    The code looks fine at a quick glance so put this line as your query
    PHP Code:
    $sql mysql_query("UPDATE users SET activated='1' WHERE userid='$userid' AND password='$db_password'") or die(mysql_error()); 
    and see if mysql has a problem somewhere along the line.

    Spike

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    UK
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thank you very much for your reply.

    I tried that and i receive no MySql error, just the error message i created, where it says an account can't be created.

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    OK, If the variables are being sent through the url, take out one of the WHERE clauses and see if it makes a difference
    PHP Code:
    $sql mysql_query("UPDATE users SET activated=1 WHERE userid='"$userid."'"); 
    Spike

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    UK
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It comes up with the same reply................................

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    try...
    PHP Code:
    $userid $_REQUEST['id'];
    $db_password $_REQUEST['code'];

    $sql mysql_query("UPDATE users SET activated='1' WHERE userid='"$userid."'");
    if(
    mysql_affected_rows($sql) == 0) {

    echo 
    "<strong><font color=red>Your account could not be activated!</font></strong>";
    } else {
    echo 
    "<strong>Your account has been activated!</strong><br />";

    and see if that changes anything

  7. #7
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    UK
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in C:\Program Files\Apache Group\Apache2\htdocs\Masters Accommodation\activate.php on line 12

    That was the results, not sure what it means, lol

  8. #8
    SitePoint Zealot
    Join Date
    Oct 2005
    Location
    Los Angeles
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the problem might be in your query:
    password='$db_password'

    password is reserved word. Thry to rename the field or use backtick:
    `password`='$db_password'

    i hope it helps...

  9. #9
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    The error messgae basically means the query didn't execute properly as it bugged out somewhere.
    run...
    PHP Code:
    $userid $_REQUEST['id']; 
    $db_password $_REQUEST['code']; 

    $sql mysql_query("UPDATE users SET activated='1' WHERE userid='"$userid."'")or die(mysql_error()); 
    if(
    mysql_affected_rows($sql) == 0) { 

    echo 
    "<strong><font color=red>Your account could not be activated!</font></strong>"
    } else { 
    echo 
    "<strong>Your account has been activated!</strong><br />"

    See what it says

  10. #10
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    UK
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get the same error message, but when i checked the database it had done the update.

  11. #11
    SitePoint Zealot
    Join Date
    Oct 2005
    Location
    Los Angeles
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just do not display warnings

  12. #12
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Vadim22
    just do not display warnings
    Yup, agree with that!
    If it works then redirect the user away to another page

  13. #13
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mysqlvirgin
    The script send the variables to active.php, where it activates the account and updates activate in the database from 0 to 1. But when i run the script is always says can't activate the account. The variables are being sent as there in the URL when i checked.
    PHP Code:
    <?
    //activate.php
    // Get database connection
    include 'db.php';

    // Create variables from URL
    $userid $_REQUEST['id'];
    $db_password $_REQUEST['code'];

    $sql "UPDATE users SET activated='1' WHERE userid='".$userid."' && password='".$db_password."'";

    $result mysql_query($sql)or die(mysql_error());

    if(
    $result) {

    $check "SELECT * FROM users WHERE activated='1' && password='".$db_password."'";

    $check_query mysql_query($check)or die(mysql_error());

    $get_numbers mysql_num_rows($check_query)

    if(
    $get_numbers 0) {

    echo 
    "Your account has been activated!"

    } else {

    echo 
    "Sorry there was a problem with the Activation.";

    }
    ?>
    Try that code, and see what happens.

    If it is updating your database, then the connection's should be fine.

    The other option is that it may be something to do with your queries's, so i uncluded some mysql_error() functions so the problems will be shown to us if the queries are the fault. If there are any errors returned, make sure you tell us them when you post back here, they're really helpfull.

    Tip: use syntaxing in your posts.
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  14. #14
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    UK
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, thank you very much

  15. #15
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    another tip, try and avoid double posting!
    http://www.sitepoint.com/forums/showthread.php?t=334281

  16. #16
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ
    another tip, try and avoid double posting!
    http://www.sitepoint.com/forums/showthread.php?t=334281
    Indeed. The big bad people with Green and Orange badges will come and tell you off .

    Edit

    I just read that other post and noticed this problem has been posted and fixed already..
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  17. #17
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Next edit:
    Apologies to mysqlvirgin, just noticed that this thread was moved from the mysql forum into this one which gave the appearence of a double post.

    Spike

  18. #18
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,424
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    Threads merged.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •