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.
Results 1 to 15 of 15
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question PHP script does not appear to execute

    I am trying to make a PHP script to upload two graphics files from each user and to collect information about the users who are uploading these two graphics files. (We have multiple users, but each user will be uploading only two graphics files each time he or she logs on.)

    However, the script apparently fails to execute. Although the file ran, I do not get output of error messages and (more importantly) the uploaded data does not get added to the database (which is the main thing that this script is for).

    Here is my script. Please tell me where the problem is and suggest how to solve it. I can't find the problem, and neither can my business associates. We need to get this script running ASAP for a work-related data collection project.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>phpsubmit</title>
    <meta http-equiv="generator" content=
    "CoffeeCup HTML Editor (www.coffeecup.com)">
    <meta name="created" content="Wed, 17 Feb 2010 15:42:33 GMT">
    <meta http-equiv="content-type" content=
    "text/html; charset=us-ascii">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <?php function upload($var$file) {if (isset($_FILES[$var])) {
            
            
    // Validate the type. Should be jpeg, jpg, or gif.
            
    $allowed = array ('image/gif''image/jpeg''image/jpg''image/pjpeg');
            if (
    in_array($_FILES[$var]['type'], $allowed)) {
            
                
    // Move the file over.
                
    if (move_uploaded_file($_FILES[$var]['tmp_name'], $file)) {
                
                    echo 
    '<p>File uploaded successfully!</p>';
                
                } else { 
    // Couldn't move the file over.
                
                    
    echo '<p><font color="red">The file could not be uploaded because: </b>';
            
                    
    // Print a message based upon the error.

                    
    switch ($_FILES[$var]['error']) {
                        case 
    1:
                            print 
    'The file exceeds 512 kilobytes: the maximum file size for this program.';
                            break;
                        case 
    2:
                            print 
    'The file exceeds 512 kilobytes: the maximum file size for this form.';
                            break;
                        case 
    3:
                            print 
    'The file was only partially uploaded.';
                            break;
                        case 
    4:
                            print 
    'No file was uploaded.';
                            break;
                        case 
    6:
                            print 
    'No temporary folder was available.';
                            break;
                        default:
                            print 
    'A system error occurred.';
                            break;
                    } 
    // End of switch.
                    
                    
    print '</b></font></p>';

                } 
    // End of move... IF.
                
            
    } else { // Invalid type.
                
    echo '<p><font color="red">Please upload a JPEG or GIF image.</font></p>';
                
    unlink ($_FILES[$var]['tmp_name']); // Delete the file.
            
    }

        } else { 
    // No file uploaded.
            
    echo '<p><font color="red">Please upload a JPEG or GIF image smaller than 512KB.</font></p>';}
        }; 
    ?>
    </head>
    <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=
    "#800080" alink="#FF0000">

    <?php 
    if ($_POST['submitted']) {
      
    $errors[] = array(); //Initialize error array
      
    echo 'start';
      
    //check for firstname
      
    if (empty($_POST['firstname'])) {
        
    $errors[] = 'You forgot to enter your first name.';
              } else {
        
    $fn trim($_POST['firstname']);
        }
      
    //check for lastname
      
    if (empty($_POST['lastname'])) {
        
    $errors[] = 'You forgot to enter your last name or initial.';
      } else {
        
    $ln trim($_POST['lastname']);
        } 
      
    //check for age
      
    if (empty($_POST['age'])) {
        
    $errors[] = 'You forgot to enter your age.';
      } else {
        
    $age trim($_POST['age']);
        }
      
    //check for which hand
      
    if (empty($_POST['hand'])) {
        
    $errors[] = 'You forgot to enter which hand you use.';
      } else {
        
    $hand trim($_POST['Hand']);
        }
      
    //check for how long used Better Letters
      
    if (empty($_POST['uselong'])) {
        
    $errors[] = 'You forgot to enter how long you have used Better Letters.';
      } else {
        
    $ul trim($_POST['uselong']);
        } 
      
    //check for how often used Better Letters
      
    if (empty($_POST['useoften'])) {
        
    $errors[] = 'You forgot to enter how often you use Better Letters.';
      } else {
        
    $uo trim($_POST['useoften']);                                    
        } 
      
    //check for email
      
    if (empty($_POST['email'])) {
        
    $errors[] = 'You forgot to enter your e-mail address.';
      } else {
        
    $em trim($_POST['email']);
        }
      
    //check for web
      
    if (empty($_POST['web'])) {
        
    $wb NULL;
      } else {
        
    $wb trim($_POST['web']);
        }
        
    //check for comments
      
    if (empty($_POST['comments'])) {
        
    $cm NULL;
      } else {
        
    $cm trim($_POST['comments']);
        }
      if (
    $hand != 'other') {
        
    $ot 'NULL';
      } else {
        if (empty(
    $_POST['other'])) {
        
    $ot 'NULL';
        } else {
        
    $ot trim($_POST['other']);
      }}
      if (empty(
    $errors)) {// If everything is OK
    define('DB_USER''');
    define('DB_PASSWORD''');
    define('DB_HOST''');
    define('DB_NAME''');
    //Make the connection
    $dbc = @mysql_connect(DB_HOSTDB_USERDB_PASSWORD) OR die('Could not connect to MYSQL: ' mysql_error());
    //s Select Database
    @mysql_select_db(DB_NAME)OR die('Could not select database' mysql_error());
    // Make the query
    $query "INSERT INTO defalt (firstname, lastname, age, hand, other, uselong, useoften, comment, email, web, whensubmited) 
    VALUES ('$fn', '$ln', '$age', '$hand', '$ot', '$ul', '$uo', '$em', $wb', NOW())"
    ;
    $result = @mysql_query ($query); // Run the query
    if ($result) {//it ran OK
    //Find Id#
    $query 'SELECT * from defalt WHERE whensubmited == max(whensubmited)'
    $result = @mysql_query ($query);
    $row mysql_fetch_array($result);
    $i $row(0);
    //before file
    $filename 'BetterLetters/before' $i;
    //Check for "before" file
    upload ('before'$filename);
     
    //after file
     
    $filename 'BetterLetters/after' $i;
    upload ('after'$filename);
      } else {
      echo 
    '<p> the following error(s) occurred: <br />';
      foreach (
    $errors as $msg) {
        echo 
    " - $msg<br />\n";
      }}
      echo 
    '</p>' ;}  }; 
     
    ?>
     <p> Program ran</p>
    </body>
    </html>
    Last edited by ashelred; 03-18-2010 at 01:54 PM.

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Two things, REMOVE your username,password,database information. People can now login and steal your account.

    and second please post your code using the PHP code tags.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question

    I'm a real newbie -- I didn;t have to start learning code till two weeks ago, and I only found this forum today.

    How do I "post my code using the PHP code tags"? Assume that I know nothing.

  • #4
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Your database info is still there, you can edit your post and change it.

    To use code tags, you can edit your post and highlight the whole code and above where you type is a little icon looks like a piece of paper and says PHP on it, click that.

    Or else click it and then paste your coding.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Put, (without the trailing space after the [):

    Code:
    [ code]
    your code goes here
    [ /code]

  • #6
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Okay, I edited it the way you said.
    Now, what else needs change to get this code executing?

  • #7
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Not sure about this query, something seems wrong.

    PHP Code:
    $query 'SELECT * from defalt WHERE whensubmited == max(whensubmited)'
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #8
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by masterofollies View Post
    Not sure about this query, something seems wrong.

    PHP Code:
    $query 'SELECT * from defalt WHERE whensubmited == max(whensubmited)'
    The spelling is incorrect for "default" and "whensubmitted", but if this matches the actual database then that wouldn't be the problem...

    If you aren't sure that the script is running try inserting debugging statements (such as 'print "This is a debugging statement<br />\n";') and see where they stop appearing. Usually you'd want to start these just after entering a loop or inside of an "if" statement.

    If you do this and find out that all of your code is all running as you expected then I would guess that you most likely have a problem with matching your database information (for table names/column names/etc.) between your actual database and your PHP code. Start checking row counts for 'select' queries and then check all spelling and SQL names for the 'insert' queries to make sure that they are complete and correct.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #9
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Also you are missing a single quote.

    PHP Code:
    VALUES ('$fn''$ln''$age''$hand''$ot''$ul''$uo''$em'$wbNOW())"; 
    the $wb needs a beginning quote.

    PHP Code:
    VALUES ('$fn''$ln''$age''$hand''$ot''$ul''$uo''$em''$wb'NOW())"; 
    I don't know if NOW() requires quotes or not.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #10
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Okay, I'm posting the way the program looks now. I put in three debugging statements. When I try to run it, only the first debugging statement prints out. So the problem is that the "if" fails, and then does not go to the "else" where I have an error code for it.
    Here is the code:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>phpsubmit</title>
    <meta http-equiv="generator" content=
    "CoffeeCup HTML Editor (www.coffeecup.com)">
    <meta name="created" content="Wed, 17 Feb 2010 15:42:33 GMT">
    <meta http-equiv="content-type" content=
    "text/html; charset=us-ascii">
    <meta name="description" content="">
    <meta name="keywords" content="">
    <?php function upload($var$file) {if (isset($_FILES[$var])) {
            
            
    // Validate the type. Should be jpeg, jpg, or gif.
            
    $allowed = array ('image/gif''image/jpeg''image/jpg''image/pjpeg');
            if (
    in_array($_FILES[$var]['type'], $allowed)) {
            
                
    // Move the file over.
                
    if (move_uploaded_file($_FILES[$var]['tmp_name'], $file)) {
                
                    echo 
    '<p>File uploaded successfully!</p>';
                
                } else { 
    // Couldn't move the file over.
                
                    
    echo '<p><font color="red">The file could not be uploaded because: </b>';
            
                    
    // Print a message based upon the error.

                    
    switch ($_FILES[$var]['error']) {
                        case 
    1:
                            print 
    'The file exceeds 512 kilobytes: the maximum file size for this program.';
                            break;
                        case 
    2:
                            print 
    'The file exceeds 512 kilobytes: the maximum file size for this form.';
                            break;
                        case 
    3:
                            print 
    'The file was only partially uploaded.';
                            break;
                        case 
    4:
                            print 
    'No file was uploaded.';
                            break;
                        case 
    6:
                            print 
    'No temporary folder was available.';
                            break;
                        default:
                            print 
    'A system error occurred.';
                            break;
                    } 
    // End of switch.
                    
                    
    print '</b></font></p>';

                } 
    // End of move... IF.
                
            
    } else { // Invalid type.
                
    echo '<p><font color="red">Please upload a JPEG, PNG or GIF image.</font></p>';
                
    unlink ($_FILES[$var]['tmp_name']); // Delete the file.
            
    }

        } else { 
    // No file uploaded.
            
    echo '<p><font color="red">Please upload a JPEG, PNG or GIF image smaller than 512KB.</font></p>';}
        }; 
    ?>
    </head>
    <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink=
    "#800080" alink="#FF0000">

    <?php 
    print "This is debugging statement 1<br />\n"
    if (
    $_POST['submitted']) {
        print 
    "This is debugging statement 2 <br />\n";
      
    $errors[] = array(); //Initialize error array
      //check for firstname
      
    if (empty($_POST['firstname'])) {
        
    $errors[] = 'You forgot to enter your first name.';
              } else {
        
    $fn trim($_POST['firstname']);
        }
      
    //check for lastname
      
    if (empty($_POST['lastname'])) {
        
    $errors[] = 'You forgot to enter your last name or initial.';
      } else {
        
    $ln trim($_POST['lastname']);
        } 
      
    //check for age
      
    if (empty($_POST['age'])) {
        
    $errors[] = 'You forgot to enter your age.';
      } else {
        
    $age trim($_POST['age']);
        }
      
    //check for which hand
      
    if (empty($_POST['hand'])) {
        
    $errors[] = 'You forgot to enter which hand you use.';
      } else {
        
    $hand trim($_POST['Hand']);
        }
      
    //check for how long used Better Letters
      
    if (empty($_POST['uselong'])) {
        
    $errors[] = 'You forgot to enter how long you have used Better Letters.';
      } else {
        
    $ul trim($_POST['uselong']);
        } 
      
    //check for how often used Better Letters
      
    if (empty($_POST['useoften'])) {
        
    $errors[] = 'You forgot to enter how often you use Better Letters.';
      } else {
        
    $uo trim($_POST['useoften']);                                    
        } 
      
    //check for email
      
    if (empty($_POST['email'])) {
        
    $errors[] = 'You forgot to enter your e-mail address.';
      } else {
        
    $em trim($_POST['email']);
        }
      
    //check for web
      
    if (empty($_POST['web'])) {
        
    $wb NULL;
      } else {
        
    $wb trim($_POST['web']);
        }
        
    //check for comments
      
    if (empty($_POST['comments'])) {
        
    $cm NULL;
      } else {
        
    $cm trim($_POST['comments']);
        }
      if (
    $hand != 'other') {
        
    $ot 'NULL';
      } else {
        if (empty(
    $_POST['other'])) {
        
    $ot 'NULL';
        } else {
        
    $ot trim($_POST['other']);
      }}
      if (empty(
    $errors)) {// If everything is OK
    define('DB_USER''');
    define('DB_PASSWORD''');
    define('DB_HOST''');
    define('DB_NAME''');
    //Make the connection
    $dbc = @mysql_connect(DB_HOSTDB_USERDB_PASSWORD) OR die('Could not connect to MYSQL: ' mysql_error());
    //s Select Database
    @mysql_select_db(DB_NAME)OR die('Could not select database' mysql_error());
    // Make the query
    $query "INSERT INTO defalt (firstname, lastname, age, hand, other, uselong, useoften, comment, email, web, whensubmited) 
    VALUES ('$fn', '$ln', '$age', '$hand', '$ot', '$ul', '$uo', '$em', '$wb', NOW())"
    ;
    $result = @mysql_query ($query); // Run the query
    if ($result) {//it ran OK
    //Find Id#
    $query 'SELECT * from defalt WHERE whensubmited == max(whensubmited)'
    $result = @mysql_query ($query);
    $row mysql_fetch_array($result);
    $i $row(0);
    //before file
    $filename 'BetterLetters/before' $i;
    //Check for before file
    upload ('before'$filename);
     
    //after file
     
    $filename 'BetterLetters/after' $i;
    upload ('after'$filename);
      } else {
          print 
    "This is debugging statement 3. <br />\n";
      echo 
    '<p> the following error(s) occurred: <br />';
      foreach (
    $errors as $msg) {
        echo 
    " - $msg<br />\n";
      }}
      echo 
    '</p>' ;}  }; 
     
    ?>
     <p> Program ran</p>
    </body>
    </html>
    Last edited by ashelred; 03-22-2010 at 10:00 PM.

  • #11
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by ashelred View Post
    Okay, I'm posting the way the program looks now. I put in three debugging statements. When I try to run it, only the first debugging statement prints out. So the problem is that the "if" fails, and then does not go to the "else" where I have an error code for it.
    Here is the code:
    OK, if debugging statement 3 is not showing then that means that your "if" statement IS returning true and IS being entered. If not you would definitely see the 3rd debugging statement because there is essentially no questionable code inside of the "else" statement before that debugging statement is printed. What is most likely happening is that you have an error whose error reporting is being suppressed. You have four possible locations for this (starting at line 140 of your script):

    Code:
    //Make the connection
    $dbc = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) OR die('Could not connect to MYSQL: ' . mysql_error());
    //s Select Database
    @mysql_select_db(DB_NAME)OR die('Could not select database' . mysql_error());
    // Make the query
    $query = "INSERT INTO defalt (firstname, lastname, age, hand, other, uselong, useoften, comment, email, web, whensubmited) 
    VALUES ('$fn', '$ln', '$age', '$hand', '$ot', '$ul', '$uo', '$em', '$wb', NOW())";
    $result = @mysql_query ($query); // Run the query
    if ($result) {//it ran OK
    //Find Id#
    $query = 'SELECT * from defalt WHERE whensubmited == max(whensubmited)'; 
    $result = @mysql_query ($query);
    My advice would be to remove the "@" character from these locations and then re-run the script. The errors should then be reported and you can fix the problem(s) causing them. After that you can add the "@" characters back in, but until the page works as expected it's best not to suppress that error reporting. Give that a shot and let us know what you find.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #12
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Angry

    I removed the "@" symbols. After that, the program still failed to report debugging error 2 as well as debugging error 3. This suggests that the "if" statement did not execute and did not go to "else". Please tell me what could have caused this.

  • #13
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by ashelred View Post
    I removed the "@" symbols. After that, the program still failed to report debugging error 2 as well as debugging error 3. This suggests that the "if" statement did not execute and did not go to "else". Please tell me what could have caused this.
    I haven't checked over your entire code to make sure that you haven't mis-matched the brackets for the if/else, but trust me - if you have those right and the "if" fails you certainly will go to the "else" statement unless there is some "fatal error" - which would be reported to you as long as you haven't disabled error reporting in PHP. So I guess let's back up and try that first.

    Try adding this line of code at the VERY TOP of your file:
    PHP Code:
    <?php phpinfo(); ?>
    When you do this it tells the page to print a huge block of information about your PHP. Among the details will be whether error reporting is turned on or not. Look in the "Configuration, PHP Core" section. Check for the "display_errors" directive and make sure that the local value is set to "On" and then check to see if "error_reporting" is set to "6135." Let me know what you find.

    Also, you have posted your database connection information in the code again. At this point it may have been cached by google, making it potentially visible for a long time to come. I would advise you to remove that login information (and be sure not to post it again) and update your username/password for your database to avoid a serious security issue.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    ashelred (03-23-2010)

  • #14
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Rowsdower! View Post
    I haven't checked over your entire code to make sure that you haven't mis-matched the brackets for the if/else, but trust me - if you have those right and the "if" fails you certainly will go to the "else" statement unless there is some "fatal error" - which would be reported to you as long as you haven't disabled error reporting in PHP. So I guess let's back up and try that first.

    Try adding this line of code at the VERY TOP of your file:
    PHP Code:
    <?php phpinfo(); ?>
    When you do this it tells the page to print a huge block of information about your PHP. Among the details will be whether error reporting is turned on or not. Look in the "Configuration, PHP Core" section. Check for the "display_errors" directive and make sure that the local value is set to "On" and then check to see if "error_reporting" is set to "6135." Let me know what you find.

    Also, you have posted your database connection information in the code again. At this point it may have been cached by google, making it potentially visible for a long time to come. I would advise you to remove that login information (and be sure not to post it again) and update your username/password for your database to avoid a serious security issue.
    I followed above instructions. The "error_reporting" is set to "6135."
    What do I do now.

  • #15
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    If it were my script, I would insert some code that I KNOW is wrong and should throw an error (make a call to an undefined function, like <?php this_is_a_garbage_function(); ?>). I'd put this right at the top of the page (in place of the php_info() code that you just did) and see if I get an error message. If not, then the error reporting is still not active so we still can't see where your code is failing. The solution then would be to get error reporting turned on and/or check your error logs.

    My thinking is that there would have to be an error that terminates your code in order for your if/else to produce no debugging output at all. As far as I know PHP doesn't just skip if/else statements for the fun of it. So, if you actually do get an error message as expected after doing the above, then I would take care to examine all of the nested statements to make sure that there is no mis-match in opening/closing brackets.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    ashelred (03-23-2010)


  •  

    Tags for this Thread

    Posting Permissions

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