Wijzigen gegevens in database

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Johhny Westra

Johhny Westra

31/12/2012 14:00:01
Quote Anchor link
Hallo,

ik ben bezig om een pagina te maken waar een beheerder gegevens kan veranderen in de database. Nu krijg ik een foutmelding wanneer ik de pagina test. Die luidt als volgt:

"7
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in ..... on line 85
Error"

Ik heb gegoogled maar ik kon het probleem niet vinden. Kan iemand mij verder helpen? Alvast hartstikke bedankt!

Het is niet zo mooi en netjes geschreven, mijn excuus hiervoor.

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php

[code]<?php

    // Starten sessie en maken connectie met de database
    require("common.php");
    
    // Kijken of er is ingelogt als een gebruiker
    if(empty($_SESSION['id']))
    {

        // Zo nee worden ze hier doorverstuurd naar het inlogscherm
        header("Location: login.php");
        die("Redirecting to login.php");
    }

    
    // Controleren of er is ingelogt met het account van hap
    if($_SESSION['id'] == 2)
    {

    // Zo ja laten we de pagina zien

if($_SERVER['REQUEST_METHOD']=="POST"){
    
if(isset($_SESSION['item'])){
    $current = $_SESSION['item'];
    }

    else{
        echo "Selecteer een gerecht die je wil wijzigen";
    }

  
echo "$current";
    
    
if($_POST['naamgerecht'] == ''){
    echo "Er is geen naam ingevoerd";
}


else{
    
if($_POST['omschrijvinggerecht'] == ''){
    echo "Er is geen omschrijving ingevoerd";
}


else{
        
$image_size0 = @getimagesize($_FILES["photo0"]['tmp_name']);
$image_size1 = @getimagesize($_FILES["photo1"]['tmp_name']);
$image_size2 = @getimagesize($_FILES["photo2"]['tmp_name']);
$image_size3 = @getimagesize($_FILES["photo3"]['tmp_name']);

if($image_size0 == FALSE OR $image_size1 == FALSE OR $image_size2 == FALSE OR $image_size3 == FALSE)
    echo "Dit is een ongeldig bestandsformaat";
else{

//This is the directory where images will be saved
$target = "images/gerechten/";
$target0 = $target . basename($_FILES['photo0']['name']);
$target1 = $target . basename($_FILES['photo1']['name']);
$target2 = $target . basename($_FILES['photo2']['name']);
$target3 = $target . basename($_FILES['photo3']['name']);

//This gets all the other information from the form
$naam = $_POST['naamgerecht'];
$omschrijving = $_POST['omschrijvinggerecht'];
$file0 = $_FILES['photo0']['name'];
$file1 = $_FILES['photo1']['name'];
$file2 = $_FILES['photo2']['name'];
$file3 = $_FILES['photo3']['name'];
$subcategorie = $_POST['subcategorie'];

$user = "root";
$pass = "usbw";

$dbh = new PDO ("mysql:host=localhost;port=3307;dbname=lachnhap", $user, $pass);
$sql = ("UPDATE items SET naam = :naam, subcategorie = :subcategorie, omschrijving = :omschrijving, primairefoto = :file0, bijfoto1 = file1, bijfoto2 = file2, bijfoto3 = :file3 WHERE id = :itemid");
        
$sth = $dbh->prepare ($sql);

$sth->bindValue(":itemid", $current, PDO::PARAM_INT);
$sth->bindValue(":naam", $naam, PDO::PARAM_STR);
$sth->bindValue(":omschrijving", $omschrijving, PDO::PARAM_STR);
$sth->bindValue(":file0", $file0, PDO::PARAM_STR);
$sth->bindValue(":file1", $file1, PDO::PARAM_STR);
$sth->bindValue(":file2", $file2, PDO::PARAM_STR);
$sth->bindValue(":file3", $file3, PDO::PARAM_STR);
$sth->bindValue(":subcategorie", $subcategorie, PDO::PARAM_STR);

$sth->execute() or DIE("Error");

//Writes the photo to the server
if((move_uploaded_file($_FILES['photo0']['tmp_name'], $target0)) AND (move_uploaded_file($_FILES['photo1']['tmp_name'], $target1)) AND (move_uploaded_file($_FILES['photo2']['tmp_name'], $target2)) AND (move_uploaded_file($_FILES['photo3']['tmp_name'], $target3)))
{

    
//Tells you if its all ok
echo "Het gerecht is succesvol gewijzigd.";
}

else {

//Gives and error if its not
echo "Sorry, er is een probleem opgetreden.";
                    }
                }
            }
        }
    }


?>


<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form method="post" action="wijzigengerecht.php" enctype="multipart/form-data">
            <table>
                <tr>
                    *Alle velden moeten worden ingevuld. kkkkkk
                </tr>
                <tr>
                  <td>Naam gerecht:</td>
                  <td><input type="text" name="naamgerecht" /></td>
                </tr>
                <tr>
                  <td>Subcategorie:</td>
                  <td><input type="text" name="subcategorie" /></td>
                </tr>
                <tr>
                  <td>Omschrijving:</td>
                  <td><textarea rows="10" cols="35" name="omschrijvinggerecht"></textarea></td>
                </tr>
                <tr>
                  <td>Primaire foto:</td>
                  <td><input type="file" name="photo0" /></td>
                </tr>
                <tr>
                  <td>Eerste bijfoto:</td>
                  <td><input type="file" name="photo1" /></td>
                </tr>
                <tr>
                  <td>Tweede bijfoto:</td>
                  <td><input type="file" name="photo2" /></td>
                </tr>
                <tr>
                  <td>Derde bijfoto:</td>
                  <td><input type="file" name="photo3" /></td>
                </tr>
                <tr>
                  <td><input type="submit" title="nieuwgerecht" value="Toevoegen" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>
    
<?php

    }
    
    // Wanneer er is ingelogt maar niet met het account van hap wordt je ook weer naar het inlogscherm doorverstuurd
    else{
        header("Location: login.php");
        die("Redirecting to login.php");
    }

