Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    70
    Thanks
    5
    Thanked 0 Times in 0 Posts

    if statement not working

    II have created a password change script one minor issue
    When i write a else statement it ignoes it? But if i take the else statement out it procceses it succesdfully.


    Heres my code (First big code ive written getting better

    I know The sql select query is a sql injection prone im going to chànge this once i get thè script working.

    Please Tweak This abit to get the else statements in (Been Working Hard Today)


    PHP Code:
    <?php
    session_start
    ();

    require 
    "connect.php";

    $oldpass $_POST['oldpass'];
    $newpass $_POST['newpass'];
    $compass $_POST['compass'];

    $username $_SESSION['username'];

    //create values as md5 for securiry

    $oldpassmd5 md5($_POST['oldpass']);
    $newpassmd5 md5($_POST['newpass']);
    $compassmd5 md5($_POST['compass']);


    $query mysql_query ("SELECT * FROM users WHERE username='$username'") or die ("SQL Error");

    $numrows = (mysql_num_rows($query));

    if (
    $numrows!=0)

    while (
    $row mysql_fetch_assoc($query))

    $dbpassword $row['password'];







    if (empty(
    $oldpass) && empty  ($newpass) && empty ($compass))
    die (
    "You Haven't Filled In The Required Fields");





    if (
    $oldpassmd5==$dbpassword)



    if (
    $compassmd5==$newpassmd5)


    $passchange mysql_query ("UPDATE users SET password='$newpassmd5' WHERE username='$username'") or die ("Error");
    if (
    $passchange==true)
    echo 
    "Password Changed";

    //otherwise if there not equal
    if ($oldpassmd5!=$dbpassword)
    echo 
    "Wrong Password";

    //otherwise statement should go here aswill

    if ($compass!=$newpass)
    echo 
    "Password Do Not Match";

    ?>
    <meta name='viewport' name='width'content='width=device-width' </
    Last edited by Spudster; 07-10-2012 at 03:00 AM.

  • #2
    Regular Coder Microsuck's Avatar
    Join Date
    Oct 2011
    Location
    127.0.0.1
    Posts
    123
    Thanks
    44
    Thanked 5 Times in 5 Posts
    Are you against the use of braces/curly brackets?
    PHP Code:
    <?php echo "Microsuck says hi!"?>

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,679
    Thanks
    25
    Thanked 654 Times in 653 Posts
    You never showed us the else that did not work. Here's one place it might have been:
    Code:
    if ($passchange == true){
    	echo "Password Changed";
    }else{
    	echo "Wrong Password";
    }

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Curly braces, tabbing, elseifs/else and &&/|| are all needed in that code block... Show us the else that failed, or we can't really debug anything... showing us working code does nothing...

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    something like this?

    PHP Code:
    <?php
    session_start
    ();
    require 
    "connect.php";
    $username $_SESSION['username'];

    if (empty(
    $_POST['oldpass']) && empty($_POST['newpass']) && empty($_POST['compass'])){
        die (
    "You Haven't Filled In The Required Fields");
    }
    else{
        if (
    $_POST['newpass'] == $_POST['compass']){
            
    $newpass md5($_POST['newpass']);
            
    $query mysql_query ("SELECT * FROM users WHERE username='$username'") or die ("SQL Error");
            if (
    mysql_num_rows($query) != 0){
                
    $row mysql_fetch_assoc($query);
                
    $dbpassword $row['password'];
            }
        }
        else{
            echo 
    "Password Do Not Match";
        }
    }

    if (
    $dbpassword == md5($_POST['oldpass'])){
        
    $passchange mysql_query ("UPDATE users SET password='$newpass' WHERE username='$username'") or die ("Error");
        if (
    $passchange==true){
            echo 
    "Password Changed";
        }
        else{
        echo 
    "Wrong Password";
        }
    }else{

    // your else statement here

    }
    ?>
    Last edited by Arcticwarrio; 07-10-2012 at 07:17 PM. Reason: missed a bit

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by Spudster View Post
    When i write a else statement it ignoes it?
    There is no else section of code in the code you have supplied.
    I can't really think of anything to write here now...

  • #7
    New Coder
    Join Date
    Jul 2012
    Posts
    70
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Oh didnt know that curly brackets was for that

    That probably why it didnt work, learnt something new

    Thanks for your help.

    This seems like a great communnity.

    I managed to get the else statements working by adding the if contion now i can take that out and replace it with curly brackets.

    Internet exployer is the worst browser i will remove that and add $_POST instead.


    Getting to fixing bugs..

  • #8
    New Coder
    Join Date
    Jul 2012
    Posts
    70
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Reply

    Another question:

    Is it posdible to loop though all errors and display everything the users done wrong?

    It makes it alot more user friendly.

  • #9
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    yes,

    post your latest code and i'll take a look for you

  • #10
    Regular Coder jdswebservice's Avatar
    Join Date
    Aug 2010
    Location
    Moon Township, PA
    Posts
    107
    Thanks
    0
    Thanked 5 Times in 5 Posts
    I'll also chime in on this as well! I'm interested in keeping up to date with this. looks like a fun project!
    Jonathan R.
    Email: jonathan@logicaldevelopment.net
    Skype, Aim, & Jabber: logicaldevelopment
    Website: http://logicaldevelopment.net/

    NOTE: When contacting me please have the following things ready:

    Thread ID (This can be found in the URL of your thread)
    Budget of Project
    Methods of contacting you/your organization
    Preferred method if many methods are provided

    Any website information that you PM, Email, IM, or otherwise is strictly confidential and will not be disseminated, distributed, or copied in any way, shape or form.

  • #11
    New Coder
    Join Date
    Jul 2012
    Posts
    70
    Thanks
    5
    Thanked 0 Times in 0 Posts
    This is going to be a great project once im done

    I will post the new code when tommorow.

    And Im adding A field for users Websites how can i post hem as hyperlinks? so any with admintalk.net or http:// will show up as a Link.

    Thanks Alot,

    Spudster

  • #12
    Regular Coder jdswebservice's Avatar
    Join Date
    Aug 2010
    Location
    Moon Township, PA
    Posts
    107
    Thanks
    0
    Thanked 5 Times in 5 Posts
    I could just do this project for you and then have you look at the code, but it wouldnt be as fun.

    If you are having users submit stuff to a database, you have to loop through the database and then pull out the information you need, sort it, and throw it back at the user using echo.

    Functions that will be helpful to you are,

    for loop
    echo (duh)
    mysql_connect()

    Listing data bases, etc

    the site that i personally learned from is this one. Its been around for 10+ years

    http://www.tizag.com/mysqlTutorial/index.php

    This will teach you all you need to know about syntax, and everything else.

    Have fun! happy coding!

    J-
    Jonathan R.
    Email: jonathan@logicaldevelopment.net
    Skype, Aim, & Jabber: logicaldevelopment
    Website: http://logicaldevelopment.net/

    NOTE: When contacting me please have the following things ready:

    Thread ID (This can be found in the URL of your thread)
    Budget of Project
    Methods of contacting you/your organization
    Preferred method if many methods are provided

    Any website information that you PM, Email, IM, or otherwise is strictly confidential and will not be disseminated, distributed, or copied in any way, shape or form.

  • #13
    New Coder
    Join Date
    Jul 2012
    Posts
    70
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Thanks You could write some codes out for me And i have a look how its done To code it myself its a good way of learning..

    And im going to have A look at that site

  • #14
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by jdswebservice View Post
    the site that i personally learned from is this one. Its been around for 10+ years

    http://www.tizag.com/mysqlTutorial/index.php

    This will teach you all you need to know about syntax, and everything else.
    +1

    The only thing I disliked about tizag is its tutorials about joining in mysql. That was very confusing.. I ended up on another site for that.
    I can't really think of anything to write here now...

  • #15
    New Coder
    Join Date
    Jul 2012
    Posts
    70
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Tizag have okld tutorial that ive seen before..


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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