After many failed attempts to get the former implementation to work, I am trying to rework the code in a new format.
I understand that there might be many errors in my construction, but any helpful thoughts would be appreciated.
<?php
// sorry guessing here with this first line. Not sure if this is the appropriate way to construct the isset(). Not sure if && is the proper comparison.
if( isset( $_FILES['user_pic'] ) && isset( $_POST['username'] ) ) {
$username = isset( $_POST['username'] ) ? $_POST["username"] : '';
$gender = isset( $_POST['gender'] ) ? $_POST["gender"] : '';
$b_m = isset( $_POST['b_m'] ) ? $_POST["b_m"] : '';
$b_d = isset( $_POST['b_d'] ) ? $_POST["b_d"] : '';
$b_y = isset( $_POST['b_y'] ) ? $_POST['b_y'] : '';
$email1 = isset( $_POST['email1'] ) ? $_POST["email1"] : '';
$email2 = isset( $_POST['email2'] ) ? $_POST["email2"] : '';
$pass1 = isset( $_POST['pass1'] ) ? $_POST["pass1"] : '';
$pass2 = isset( $_POST['pass2'] ) ? $_POST["pass2"] : '';
$user_pic = isset( $_FILES['user_pic'] ) ? $_FILES["user_pic"] : '';
include_once "scripts/connect_to_mysql.php";
$emailCHecker = mysql_real_escape_string($email1);
$emailCHecker = str_replace("`", "", $emailCHecker);
$sql_uname_check = mysql_query("SELECT username FROM myMembers WHERE username='$username'");
$uname_check = mysql_num_rows($sql_uname_check);
$sql_email_check = mysql_query("SELECT email FROM myMembers WHERE email='$emailCHecker'");
$email_check = mysql_num_rows($sql_email_check);
$maxfilesize = 51200;
$errormessage = array();
if( empty( $username ) )
$errormessage[] = "Please enter a username";
if( empty( $gender ) )
$errormessage[] = "Please identify your gender";
if( empty( $b_m ) )
$errormessage[] = "Please select the month of your birth";
if( empty( $b_d ) )
$errormessage[] = "Please select the day of your birth";
if( empty( $b_y ) )
$errormessage[] = "Please select the year you were born";
if( empty( $email1 ) )
$errormessage[] = "Please enter your email address";
if( empty( $email2 ) )
$errormessage[] = "Please verify your email by re-entering it";
if( empty( $pass1 ) )
$errormessage[] = "Please enter a password";
if( empty( $pass2 ) )
$errormessage[] = "Please verify your password by re-entering it";
if( $email1 != $email2 )
$errormessage[] = "Your email entries do not match";
if( $pass1 != $pass2 )
$errormessage[] = "Your password entries do not match";
if( strlen( $username ) < 4 )
$errormessage[] = "Your username must be longer than 4 characters";
if( strlen( $username ) > 20 )
$errormessage[] = "Your username must be shorter than 20 characters";
if( $uname_check > 0 )
$errormessage[] = "Your username is already inside of our system";
if( $email_check > 0 )
$errormessage[] = "Your email is already inside of our system";
if( $_FILES['user_pic']['tmp_name'] != "" )
$errormessage[] = "You must upload an image";
unlink($_FILES['user_pic']['tmp_name']);
if( $_FILES['user_pic']['size'] > $maxfilesize )
$errormessage[] = "Your image is too large";
unlink($_FILES['user_pic']['tmp_name']);
if( !preg_match("/\.(gif|jpg|png)$/i", $_FILES['user_pic']['name'] ) )
$errormessage[] = "Your image is in an unacceptabe format";
unlink($_FILES['user_pic']['tmp_name']);
// sorry guessing here with this.
if( $_FILES['user_pic']['error'] == 4 )
$errormessage[] = "There was an error uploading your image";
unlink($_FILES['user_pic']['tmp_name']);
} else {
// Add MD5 Hash to the password variable
$db_password = md5($pass1);
// Convert Birthday to a DATE field type format(YYYY-MM-DD) out of the month, day, and year supplied
$full_birthday = "$b_y-$b_m-$b_d";
// GET USER IP ADDRESS
$ipaddress = getenv('REMOTE_ADDR');
// Add user info into the database table for the main site table
$sql = mysql_query("INSERT INTO myMembers (username, gender, birthday, email, password, ipaddress, sign_up_date)
VALUES('$username','$gender','$full_birthday','$email1','$db_password', '$ipaddress', now())")
or die (mysql_error());
$id = mysql_insert_id();
// Create directory to hold each user's files(pics, MP3s, etc.)
mkdir("members/$id", 0755);
$newname = "image01.jpg";
$place_file = move_uploaded_file( $_FILES['user_pic']['tmp_name'], "members/$id/".$newname);
}
?>
The html form is as follows:
<table class="table_f" width="100%" cellpadding="3">
<form action="register.php" method="post" enctype="multipart/form-data">
<tr><td colspan="2"><font color="#94A0D1"><?php print "$errormessage"; ?></font></td></tr>
<tr><td><h11>User Name:</h11></td><td><input name="username" type="text" class="formFields" id="username" value="<?php print "$username"; ?>" size="32" maxlength="20" /></tr>
<tr><td><h11>Gender:</h11></td>
<td><label><input name="gender" style="color: #a2a2a2; font-family: 'light', Verdana; font-size: 11px; letter-spacing: 1px" type="radio" id="gender" value="m" checked="checked" />Male <input type="radio" name="gender" id="gender" value="f" />Female</label></td></tr>
<tr><td><h11>Date of Birth: </h11></td>
<td>
<select name="birth_month" class="formFields" id="birth_month">
<option value="<?php print "$b_m"; ?>"><?php print "$b_m"; ?></option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
// etc.. other values removed for space considerations
</select>
<select name="birth_day" class="formFields" id="birth_day">
<option value="<?php print "$b_d"; ?>"><?php print "$b_d"; ?></option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
<option value="04">4</option>
<option value="05">5</option>
// etc..
</select>
<select name="birth_year" class="formFields" id="birth_year">
<option value="<?php print "$b_y"; ?>"><?php print "$b_y"; ?></option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
// etc.
</select>
</td></tr>
<tr><td><h11>Email Address: </h11></td>
<td><input name="email1" type="text" class="formFields" id="email1" value="<?php print "$email1"; ?>" size="32" maxlength="48" /></td></tr>
<tr><td><h11>Confirm Email: </h11></td>
<td><input name="email2" type="text" class="formFields" id="email2" value="<?php print "$email2"; ?>" size="32" maxlength="48" /></td></tr>
<tr><td><h11>Create Password: </h11></td>
<td><input name="pass1" type="password" class="formFields" id="pass1" size="32" maxlength="16" /></tr>
<tr><td><h11>Confirm Password: </h11></td>
<td><input name="pass2" type="password" class="formFields" id="pass2" size="32" maxlength="16" /></tr>
<tr><td><h11>Add Profile Photo: </h11></td>
<td width="521"><input name="user_pic" type="file" class="formFields" id="user_pic" size="42" /> 50 kb max </td></tr>
<tr><td> </td>
<td><input type="submit" style="color: #a2a2a2; font-family: helvetica; font-size: 11px; letter-spacing: 1px" name="Submit" value="Register" /></td></tr>
</form>
</table>
<br />
</div>
<?php include_once "footer_template.php"; ?>
</div>
</body>
</html>
Currently, the script is generating this error code:
"Warning: mysql_query() [function.mysql-query]: Access denied for user 'nbewley'@'localhost' (using password: NO) in /home/nbewley/public_html/manualprintcompany.com/register.php on line 82
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/nbewley/public_html/manualprintcompany.com/register.php on line 82
Access denied for user 'nbewley'@'localhost' (using password: NO)"
which corresponds with the query:
$sql = mysql_query("INSERT INTO myMembers (username, gender, birthday, email, password, ipaddress, sign_up_date)
VALUES('$username','$gender','$full_birthday','$email1','$db_password', '$ipaddress', now())")
or die (mysql_error());
Truthfully, I'm guessing here a bit, but hoping to piece together my knowledge into a workable piece of code. I'm not sure why my query is now generating an error, to begin with. The query is the same that I was using before (which was working) and the database values are the same. I'm sure something is wrong with the way I have structured the code, but I'm not sure what.
Thanks in advance for any advice.
nbewley