www.webdeveloper.com
Results 1 to 7 of 7

Thread: Message displaying issue.

  1. #1
    Join Date
    Jul 2012
    Location
    McCune, KS
    Posts
    3

    Question Message displaying issue.

    Hiyas. I'm having a simple issue concerning displaying a message based off of an if statement in conjunction with HTML and MySQL. Here's the code that I'm using;

    PHP Code:
    <?php
                
    if($_POST) {
                    
    $password $_POST['password'];
                    
    $confirm $_POST['confirm'];
                    if(
    $password != $confirm() { 
            
    ?>
            
        <span style="color: Red;">Error: Passwords do not match!</span>
        
            <?php } else {
                        
    // include the database info to connect to mySQL
                        
    require_once 'dbconfig.php';
                
                        
    $conn mysql_connect($dbhost,$dbuser,$dbpass)
                            or die (
    'Error connecting to database');
                        
                        
    // select the database
                        
    mysql_select_db($dbname);
                
                
                        
    $query springf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('$s')",
                            
    mysql_real_escape_string($_POST['username']));
                    
                        
    $result mysql_query($query);
                
                        list(
    $count) = mysql_fetch_row($result);
                
                        if(
    $count >= 1) { ?>
                
                        <span style="color: Red;">Oops, that username is already taken!</span>
                
            <?php    } else {
                        
    $query sprintf("INSERT INTO users(username,password) VALUE ('$s','$s');",
                            
    mysql_real_escape_string($_POST['username']),
                            
    mysql_real_escape_string(md5($password)));
                                    
                        
    mysql_query($query);
                                    
                
    ?>
                
                <span style="color: Green;">Congratulations, you registered successfully!</span>
                
    <?php
                
    }
            }
        }
    ?>
    And a screenshot of what happens to my webpage;

    A picture at imgur...

    Any reason why this is occurring? Or am I just going about this wrong?

  2. #2
    Join Date
    Aug 2006
    Posts
    1,907
    Code:
    if($password != $confirm() {
    What's that left paren doing?

    Dave

  3. #3
    Join Date
    Jul 2012
    Location
    McCune, KS
    Posts
    3

    Unhappy Meh...

    A typo. Fixed it, but it didn't solve anything.

  4. #4
    Join Date
    Jul 2012
    Location
    McCune, KS
    Posts
    3
    I may have something wrong, I'm going to check the MySQL part of things and see if that changes anything.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    Start by checking whether or not $result is false, and if it is, do some query debugging.

    Similarly, any place else you depend on some value to determine what happens next, echo or error_log() that value to find out if it's really what you expect it to be.

    So forth and so on until you find the point where things are broken. (Though it's almost always a good idea to check query responses, since you're effectively leaving PHP at that point and depending on the result of an external process, and thus you may need to implement some sort of error-handling at that point just in case.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Aug 2009
    Posts
    79
    What I'd suggest doing is using echo's to display the html instead of closing the php tag and then reopening it because the html is being displayed wither the conditions in php are met or not
    PHP Code:
    <?php 
                
    if($_POST) { 
                    
    $password $_POST['password']; 
                    
    $confirm $_POST['confirm']; 
                    if(
    $password != $confirm) { 
             
        echo 
    "<span style=\"color: Red;\">Error: Passwords do not match!</span>";
                            } else { 
                        
    // include the database info to connect to mySQL 
                        
    require_once 'dbconfig.php'
                 
                        
    $conn mysql_connect($dbhost,$dbuser,$dbpass
                            or die (
    'Error connecting to database'); 
                         
                        
    // select the database 
                        
    mysql_select_db($dbname); 
                 
                 
                        
    $query springf("SELECT COUNT(id) FROM users WHERE UPPER(username) = UPPER('$s')"
                            
    mysql_real_escape_string($_POST['username'])); 
                     
                        
    $result mysql_query($query); 
                 
                        list(
    $count) = mysql_fetch_row($result); 
                 
                        if(
    $count >= 1) {
                 
                        echo 
    "<span style=\"color: Red;\">Oops, that username is already taken!</span>";
                 } else { 
                        
    $query sprintf("INSERT INTO users(username,password) VALUE ('$s','$s');"
                            
    mysql_real_escape_string($_POST['username']), 
                            
    mysql_real_escape_string(md5($password))); 
                                     
                        
    mysql_query($query); 
                                     echo 
    "<span style=\"color: Green;\">Congratulations, you registered successfully!</span>";
                } 
            } 
        } 
    ?>

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    Quote Originally Posted by Yokiest View Post
    What I'd suggest doing is using echo's to display the html instead of closing the php tag and then reopening it because the html is being displayed wither the conditions in php are met or not
    ...
    Err...negatory.
    PHP Code:
    <?php
    if(== 1) { ?>
      <h1>Hello, World.</h1>
    <?php
    }
    if(
    == 2) {
    ?>
      <h2>Goodbye, cruel world.</h2> <!-- does not get output -->
    <?php
    }
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles