/    Sign up×
Community /Pin to ProfileBookmark

php signup form with image upload

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>
[/code]

Am confused about the errors as does seem to be working sort of

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@KruSuPhyJul 10.2015 — Notices are less like errors in that they don't affect the way your code is run. It's just PHP letting you know something that you may need to know. Here, it's saying that $error and uploadedfile aren't set to anything. In the case of $error, that's probably what you're going for, so that could be unimportant. I'm not sure, but maybe changing (isset($_POST['submit']) &amp;&amp; $error == '') to (isset($_POST['submit']) &amp;&amp; !($error)) would get rid of the notice. But like I said, I'm not sure. just for the sake of running the code, you could ignore that notice more than likely. in the case of "programming efficiently", there may be a different story that someone better at PHP could tell you about.

The undefined index 'uploadedfile' is more of a problem. I haven't done any file uploading(though I will be soon with the current project I'm working on, so I'd like to figure out the answer to your problem as well in case I run into it.) The notice just means that either uploadedfile itself isn't declared or 'name' isn't, and i'm not really sure which one that would be since I assume you're using a form and both would have to be declared when the form is submitted... you are actually uploading a file when you test out the code, right?
Copy linkTweet thisAlerts:
@KruSuPhyJul 10.2015 — sorry for the double post, but it seems i can't edit my previous post for some reason. but i would like to add that my input may be partially incorrect or just completely wrong, but since nobody else had responded yet, it couldn't hurt anything to try and help.
Copy linkTweet thisAlerts:
@ianhaneyauthorJul 10.2015 — I sorted the two issues now and works perfect, just got two little ones still

on the php page I get the following

Form has been submitted successfully.

The file new-logo.jpg has been uploaded, and your information has been added to the directory

MySQL error no 1062 : Duplicate entry 'ianhaney' for key 'username' - NOT SURE WHAT IS CAUSING THIS ERROR

also the php should redirect to login.php?msg="Registered Successfully but is not doing that, it just stays on the register.php page but get the above message I pasted bit further up
Copy linkTweet thisAlerts:
@ianhaneyauthorJul 10.2015 — sorry sussed it, I had the querie being executed twice within the coding so took one out and is working perfect now and is not duplicating anymore in the database
×

Success!

Help @ianhaney spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...