?>


?>
Gewijzigd op 31/12/2012 14:01:09 door Johhny Westra
 
PHP hulp

PHP hulp

29/03/2024 12:41:32
 
Erwin H

Erwin H

31/12/2012 14:06:10
Quote Anchor link
Het aantal parameters in je sql query is dus niet gelijk aan het aantal waardes dat je wilt binden. In de meeste gevallen wil het zeggen dat je een waarde teveel of te weinig wil binden. In jouw geval betekent het dat je een tweetal parameters niet juist hebt in je query (file1 en file2).
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
SET naam = :naam,
subcategorie = :subcategorie,
omschrijving = :omschrijving,
primairefoto = :file0,
bijfoto1 = file1,
bijfoto2 = file2,
 bijfoto3 = :file3
WHERE id = :itemid"


Overigens, dit soort fouten zie je een stuk sneller als je niet van die belachelijk lange regels maakt. Er staat nergens dat een SQL statement op 1 regel moet, waarom dan toch doen? Leesbaarder wordt het er echt niet van!
 
Johhny Westra

Johhny Westra

31/12/2012 14:07:56
Quote Anchor link
Bedankt voor de tip! Zal ik in de toekomst rekening mee houden. Het is inderdaad overzichtelijker wanneer je de regel opbreekt.

Toevoeging op 31/12/2012 14:10:34:

Verbeterde versie voor mensen met (hoewel het onwaarschijnlijk is) hetzelfde probleem:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

