1

EDIT: The problem now is there are no more code errors causing error/notice splats thanks to contributors, but the data still isn't being posted into the MYSQL db, the table is just empty even though script says it's been uploaded.

Here's the code:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {   
    $ogrod_id = trim(sql_safe($_POST['ogrod_id']));
    $doborsadzenieroslin = trim(sql_safe($_POST['doborsadzenieroslin']));
    $nawierzchnia = trim(sql_safe($_POST['nawierzchnia']));
    $systnawadn = trim(sql_safe($_POST['systnawadn']));
    $malaarchitektura = trim(sql_safe($_POST['malaarchitektura']));
    $oczkawodne = trim(sql_safe($_POST['oczkawodne']));
    $trawniki = trim(sql_safe($_POST['trawniki']));
    $oswietlenie = trim(sql_safe($_POST['oswietlenie']));
    $tarasy = trim(sql_safe($_POST['tarasy']));
    $pielegnacja = trim(sql_safe($_POST['pielegnacja']));
    $opis = trim(sql_safe($_POST['opis']));

    if ($opis === '') {
        $opis = '(brak opisu)';
    }

    if ($password !== 'jeeus') {
            $msg = 'Błąd - błędne hasło wgrywania.';
    } else {
        if (isset($_FILES['image'])) {
            $image = $_FILES['image'];
            @list(, , $imtype, ) = getimagesize($_FILES['image']['tmp_name']);

            if ($imtype === 3)
                $ext="png";
            elseif ($imtype === 2)
                $ext="jpeg";
            elseif ($imtype === 1)
                $ext="gif";
            else
                $msg = 'Błąd - nieznany format pliku.';

            if (!isset($msg)) {
                $data = file_get_contents($_FILES['image']['tmp_name']);
                $data = mysqli_real_escape_string($con, $data);

                mysqli_query($con, "INSERT INTO realizacje (ext,opis,image,ogrod_id,doborsadzenierosli,nawierzchnia,systnawadn,malaarchitektura,oczkawodne,trawniki,oswietlenie,tarasy,pielegnacja) VALUES ('$ext','$opis','$data','$ogrod_id','$doborsadzenieroslin','$nawierzchnia','$systnawadn','$malaarchitektura','$oczkawodne','$trawniki','$oswietlenie','$tarasy','$pielegnacja')");

                $msg = 'Sukces - obraz został wgrany na serwer.';
            }
        } else if (isset($_GET['ogrod_id'])) {
            $msg = 'Błąd - plik nie został wgrany.';
        }
    }
}
?>

Here's the HTML markup for image along with it's name property.

Also, the whole form HTML part:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data">
    <div id="filtry">
        <div class="realizacjedodaj">
            <b>Czy na zdjęciu znajduje/ą się <br>wykonany/e przez firmę:</b><br><br>
            <label for="doborsadzenieroslin">dobór/sadzenie roślin?</label><br />
            <input type="radio" name="doborsadzenieroslin" value="0" checked> Tak
            <input type="radio" name="doborsadzenieroslin" value="1"> Nie<br><br>
            <label for="nawierzchnia">nawierzchnia?</label><br />
            <input type="radio" name="nawierzchnia" value="0" checked> Tak
            <input type="radio" name="nawierzchnia" value="1"> Nie<br><br>
            <label for="systnawadn">system nawadniający?</label><br />
            <input type="radio" name="systnawadn" value="0" checked> Tak
            <input type="radio" name="systnawadn" value="1"> Nie<br><br>
            <label for="malaarchitektura">mała architektura?</label><br />
            <input type="radio" name="malaarchitektura" value="0" checked> Tak
            <input type="radio" name="malaarchitektura" value="1"> Nie<br><br>
        </div>
        <div class="realizacjedodaj">
            <label for="oczkawodne">oczko wodne?</label><br />
            <input type="radio" name="oczkawodne" value="0" checked> Tak
            <input type="radio" name="oczkawodne" value="1"> Nie<br><br>
            <label for="trawniki">trawnik?</label><br />
            <input type="radio" name="trawniki" value="0" checked> Tak
            <input type="radio" name="trawniki" value="1"> Nie<br><br>
            <label for="oswietlenie">oswietlenie?</label><br />
            <input type="radio" name="oswietlenie" value="0" checked> Tak
            <input type="radio" name="oswietlenie" value="1"> Nie<br><br>
            <label for="tarasy">taras?</label><br />
            <input type="radio" name="tarasy" value="0" checked> Tak
            <input type="radio" name="tarasy" value="1"> Nie<br><br>
            <label for="pielegnacja">pielęgnacja ogrodu?</label><br />
            <input type="radio" name="pielegnacja" value="0" checked> Tak
            <input type="radio" name="pielegnacja" value="1"> Nie<br><br>
        </div>
    </div>
    <div id="listaOgrodow">
        <div class="realizacjedodaj">
            <label for="ogrod_id"><b>Ogród:</b></label><br />
            <?php
            $ogrodysql = "SELECT id_ogrodu, nazwa FROM ogrody";
            $result = mysqli_query($con, $ogrodysql);

            if (mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)) {
                    echo "#" . $row["id_ogrodu"]. " " . $row["nazwa"]. "<input type='radio' name='ogrod_id' value=" .$row["id_ogrodu"]." <br>";
                }
            } else {
                echo "0 results";
            }
            ?>
            <br /><br /><br />
        </div>
    </div>
    <div id="resztaDanych" class="realizacjedodaj">
        <br><br>
        <label for="image">Zdjęcie realizacji:</label><br />
        <input type="file" name="image" id="image"/><br /><br /><br />

        <label for="opis">Opis (opcjonalnie):</label><br />
        <textarea rows="4" cols="50" name="opis" id="opis"></textarea><br /><br /><br />

        <input type="submit" value="Wgraj"/>
    </div>
