Hi
I am building a sign up form with image upload as I do know is better to store the images on the server within a folder and just store the image filepath within the database so that’s what I have done and seems to be working and not at the same time
I created a user and it has registered successfully with all the data and has stored the image itself in the uploads folder on the server and in the database it has stored the image filepath but am getting the following errors on the register.php page
Notice: Undefined variable: error in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/directory-site/register.php on line 25
Form has been submitted successfully.
Notice: Undefined index: uploadedfile in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/directory-site/register.php on line 84 The file has been uploaded, and your information has been added to the directory
MySQL error no 1062 : Duplicate entry ‘ianhaney’ for key ‘username’
The coding is below
[code=php]
<?php
if (isset($_POST[‘submit’]) && $error == ”) { // if there is no error, then process further
echo “<p class=’success’>Form has been submitted successfully.</p>”; // showing success message
## connect mysql server
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
# check connection
if ($mysqli->connect_errno) {
echo “<p>MySQL error no {$mysqli->connect_errno} : {$mysqli->connect_error}</p>”;
exit();
}
//This is the directory where images will be saved
$target = “uploads/”;
$target = $target . basename( $_FILES[‘photo’][‘name’]);
## query database
# prepare data for insertion
$username = mysqli_real_escape_string($mysqli, $_POST[‘username’]);
$password = md5($_POST[‘password’]);
$companyname = mysqli_real_escape_string($mysqli, $_POST[‘companyname’]);
$email = mysqli_real_escape_string($mysqli, $_POST[’email’]);
$address1 = mysqli_real_escape_string($mysqli, $_POST[‘address1’]);
$address2 = mysqli_real_escape_string($mysqli, $_POST[‘address2’]);
$town = mysqli_real_escape_string($mysqli, $_POST[‘town’]);
$county = mysqli_real_escape_string($mysqli, $_POST[‘county’]);
$postcode = mysqli_real_escape_string($mysqli, $_POST[‘postcode’]);
$telnumber = mysqli_real_escape_string($mysqli, $_POST[‘telnumber’]);
$category = mysqli_real_escape_string($mysqli, $_POST[‘category’]);
$pic = ($_FILES[‘photo’][‘name’]);
# check if username and email exist else insert
// u = username, e = emai, ue = both username and email already exists
$exists = “”;
$result = $mysqli->query(“SELECT username from users WHERE username = ‘{$username}’ LIMIT 1”);
if ($result->num_rows == 1) {
$exists .= “u”;
}
$result = $mysqli->query(“SELECT email from users WHERE email = ‘{$email}’ LIMIT 1”);
if ($result->num_rows == 1) {
$exists .= “e”;
}
if ($exists == “u”) echo “<p><b>Error:</b> Username already exists!</p>”;
else if ($exists == “e”) echo “<p><b>Error:</b> Email already exists!</p>”;
else if ($exists == “ue”) echo “<p><b>Error:</b> Username and Email already exists!</p>”;
else {
# insert data into mysql database
$sql = “INSERT INTO `users` (`id`, `username`, `password`, `companyname`, `email`, `address1`, `address2`, `town`, `county`, `postcode`, `telnumber`, `category`, `photo`)
VALUES (NULL, ‘{$username}’, ‘{$password}’, ‘{$companyname}’, ‘{$email}’, ‘{$address1}’, ‘{$address2}’, ‘{$town}’, ‘{$county}’, ‘{$postcode}’, ‘{$telnumber}’, ‘{$category}’, ‘{$pic}’)”;
//Writes the photo to the server
if(move_uploaded_file($_FILES[‘photo’][‘tmp_name’], $target))
{
//Tells you if its all ok
echo “The file “. basename( $_FILES[‘uploadedfile’][‘name’]). ” has been uploaded, and your information has been added to the directory”;
}
else {
//Gives and error if its not
echo “Sorry, there was a problem uploading your file.”;
}
$result = $mysqli->query($sql);
if ($mysqli->query($sql)) {
$to = $_POST[’email’];
$subject = “Add Listing Confirmation and Login Credentials”;
$message = “Thank you for signing up and adding your listing, your login information is below rn Username: {$_POST[‘username’]} rn Password: {$_POST[‘password’]}”;
$header = “From:[email protected] rn”;
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo “Message sent successfully…”;
}
else
{
echo “Message could not be sent…”;
}
redirect_to(“login.php?msg=Registered successfully”);
} else {
echo “<p>MySQL error no {$mysqli->errno} : {$mysqli->error}</p>”;
exit();
}
}
}
?>
<div id=”column-whole”>
<!– The HTML registration form –>
<form method=”post” action=”<?=$_SERVER[‘PHP_SELF’]?>” enctype=”multipart/form-data”>
<label>Username:</label> <input type=”text” name=”username” required=”required” placeholder=”Please Enter your chosen username”/>
<br />
<label>Password:</label> <input type=”password” name=”password” required=”required” placeholder=”Please Enter your chosen password”/>
<br />
<label>Company Name:</label> <input type=”text” name=”companyname” required=”required” placeholder=”Please Enter your company name”>
<br />
<label>Email:</label> <input type=”email” name=”email” required=”required” placeholder=”Please Enter your email”/>
<br />
<label>Address Line 1:</label> <input type=”text” name=”address1″ required=”required” placeholder=”Please Enter the first line of your address”/>
<br />
<label>Address Line 2:</label> <input type=”text” name=”address2″ required=”required” placeholder=”Please Enter the second line of your address”/>
<br />
<label>Town:</label> <input type=”text” name=”town” required=”required” placeholder=”Please Enter your town”/>
<br />
<label>County:</label> <input type=”text” name=”county” required=”required” placeholder=”Please Enter your county”/>
<br />
<label>Postcode:</label> <input type=”text” name=”postcode” required=”required” placeholder=”Please Enter your postcode”/>
<br />
<label>Telephone Number:</label> <input type=”text” name=”telnumber” required=”required” placeholder=”Please Enter your landline number”/>
<br />
<label>Category:</label> <input type=”text” name=”category” required=”required” placeholder=”Please Enter your chosen category”/>
<br><br>
<label>Upload Image: </label>
<input type=”hidden” name=”size” value=”350000″>
<input type=”file” name=”photo”>
<br /><br />
<div class=”box”>
<label>I agree to the <a href=”#” target=”_blank”>terms</a></label>
</div>
<input type=”checkbox” class=”checkbox” id=”the-terms” value=”I Agree”>
<input type=”submit” name=”submit” value=”Register” disabled=”disabled” id=”submitBtn” />
<a class=”haveaccount” href=”login.php”>I already have an account…</a>
</form>
Am confused about the errors as does seem to be working sort of