I am using this code for user registration. I would like that if the user does not select any image for his profile a default image will be uploaded.
Any help please?
Code: Select all
<?php
session_start();
require('dbconnect.php');
if (isset($_POST['btnCancel'])) {
header('location: index.php');
} elseif (isset($_POST['btnRegister'])) {
$username = $_POST['txtUsername'];
$password = ($_POST['txtPassword']);
if (strlen(trim($username)) > 0 && strlen(trim($password)) > 0) {
$confirmpassword = ($_POST['txtConfirmPassword']);
$firstname = $_POST['txtFirstName'];
$lastname = $_POST['txtLastName'];
$email = $_POST['txtEmail'];
if (strlen($_POST['txtDOB']) > 0) {
$dob = explode("/", $_POST['txtDOB']);
$day = $dob[0];
$month = $dob[1];
$year = $dob[2];
$dob = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
} else {
$dob = "";
}
$country = $_POST['txtcountry'];
$image = $_FILES['txtImage'];
$filename = "";
//checking if an image was uploaded
if(!empty($image)){
//checking if image is JPG
if ($image['type'] == "image/jpeg" || $image['type'] == "image/pjpeg") {
$filename = $image['name'];
//uploading the file
move_uploaded_file($image['tmp_name'], "images/users/" . $image['name']);
// do the query to check if this image exists
$query = mysql_query('SELECT filename FROM users WHERE filename="' . mysql_real_escape_string($filename) . '"') or die(mysql_error());
if (mysql_num_rows($query))
$message = 'The Image name already exists in the database. Please change Image name and try again.';
}elseif(!empty($image['type'])){
$message = "Only .jpg format images are allowed to be uploaded";
}
}
// do the query to check if the username exists
$query2 = mysql_query('SELECT UserName FROM users WHERE username="' . mysql_real_escape_string($username) . '"') or die(mysql_error());
if (mysql_num_rows($query2))
$message = 'The Username is already in use. Please select a different Username and try again.';
if (isset($_POST['rbnsex'])) {
$sex = $_POST['rbnsex'];
} else {
$sex = "";
}
if (!isset($message)) {
if ($password == $confirmpassword) {
$insert = "INSERT INTO users (username, password, firstname, lastname, email, dob, country, sex, filename, usertype) VALUES ('" . mysql_real_escape_string($username) . "', '" . mysql_real_escape_string($password) . "', '" . mysql_real_escape_string($firstname) . "', '" . mysql_real_escape_string($lastname) . "', '" . mysql_real_escape_string($email) . "', '" . mysql_real_escape_string($dob) . "', '" . mysql_real_escape_string($country) . "', '" . mysql_real_escape_string($sex) . "', '" . mysql_real_escape_string($filename) . "', '0')";
mysql_query($insert) or die(mysql_error());
header('location: index.php');
} else {
$message = "Error: Passwords do not match";
}
}
} else {
$message = "Error: Username and Password are mandatory";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="CSSWebStyles/style.css" />
<title>Online Movie Database - Register</title>
</head>
<body>
<div id="website">
<?php
include('banner.php');
?>
<div id="content">
<center>
<br><h2>Register at Online Movie Database</h2><br>
</center>
<div id="contentgeneral2">
<?php
if (isset($message)) {
echo "<center>$message</center>";
unset($message);
}
?>
<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td>
First Name:
</td>
<td>
<input type="text" style="width: 200px;" name="txtFirstName" />
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" style="width: 200px;" name="txtLastName" />
</td>
</tr>
<tr>
<td>
Username:
</td>
<td>
<input type="text" style="width: 200px;" name="txtUsername" />*
</td>
</tr>
<tr>
<td>
Select a Password:
</td>
<td>
<input type="password" style="width: 200px;" name="txtPassword" />*
</td>
</tr>
<tr>
<td>
Confirm Password:
</td>
<td>
<input type="password" style="width: 200px;" name="txtConfirmPassword" />*
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="text" style="width: 250px;" name="txtEmail" />
</td>
</tr>
<tr>
<td>
D.O.B.:
</td>
<td>
<input type="text" style="width: 150px;" name="txtDOB" /><small>format: (dd/mm/yyyy)</small>
</td>
</tr>
<tr>
<td>
Country:
</td>
<td>
<select name="txtcountry" class="fields" id="country">
<option value="" selected>Please choose from list</option>
<option>Afghanistan </option>
<option>Albania </option>
<option>Other</option>
</select>
</td>
</tr>
<tr>
<td>
Sex:
</td>
<td>
<input type="radio" value="Male" name="rbnsex" /> Male
<input type="radio" value="Female" name="rbnsex" /> Female
</td>
</tr>
<tr>
<td>
Profile Image:
</td>
<td>
<input type="file" name="txtImage" /> <br><br>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="btnRegister" value="Register" /> <input type="submit" name="btnCancel" value="Cancel" />
</td>
</tr>
</table>
</form>
</div>
<div id="contentright">
<div class="blog2" style="float: right; margin-right: 160px; margin-top:40px; width: 250px;">
<h2><center>Why should you Register?</center></h2>
<ul> <br>
<li>Rate Movies from 1 to 10</li> <br>
<li>Give your Comments on Movies</li> <br>
<li>Select Your Favorite Movies</li>
</ul>
</div>
</div>
</div>
<?php
//will include the code from footer.php file
include('footer.php');
?>
</div>
</body>
</html>