0
votes

I'm trying to submit a form, that should upload a picture and also put it's name in the database, for some reason it does not submit anything nor upload anything, it goes on like nothing. The site reloads every time I submit.

Form:

    <form name="myForm" action="" enctype="multipart/form-data" method="post">

        <div class="id-input">ID:</div>
            <label class="input2 red bold">
                <?php if(isset($beg_u_id)) echo($beg_u_id);?>
            </label>

        <input class="input" name="mon_by" value="<?php if(isset($beg_u_titel)) echo($beg_u_titel);?>" placeholder="By">

         <input class="input" name="mon_title" value="<?php if(isset($mon_u_by)) echo($mon_u_by);?>" placeholder="Title">

        <div class="button-area">
            <input class="btn-upload" type="file" name="mon_img">
            <div class="btn-upload-true">Vælg fil</div>
            <div id="file_name">img/months/<?php if(isset($mon_u_img)) echo($mon_u_img);?></div>
        </div>

        <input class="btn" name="mon_submit" type="submit" value="<?php if ($mode == 'update') echo 'Opdater'; elseif ($mode == 'insert') echo 'Opret'; ?>">
        <a href="panel.php?page=monthspicture.php">
            <button class="danger-btn" type="button" href="">Annuller</button>
        </a>

    <input type="hidden" name="mon_id" value="<?php if(isset($beg_u_id)) echo($beg_u_id);?>">
    </form>
</div>

Submit code:

<?php

$id = $_POST["mon_id"];

$mon_i_img = "";

if($_FILES['mon_img']['error'] == 0){
    $target_dir = "../img/months/";
    $target_file_only = basename($_FILES["mon_img"]["name"]);
    $target_file = $target_dir . $target_file_only;
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image

        $check = getimagesize($_FILES["mon_img"]["tmp_name"]);
        if($check !== false) {
            echo "<div class='success-box'>File is an image - " . $check["mime"] . ".</div>";
            $uploadOk = 1;
        } else {
            echo "<div class='error-box'>File is not an image..</div>";
            $uploadOk = 0;
        }

    // Check if file already exists
    if (file_exists($target_file)) {
        echo "<div class='error-box'>Sorry, file already exists.</div>";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["mon_img"]["size"] > 500000) {
        echo "<div class='error-box'>Sorry, your file is too large.</div>";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
        echo "<div class='error-box'>Sorry, only JPG, JPEG & PNG files are allowed.</div>";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "<div class='error-box'>Sorry, your file was not uploaded.</div>";
    // if everything is ok, try to upload file
    } else {

        if (move_uploaded_file($_FILES["mon_img"]["tmp_name"], $target_file)) {    
            echo "<div class='success-box'>The file ". $target_file_only . " has been uploaded.</div>";
        } else {
            echo "<div class='error-box'>Sorry, there was an error uploading your file.</div>";
        }

    }

    if ($uploadOk == 1) {
        $mon_i_img = $target_file_only;
    }

}

$mon_i_title = $_POST["mon_title"];
$mon_i_by = $_POST["mon_by"];

if (empty($mon_i_title) || empty($mon_i_by)) {
    header ("Location: panel.php?page=monthspicture.php&create=empty");
    exit;
}

$mon_i_id = $mysqli->real_escape_string($mob_i_id);
$mon_i_title = $mysqli->real_escape_string($mon_i_title);
$mon_i_by = $mysqli->real_escape_string($mon_i_by);

if ($mon_i_img != "") {

    $sql_i = "INSERT INTO monthspicture (mon_title, mon_by, mon_img) VALUES('$mon_i_title', '$mon_i_by', '$mon_i_img')";
        echo "<div class='success-box'>Your post have been created.</div>";

}else{

    $sql_i = "INSERT INTO monthspicture (mon_title, mon_by) VALUES('$mon_i_title', '$mon_i_by')";   
        echo "<div class='success-box'>Your post have been created. (Without image)</div>";
}

if (!$mysqli->query($sql_i)) {
 echo "Insert failed: (" . $mysqli->errno . ") " . $mysqli->error;
 die();
}else{

}
?>

Tried adding: print_r($_POST); print_r($_FILES);

Array ( [mon_by] => AthaxDesigns [mon_title] => The Sunrise [mon_submit] => Opret [mon_id] => ) Array ( [mon_img] => Array ( [name] => Athax---The-sunrise.jpg [type] => image/jpeg [tmp_name] => D:\wamp\tmp\phpF488.tmp [error] => 0 [size] => 673646 ) )
2
add print_r($_POST); print_r($_FILES);user557846
well it does submit, so what appears on the page?user557846
Nothing appears, sadly, it just reloads the site. Without uploading the picture or add anything to the database.Athax
serious question - did you write all that code then test it? i test almost every single line as i go along.user557846
@halfer this might be eaiser for both of us, here's a screen share link: join.me/527-245-702 it works through the browser and you can even take controle of my screen.Athax

2 Answers

0
votes

The problem has been found I was a dummy, and did a typo in one of my $_POST['names']; Thanks to everybody who tried to help me.

-1
votes

Try using $_FILES['and your file field name here']

if ($mon_i_img != "") {

$sql_i = "INSERT INTO monthspicture (mon_title, mon_by, mon_img) VALUES('$mon_i_title', '$mon_i_by', '$mon_i_img')";
    echo "<div class='success-box'>Your post have been created.</div>";

}else{

$sql_i = "INSERT INTO monthspicture (mon_title, mon_by) VALUES('$mon_i_title', '$mon_i_by')";   
    echo "<div class='success-box'>Your post have been created. (Without image)</div>";

}