</form>
LFlare
  • 150
  • 5
Damian Doman
  • 522
  • 8
  • 19

2 Answers2

1

Looks like you have multiple issues in your code:

1st, the proper simple way to insert into MySQL is:

INSERT INTO table_name (field1,field2) VALUES ('$value1','$value2')

NOT

INSERT INTO {$table}  SET ext='$ext', opis='$opis',

2nd, are you trying to post a normal form's field named image?

trim(sql_safe($_POST['image']));

$_POST is different than $_FILE

also, use

if (isset($_FILE['image'])) {
    $image= $_FILE['image'];
}
wpcoder
  • 1,016
  • 11
  • 17
  • So far i have noticed the $_FILES issue thanks to Grandpa Guru. Ihave managed to make the code not bring any more errors, still the errorless code doesn't put data into mysql DB, i'll try changing the way i insert in a moment and see if it'd help me now – Damian Doman Mar 26 '17 at 00:03
  • unfortunatelly this still wouldn't bring much light on why wouldn't it get posted into mysql db, any more clues on what might be going wrong? ill update codes in a sec – Damian Doman Mar 26 '17 at 00:18
  • [enable error display](http://stackoverflow.com/q/845021/7735285) , provide error logs, check database fields data types, post form action to proper php file. – wpcoder Mar 26 '17 at 00:23
  • Seems as PHP is all fine, but after studying phpinfo(); i've found something i may consider cause of this issue. All the data is stored, along with $image, but not $ext. maybe the part of code creating $ext variable is improper, would be grateful if someone with greater knowledge might have taken a look there. – Damian Doman Mar 26 '17 at 12:58
  • This is how to get image file name: `$name = $_FILES['image']['name'];` and this how to get extension `$ext = pathinfo($name, PATHINFO_EXTENSION);` , [read more...](http://stackoverflow.com/q/10368217/7735285) – wpcoder Mar 27 '17 at 15:26
0

Do you have an element (most likely the file upload element) named image in your form? If not then add it.

Anis Alibegić
  • 2,941
  • 3
  • 13
  • 28
  • updated Q with html markup of this form element, good notice, i might have failed here too rofl – Damian Doman Mar 25 '17 at 23:33
  • 1
    Oh, then check if it's in $_FILES associative array of items that you wish to upload. Hint: `$_FILES["image"]`. – Anis Alibegić Mar 25 '17 at 23:36
  • nope, actually it seems like it gather all the data from form BUT the image from file upload. JS serialize shows : doborsadzenieroslin=0&nawierzchnia=0&systnawadn=0&malaarchitektura=0&oczkawodne=0&trawniki=0&oswietlenie=0&tarasy=0&pielegnacja=0&ogrod_id=1&opis=f4r34 so it gathers everything but image as i said... – Damian Doman Mar 25 '17 at 23:52
  • Uploaded files are received through $_FILES. Everything else through $_POST/$_GET. – Anis Alibegić Mar 25 '17 at 23:55