First, a general comment about all of the code: Find an indentation/whitespace style that you like and stick to it! Properly formatted code is much easier to read and analyze.
R an doml y spa c edcode , on th e other han d, i
s muc h ha
rder to re ad.
if ($_SESSION['login'] == "1"){
//if (!(isset($_SESSION['login']) && $_SESSION['login'] != '1')) {
// header ("Location: index.php");
//}
Note that the commented out version of the if() statement is actually closer to what you should be using than the first is.
If $_SESSION['login'] is undefined, the first if() statement is going to generate an E_NOTICE level error message. However, note that the second if() statement should have been written with a logical OR rather than an AND in order for it to make sense (how can something not be set and have a value?).
If you wanted to keep the header() statement in the 'else' branch below, note you could simply invert the logical expression in the if() statement.
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
Just because you know the user sent a POST request doesn't mean you can assume all of the fields you're expecting to use were actually contained in the POST'ed data. For that reason, I always define variables holding external data like so:
$email = isset($_POST['email']) ? $_POST['email'] : '';
This uses the ternary operator to set a default value (the empty string, here - could be any default value that is appropriate for the type of data you're expecting) as well as perhaps doing some general filtering of the incoming data (e.g. you could wrap the second $_POST['email'] with something like [man]intval/man/etc. if you were expecting numeric data).
$target = "pictures/";
$target = $target . basename( $_FILES['upload']['name']);
$pic= $_FILES ['upload']['tmp_name'];
Same comment here as above - you're assuming that $_FILES['upload'] exists; what if it doesn't?
if($email = ''){
Note here that you're assigning the empty string to the variable $email rather than comparing the two with each other.
$errorMessage = $errorMessage . "You must enter an Email." . "<BR>";
$errorMessage wasn't defined before this line, so attempting to append that error message to itself means you're trying to append a string to a variable that doesn't exist, which will generate an error message.
Speaking of error messages, you should have error_reporting set to E_ALL and either display_errors or log_errors enabled depending whether we're talking about your development or production environment, respectively. (And if you do, it would help us help you if you pasted all of the PHP error messages you're receiving, since you should at least be getting the one mentioned above.)
if(($_FILES["upload"]["type"] == "image/gif")
|| ($_FILES["upload"]["type"] == "image/jpeg")
|| ($_FILES["upload"]["type"] == "image/jpg")
|| ($_FILES["upload"]["type"] == "image/png" )
&& ($_FILES["upload"]["size"] < 25600))
Note the warning on the PHP manual page [man]features.file-upload.post-method[/man] in regards to the 'type' index:
PHP Manual wrote:This mime type is however not checked on the PHP side and therefore don't take its value for granted.
In other words, I could easily upload 'my_favorite_virus.exe' and simply lie to your script and claim it is of type "image/gif" rather than the malicious executable that it actually is.
If you really want to check if you've received a valid image, consider using [man]getimagesize[/man] or perhaps the [man]Fileinfo[/man] library of functions.
if(($_FILES["upload"]["type"] == "image/gif")
|| ($_FILES["upload"]["type"] == "image/jpeg")
|| ($_FILES["upload"]["type"] == "image/jpg")
|| ($_FILES["upload"]["type"] == "image/png" )
&& ($_FILES["upload"]["size"] < 25600)){
$errorMessage ="";
}
Here, you're clearing out the $errorMessage variable... but what if the e-mail was left blank and an error was generated above? If so, you've just lost that error message without ever handling it.
Instead, initialize $errorMessage earlier in the script and only append values to it if an error actually occurs; if there is no error at a particular step, do nothing with the variable's contents.
Speaking of appending strings, note that it's easier to use the ".=" concatenation operator, e.g.:
$string .= 'This string is being appended to $string\'s previous value.';
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found){
Note that if you truly want this code to be error-resistant, you might also want to first check to see if [man]mysql_connect[/man]() succeeded before you attempt to select a database.
$SQL = "UPDATE Accounts SET Email='$email', Picture='$pic' WHERE Username='$uname'";
User-supplied data should never be placed directly into a SQL query string, else your code will be vulnerable to SQL injection attacks and/or just plain SQL errors. Instead, you must first sanitize the data with a function such as [man]mysql_real_escape_string/man (for string data) or use prepared statements. See this manual page for more info: [man]security.database.sql-injection[/man].
$result = mysql_query($SQL);
echo "Success";
Why do you echo "Success" without checking to see if the SQL query was actually successful or not?
For one, you should always check to see if [man]mysql_query/man returned boolean FALSE (indicating an error has occurred) and, if so, outputting or logging (again, depending upon which environment you're working in) the error message returned by the MySQL server (see [man]mysql_error/man) and perhaps the SQL query itself (to aid in debugging).
else {
echo $errorMessage;
}
This echo statement will never produce any output. In addition, if $errorMessage actually contains an error message, your code will never output it.
Why? Note that the above 'else' statement matches up with the if ($db_found) statement which is wrapped inside of:
if ($errorMessage == "") {
Perhaps you meant for the 'else' statement to be paired with this latter if() statement?
Note that this problem would have been easily detected (or perhaps never would have occurred in the first place) if you had followed the advice I gave in #1 above. 😉
<html>
<body>
<center>
<FORM NAME='form1' METHOD ='POST' ACTION ='test2.php'>
<BR>
Email:
<input type='TEXT' Value='' Name='email'>
<BR>
<BR>
<input type='File' Value='Browse' Name='upload'>
<BR>
<BR>
<input TYPE = 'Submit' Name = 'Submit' VALUE = 'Submit'>
</center>
</FORM>
</body>
</html>
Yikes... once you fix the above errors, you might want to start working on writing valid HTML markup.