PHP sending emails twice and not uploading images correctly :/
<p>I have been designing a website and everything has been working perfectly, until I started adding in little extras so it would work EXACTLY how I wanted it to work.</p>

<p>This is the script for a website that uploads a title, description, name of a person, image, email address and password for the advert that they are putting online. However it no longer wants to correctly name the image and it sends out an email twice, once in the instance that there may be an image and it instantly does it in the instance where someone may not upload an image, but it is reading it as if it is doing both because there is an error with the file upload.</p>

<p>Btw this is the first PHP script I have ever created so it may seem mashy as I have been kind of mixing it up from different things that I have found online :)</p>

<p>p.s the page where the magic happens is www.afterswap.com/give.php</p>

<p>p.p.s I have a global config file that sets all of the DB connection info etc, hence it being non-existent here.</p>

<pre><code>&lt;?PHP
include("inc/header.php");
foreach ($_POST as $key =&gt; $val)
    $_POST[$key] = mysqli_real_escape_string($con, $val);
$back = "&lt;a href='give.php'&gt;Click Here To Go Back And Try Again&lt;/a&gt;";
if (isset($_POST['upload']) &amp;&amp; $_FILES['userfile']['size'] &gt; 0) {
    $title          = mysqli_real_escape_string($title123);
    $title123       = mysqli_real_escape_string($_POST['title']);
    $description    = mysqli_real_escape_string($description123);
    $description123 = mysqli_real_escape_string($_POST['description']);
    $Sell_by        = $_POST['Sell_by'];
    $name           = mysqli_real_escape_string($name123);
    $name123        = mysqli_real_escape_string($_POST['name']);
    $email          = $_POST['email'];
    $password       = $_POST['password'];
    $imagename      = basename($_FILES['userfile']['name']);
    $uploadedfile   = $_FILES['userfile']['tmp_name'];
    if (empty($imagename)) {
        $error = 1;
        echo "&lt;h2 class='error'&gt;The name of the image was not found.&lt;/h2&gt;" . $back;
    }

    if ($error != 1 &amp;&amp; $noimg != 1) {

        $filename  = stripslashes($_FILES['userfile']['name']);
        $extension = substr(strrchr($filename, '.'), 1);
        $extension = strtolower($extension);
    }

    if (($extension != "jpg") &amp;&amp; ($extension != "jpeg") &amp;&amp; ($extension != "png") &amp;&amp; ($extension != "gif")) {
        echo '&lt;h2 class="error"&gt;Error. Images Must Be Jpg, Gif, or Png Format! Please Go Back And Try Another Image.&lt;/h2&gt;' . $back . '';
        $errors = 1;
    } else {

        $time     = time();
        $newimage = "/photos/" . $time . $imagename;
        $result   = move_uploaded_file($_FILES['userfile']['tmp_name'], $newimage);
        if (empty($result)) {
            $error = 1;
            echo "&lt;h2 class='error'&gt;There was an error uploading your image.&lt;/h2&gt;&lt;br/&gt;" . $back . "";
        }


        $date  = date("Y/m/d H:i:s");
        $query = "INSERT INTO classifieds (adid, title, description, Sell_by, name, email, password, picture, date, views, authorized ) VALUES ('', '$title123', '$description123', '$Sell_by', '$name123', '$email', '$password', '$newimage', '$date', '0', '0')";
        mysqli_query($query) or die(mysqli_error());

        $pullback = "SELECT * FROM classifieds WHERE title = '$title123' AND email ='$email' limit 1";
        $query2 = mysqli_query($pullback) or die(mysqli_error());
        while ($row = mysqli_fetch_array($query2, MYSQL_ASSOC)) {
            $newid = $row['adid'];
            $pass  = $row['pass'];
        }

        $url = "http://";
        $url .= getenv("HTTP_HOST");
        $Name      = "AfterSwap";
        $emailf    = "noreply@afterswap.com";
        $recipient = $email;
        $mail_body = "Thank you for posting a new listing!&lt;br /&gt;&lt;br /&gt;You May Now Manage Your Ad by selecting one of the following options:&lt;br /&gt;&lt;br /&gt;Approve your listing: &lt;a href='" . $url . "/approve.php?id=" . $newid . "&amp;pass=" . $password . "'&gt;Click Here&lt;/a&gt;&lt;br/&gt;Edit your listing: &lt;a href='$url/edit.php?id=" . $newid . "&amp;pass=" . $password . "'&gt;Click Here&lt;/a&gt;&lt;br/&gt;Remove your listing: &lt;a href='" . $url . "/remove.php?id=" . $newid . "&amp;pass=" . $password . "'&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Regards,&lt;br /&gt;&lt;br /&gt;The AfterSwap Team";
        $subject   = "AfterSwap Ad Details";
        $headers   = "From: " . $Name . " &lt;" . $emailf . "&gt;\r\n";
        $headers .= "Content-type: text/html\r\n";

        mail($recipient, $subject, $mail_body, $headers);

        echo "&lt;div align='justify'&gt;&lt;div class='success'&gt;Your listing '" . $name123 . "' Has Been Submitted Successfully! &lt;br/&gt;&lt;br/&gt;Please take note: Your listing will not show on the website until you verify it via the email sent to you. This email also allows you to edit and remove your listing as well.&lt;/div&gt;&lt;/div&gt;";

    }
} elseif (isset($_POST['upload'])) {
    $title          = mysqli_real_escape_string($title123);
    $title123       = mysqli_real_escape_string($_POST['title']);
    $description    = mysqli_real_escape_string($description123);
    $description123 = mysqli_real_escape_string($_POST['description']);
    $Sell_by        = $_POST['Sell_by'];
    $name           = mysqli_real_escape_string($name123);
    $name123        = mysqli_real_escape_string($_POST['name']);
    $email          = $_POST['email'];
    $password       = $_POST['password'];
    $date           = date("Y/m/d H:i:s");
    $query          = "INSERT INTO classifieds (adid, title, description, cat, Sell_by, name, email, password, picture, date, views, authorized ) VALUES ('', '$title123', '$description123', '$category', '$Sell_by', '$name123', '$email', '$password', 'images/noimage.jpg', '$date', '0', '0')";
    mysqli_query($query) or die(mysqli_error());

    $pullback = "SELECT * FROM classifieds WHERE title = '$title123' AND email ='$email' limit 1";
    $query2 = mysqli_query($pullback) or die(mysqli_error());
    while ($row = mysqli_fetch_array($query2, MYSQL_ASSOC)) {
        $newid = $row['adid'];
        $pass  = $row['pass'];
    }


    $url = "http://";
    $url .= getenv("HTTP_HOST");
    $Name      = "AfterSwap";
    $emailf    = "noreply@afterswap.com";
    $recipient = $email;
    $mail_body = "Thank you for posting a new listing!&lt;br /&gt;&lt;br /&gt;You May Now Manage Your Ad by selecting one of the following options:&lt;br /&gt;&lt;br /&gt;Approve your listing: &lt;a href='" . $url . "/approve.php?id=" . $newid . "&amp;pass=" . $password . "'&gt;Click Here&lt;/a&gt;&lt;br/&gt;Edit your listing: &lt;a href='$url/edit.php?id=" . $newid . "&amp;pass=" . $password . "'&gt;Click Here&lt;/a&gt;&lt;br/&gt;Remove your listing: &lt;a href='" . $url . "/remove.php?id=" . $newid . "&amp;pass=" . $password . "'&gt;Click Here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Regards,&lt;br /&gt;&lt;br /&gt;The AfterSwap Team";
    $subject   = "AfterSwap Ad Details";
    $headers   = "From: " . $Name . " &lt;" . $emailf . "&gt;\r\n";
    $headers .= "Content-type: text/html\r\n";

    mail($recipient, $subject, $mail_body, $headers);

    echo "&lt;div align='justify'&gt;&lt;div class='success'&gt;Thank you " . $name123 . ", your listing has been submitted successfully! &lt;br/&gt;&lt;br/&gt;Please take note: Your isting will not show on the website until you verify it via the email sent to you. This email also allows you to edit and remove your listing as well.&lt;/div&gt;&lt;/div&gt;";

} else {
?&gt;

/* HTML Form here */

&lt;?PHP } ?&gt;
</code></pre>
