Social network from scratch - upload and display image

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
RIA77
New php-forum User
New php-forum User
Posts: 1
Joined: Fri Aug 28, 2020 8:52 am

Fri Aug 28, 2020 8:58 am

Hello and thank you for this beautiful forum.

This is just in order to complete my homework.
Please, no security suggestions, I need just help to complete code.

I manage to upload picture, but how to link picture to profile and to display picture ?

Code: Select all

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Registruj se</title>
  </head>
  <?php
   session_start();
   $postavi = "";


  $conn = mysqli_connect("localhost", "root", "");
   $sql = "CREATE DATABASE IF NOT EXISTS baza";
   mysqli_query ($conn, $sql);
 $conn = mysqli_connect("localhost", "root", "","baza");
 $tab = "CREATE TABLE IF NOT EXISTS Korisnici (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL,
Ime_i_prezime VARCHAR(30) NOT NULL,
broj_telefona INT(30) NOT NULL,
slika INT(30)
)";
 mysqli_query ($conn, $tab);

     if (isset($_POST["unesi"])) {
  $em = $_POST['email'];
  $pass = $_POST['pass'];
  $pass2 = $_POST['pass2'];
  $imeip = $_POST['imeip'];
  $brtel = $_POST['brtel'];
    $conn = mysqli_connect("localhost", "root", "","baza");
    $ubaci = "INSERT INTO Korisnici (email, password, Ime_i_prezime, broj_telefona) VALUES ('$em','$pass','$imeip', '$brtel')";
    if ($pass !== $pass2  ) {
    echo "Šifre se ne podudaraju";
     }
     mysqli_query($conn, $ubaci);

};

if (isset($_POST["unesi_s"])) {
  $id = "";
    $file = $_FILES['slika'];
    $fileName = $_FILES['slika']['name'];
    $fileTmpName = $_FILES['slika']['tmp_name'];
    $fileSize = $_FILES['slika']['size'];

    $fileExt = explode('.',$fileName);
    $fileActualExt = strtolower(end($fileExt));
    $allowed = array('jpeg','png','gif');
    if (in_array($fileActualExt, $allowed)) {
      if ($fileSize < 2097152) {
            $fileNameNew = uniqid ('', true).".".$fileActualExt;
          $fileDestination = 'img/'.$fileNameNew;
            move_uploaded_file($fileTmpName,$fileDestination);
             } else {
            echo "Dozvoljena veličina slike je 2 mb. Molim smanjite sliku.";
       }
    }
    else {
     echo "Nije moguće postaviti fajl sa tom ekstenzijom!";
    }
    $conn = mysqli_connect("localhost", "root", "","baza");
$proveri_id = "SELECT * FROM Korisnici";
$rezultat = mysqli_query($conn,$proveri_id);
while ($row = mysqli_fetch_assoc($rezultat))
                   {
     $id = $row["id"];
                     echo "Trenutni ID je ". $id;
                   die();
     }
}



  ?>
  <body>
    <style media="screen">
   label {color: #0000ff; font-weight: bold; display: block; width: 160px; float: left; } label:after { content: ": " }
   </style>
   <form action="ulogovan.php" method="POST">
     <input type="text" name="email"> <label> Upišite Vaš e- mail </label> <br> <br>
     <input type="password" name="pass"><label> Upišite novu šifru </label> <br> <br>
     <input type="password" name="pass2"><label> Ponovite novu šifru </label> <br> <br>
     <input type="text" name="imeip"> <label> Unesite ime i prezime </label><br> <br>
     <input type="number" name="brtel"> <label> Unesite broj telefona </label><br> <br>
      <input type="submit" name="unesi" value="Unesi"> <br> <br>
      </form>
       <form action="#" method="POST" enctype="multipart/form-data">
        <input type="file" name="slika"> <label> Unesite Vašu sliku</label><br> <br>
        <button type="submit" name="unesi_s">Unesi fajl</button>
           </form>
    </body>
</html>
I always get wrong ID from user.
Thank you.
Post Reply