...

View Full Version : PHP script does not appear to execute



ashelred
03-18-2010, 01:25 AM
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.


<!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_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);
$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>

masterofollies
03-18-2010, 02:09 AM
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.

ashelred
03-18-2010, 03:49 AM
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.

masterofollies
03-18-2010, 04:00 AM
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.

MattF
03-18-2010, 04:39 AM
Put, (without the trailing space after the [):



[ code]
your code goes here
[ /code]

ashelred
03-19-2010, 03:39 AM
Okay, I edited it the way you said.
Now, what else needs change to get this code executing?

masterofollies
03-19-2010, 05:10 AM
Not sure about this query, something seems wrong.


$query = 'SELECT * from defalt WHERE whensubmited == max(whensubmited)';

Rowsdower!
03-19-2010, 02:21 PM
Not sure about this query, something seems wrong.


$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.

masterofollies
03-19-2010, 06:02 PM
Also you are missing a single quote.


VALUES ('$fn', '$ln', '$age', '$hand', '$ot', '$ul', '$uo', '$em', $wb', NOW())";

the $wb needs a beginning quote.


VALUES ('$fn', '$ln', '$age', '$hand', '$ot', '$ul', '$uo', '$em', '$wb', NOW())";

I don't know if NOW() requires quotes or not.

ashelred
03-22-2010, 04:48 AM
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:


<!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_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);
$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>

Rowsdower!
03-22-2010, 02:17 PM
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):


//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.

ashelred
03-22-2010, 08:39 PM
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.

Rowsdower!
03-22-2010, 09:27 PM
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 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.

ashelred
03-23-2010, 01:02 AM
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 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.

Rowsdower!
03-23-2010, 04:59 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum