Pravljenje proste forme pomoću PHP i MySQL

3.deo


U prethodnom delu, uspeli smo da sastavimo upit u zavisnosti od toga šta je uneto u formu. U ovom delu ćemo dodati ispis greške ukoliko nijedan parametar nije prosleđen.

Teži deo posla smo završili, sada idemo lakše. Sve što treba da uradimo je da inicijalizujemo nove promenljive: $locationError, $priceError, $sizeError na prazne niske i da proverimo koja nije uneta. Ovo radimo posle pravljenja upita, a pre pripremanja naredbe.

Imali smo if(isset($_POST['location']) && $_POST['location'] != ""){//code}, a sada dodajemo else deo: else{$locationError = "Location not inputted."}. Ovo radimo za sva tri parametra, pa proverimo da li nijedan nije unet: if($locationError != "" && $priceError != "" && $sizeError != ""). Ovo znači da korisnik nije uneo ništa u formu i onda u ovom if bloku bacamo izuzetak: throw new PDOException();. U else blok, stavljamo sve naredbe počevši od $statement = $connection->prepare($sql); pa redom sve ispod. Na ovaj način nećemo pripremati naredbu, niti raditi bilo šta sa njom ukoliko nemamo unet barem jedan parametar. Kod:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
    if(isset($_POST['submit'])){
        try{
            require "config.php";
            
            $connection = new PDO($dsn, $username, $password, $options);
            
            $sql = "SELECT *
                    FROM houses
                    WHERE";
            
            $location = "";
            $size = "";
            $price = "";
            $locationError = "";
            $sizeError = "";
            $priceError = "";
                        
            if(isset($_POST['location']) && $_POST['location'] != ""){
                $location = $_POST['location'];
                $sql = $sql . " location = :location";
            }else{
                $locationError = "Location was not inputted.";
            }
            
            if(isset($_POST['price']) && $_POST['price'] != ""){
                $price = $_POST['price'];
                if($location != ""){
                    $sql = $sql . " AND price = :price";
                }else{
                    $sql = $sql . "price = :price";
                }
            }else{
                $priceError = "Price was not inputted.";
            }
            
            if(isset($_POST['size']) && $_POST['size'] != ""){
                $size = $_POST['size'];
                if($location != "" || $price != ""){
                    $sql = $sql . " AND size = :size";
                }else{
                    $sql = $sql . " size = :size";
                }
            }else{
                $sizeError = "Size was not inputted.";
            }
            
            
            if($locationError != "" && $sizeError != "" && $priceError != ""){
                throw new PDOException();
            }else{
            
                $statement = $connection->prepare($sql);
            
            
                if($location != ""){
                    $statement->bindParam(":location", $location, PDO::PARAM_STR);
                }   
                if($price != ""){
                    $statement->bindParam(":price", $price, PDO::PARAM_STR);
                }
                if($size != ""){
                    $statement->bindParam(":size", $size, PDO::PARAM_STR);
                }
            
                $statement->execute();
        
                $result = $statement->fetchAll();
            }
            
        }catch(PDOException $error){
            echo $error->getMessage();
        }
    }

?>

Sada menjamo HTML deo. Tamo gde smo štampali rezultate, u if bloku od if(isset($_POST['submit'])) proveravamo da li su sve error promenljive prazne niske. Ukoliko su sve prazne, znači da je sve okej i da možemo prikazati rezultat. Naš if blok postaje: if(isset($_POST['submit']) && $locationError == "" && $priceError == "" && $sizeError == ""). U else delu, štampamo poruku: echo "You have to input at least one parameter.";

Sada, naš kompletan kod izgleda ovako:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
    if(isset($_POST['submit'])){
        try{
            require "config.php";
            
            $connection = new PDO($dsn, $username, $password, $options);
            
            $sql = "SELECT *
                    FROM houses
                    WHERE";
            
            $location = "";
            $size = "";
            $price = "";
            $locationError = "";
            $sizeError = "";
            $priceError = "";
                        
            if(isset($_POST['location']) && $_POST['location'] != ""){
                $location = $_POST['location'];
                $sql = $sql . " location = :location";
            }else{
                $locationError = "Location was not inputted.";
            }
            
            if(isset($_POST['price']) && $_POST['price'] != ""){
                $price = $_POST['price'];
                if($location != ""){
                    $sql = $sql . " AND price = :price";
                }else{
                    $sql = $sql . "price = :price";
                }
            }else{
                $priceError = "Price was not inputted.";
            }
            
            if(isset($_POST['size']) && $_POST['size'] != ""){
                $size = $_POST['size'];
                if($location != "" || $price != ""){
                    $sql = $sql . " AND size = :size";
                }else{
                    $sql = $sql . " size = :size";
                }
            }else{
                $sizeError = "Size was not inputted.";
            }
            
            
            if($locationError != "" && $sizeError != "" && $priceError != ""){
                throw new PDOException();
            }else{
            
                $statement = $connection->prepare($sql);
            
            
                if($location != ""){
                    $statement->bindParam(":location", $location, PDO::PARAM_STR);
                }   
                if($price != ""){
                    $statement->bindParam(":price", $price, PDO::PARAM_STR);
                }
                if($size != ""){
                    $statement->bindParam(":size", $size, PDO::PARAM_STR);
                }
            
                $statement->execute();
        
                $result = $statement->fetchAll();
            }
            
        }catch(PDOException $error){
            echo $error->getMessage();
        }
    }

?>

<!DOCTYPE html>
<html>
<head>
<title>Basic form</title>
</head>
<body>

<h2>Simple PHP form</h2>    

<form method="post">
    <label for="location">Location:</label>
    <input type="text" name="location" id="location"> <br>

    <label for="price">Price:</label>
    <input type="text" name="price" id="price"><br>

    <label for="size">Size:</label>
    <input type="text" name="size" id="size"><br>

    <input type="submit" name="submit" value="Submit">

</form>
    
    <?php
        if(isset($_POST['submit']) && $locationError == "" && $priceError == "" && $sizeError == ""){
            if($result && $statement->rowCount() > 0){?>
                <h2>Available houses:</h2>
        <?php 
            foreach($result as $row){
                echo "Location: " . $row['location'] . "<br>";
                echo "Price: " . $row['price'] . "<br>";
                echo "Size: " . $row['size'] . "<br>";
            }
        
            }
        }else{
                echo "You have to input at least one parameter.";
            }
    ?>

</body>
</html>

Sada, ako pritisnemo Submit bez popunjavanja forme, izbaciće grešku.


To je to za ovaj deo, nadam se da ste uživali.


Pravljenje proste forme pomoću PHP i MySQL: 1. deo, 2.deo



Hvala što ste čitali, nadam se da je bilo korisno. :)

Lazar Ristić