[code]<?php

    // Starten sessie en maken connectie met de database
    require("common.php");
    
    // Kijken of er is ingelogt als een gebruiker
    if(empty($_SESSION['id']))
    {

        // Zo nee worden ze hier doorverstuurd naar het inlogscherm
        header("Location: login.php");
        die("Redirecting to login.php");
    }

    
    // Controleren of er is ingelogt met het account van hap
    if($_SESSION['id'] == 2)
    {

    // Zo ja laten we de pagina zien

if($_SERVER['REQUEST_METHOD']=="POST"){
    
if(isset($_SESSION['item'])){
    $current = $_SESSION['item'];
    }

    else{
        echo "Selecteer een gerecht die je wil wijzigen";
    }

  
echo "$current";
    
    
if($_POST['naamgerecht'] == ''){
    echo "Er is geen naam ingevoerd";
}


else{
    
if($_POST['omschrijvinggerecht'] == ''){
    echo "Er is geen omschrijving ingevoerd";
}


else{
        
$image_size0 = @getimagesize($_FILES["photo0"]['tmp_name']);
$image_size1 = @getimagesize($_FILES["photo1"]['tmp_name']);
$image_size2 = @getimagesize($_FILES["photo2"]['tmp_name']);
$image_size3 = @getimagesize($_FILES["photo3"]['tmp_name']);

if($image_size0 == FALSE OR $image_size1 == FALSE OR $image_size2 == FALSE OR $image_size3 == FALSE)
    echo "Dit is een ongeldig bestandsformaat";
else{

//This is the directory where images will be saved
$target = "images/gerechten/";
$target0 = $target . basename($_FILES['photo0']['name']);
$target1 = $target . basename($_FILES['photo1']['name']);
$target2 = $target . basename($_FILES['photo2']['name']);
$target3 = $target . basename($_FILES['photo3']['name']);

//This gets all the other information from the form
$naam = $_POST['naamgerecht'];
$omschrijving = $_POST['omschrijvinggerecht'];
$file0 = $_FILES['photo0']['name'];
$file1 = $_FILES['photo1']['name'];
$file2 = $_FILES['photo2']['name'];
$file3 = $_FILES['photo3']['name'];
$subcategorie = $_POST['subcategorie'];


$user = "root";
$pass = "usbw";

$dbh = new PDO ("mysql:host=localhost;port=3307;dbname=lachnhap", $user, $pass);
$sql = ("UPDATE items SET naam = :naam,  
                          subcategorie = :subcategorie,
                          omschrijving = :omschrijving,
                          primairefoto = :file0,
                          bijfoto1 = :file1,
                          bijfoto2 = :file2,
                          bijfoto3 = :file3
                          WHERE id = :itemid"
);
        
$sth = $dbh->prepare ($sql);

$sth->bindValue(":itemid", $current, PDO::PARAM_INT);
$sth->bindValue(":naam", $naam, PDO::PARAM_STR);
$sth->bindValue(":omschrijving", $omschrijving, PDO::PARAM_STR);
$sth->bindValue(":file0", $file0, PDO::PARAM_STR);
$sth->bindValue(":file1", $file1, PDO::PARAM_STR);
$sth->bindValue(":file2", $file2, PDO::PARAM_STR);
$sth->bindValue(":file3", $file3, PDO::PARAM_STR);
$sth->bindValue(":subcategorie", $subcategorie, PDO::PARAM_STR);


$sth->execute() or DIE("Error");

//Writes the photo to the server
if((move_uploaded_file($_FILES['photo0']['tmp_name'], $target0))
        AND (move_uploaded_file($_FILES['photo1']['tmp_name'], $target1))
        AND (move_uploaded_file($_FILES['photo2']['tmp_name'], $target2))
        AND (move_uploaded_file($_FILES['photo3']['tmp_name'], $target3)))
{

    
//Tells you if its all ok
echo "Het gerecht is succesvol gewijzigd.";
}

else {

//Gives and error if its not
echo "Sorry, er is een probleem opgetreden.";
                    }
                }
            }
        }
    }


?>


<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <form method="post" action="wijzigengerecht.php" enctype="multipart/form-data">
            <table>
                <tr>
                    *Alle velden moeten worden ingevuld. kkkkkk
                </tr>
                <tr>
                  <td>Naam gerecht:</td>
                  <td><input type="text" name="naamgerecht" /></td>
                </tr>
                <tr>
                  <td>Subcategorie:</td>
                  <td><input type="text" name="subcategorie" /></td>
                </tr>
                <tr>
                  <td>Omschrijving:</td>
                  <td><textarea rows="10" cols="35" name="omschrijvinggerecht"></textarea></td>
                </tr>
                <tr>
                  <td>Primaire foto:</td>
                  <td><input type="file" name="photo0" /></td>
                </tr>
                <tr>
                  <td>Eerste bijfoto:</td>
                  <td><input type="file" name="photo1" /></td>
                </tr>
                <tr>
                  <td>Tweede bijfoto:</td>
                  <td><input type="file" name="photo2" /></td>
                </tr>
                <tr>
                  <td>Derde bijfoto:</td>
                  <td><input type="file" name="photo3" /></td>
                </tr>
                <tr>
                  <td><input type="submit" title="nieuwgerecht" value="Toevoegen" /></td>
                </tr>
            </table>
        </form>
    </body>
</html>
    
<?php

    }
    
    // Wanneer er is ingelogt maar niet met het account van hap wordt je ook weer naar het inlogscherm doorverstuurd
    else{
        header("Location: login.php");
        die("Redirecting to login.php");
    }

?>




?>
 
Ivo Breeden

Ivo Breeden

31/12/2012 14:31:28
Quote Anchor link
Johhny Westra op 31/12/2012 14:07:56:
Toevoeging op 31/12/2012 14:10:34:

Verbeterde versie voor mensen met (hoewel het onwaarschijnlijk is) hetzelfde probleem:


Ja dat is inderdaad onwaarschijnlijk. Is het echt helemaal dezelfde melding? Maar op welke regel nu? (Die kan niet hetzelfde zijn, anders is er iets met je caching aan de hand.)
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.