Update/Insert query met 2 kolommen werkt, 5 niet

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Jeroen Nick

Jeroen Nick

22/11/2016 11:27:52
Quote Anchor link
Hallo allemaal,

Ik heb een script waarbij ik 2 kolommen update en deze werkt prima. Nu wil ik deze uitbreiden met drie extra kolommen, alleen krijg ik deze maar niet aan de praat en ik weet niet waar de fout zit.

Wellicht dat iemand van jullie weet wat ik verkeerd doe?

De werkende code (met 2 velden):

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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

<h1>View Records</h1>

<p><b>View All</b> | <a href="view-paginated.php">View Paginated</a></p>

<?php
// connect to the database
include('connect-db.php');

// get the records from the database
if ($result = $mysqli->query("SELECT * FROM seizoen16 ORDER BY id"))
{

// display records if there are records to display
if ($result->num_rows > 0)
{

// display records in a table
echo "<table border='1' cellpadding='10'>";

// set table headers
echo "<tr><th>ID</th><th>Speler</th><th>Gespeeld</th><th></th><th></th></tr>";

while ($row = $result->fetch_object())
{

// set up a row for each record
echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->Speler . "</td>";
echo "<td>" . $row->Gespeeld . "</td>";
echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}


echo "</table>";
}

// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}

// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}


// close database connection
$mysqli->close();

?>


<a href="records.php">Add New Record</a>
</body>
</html>


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
182
183
184
185
186
187
188
189
190
191
192
<?php
/*
Allows the user to both create new records and edit existing records
*/

// connect to the database

include("connect-db.php");

// creates the new/edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable

function renderForm($Spelerr = '', $Gespeeldd ='', $error = '', $id = '')
{
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
<?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
<?php if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
. "</div>";
}
?>


<form action="" method="post">
<div>
<?php if ($id != '') { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>ID: <?php echo $id; ?></p>
<?php } ?>

<strong>First Name: *</strong> <input type="text" name="Speler"
value="<?php echo $Spelerr; ?>"/><br/>
<strong>Last Name: *</strong> <input type="text" name="Gespeeld"
value="<?php echo $Gespeeldd; ?>"/>
<p>* required</p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>

<?php }



/*

EDIT RECORD

*/
// if the 'id' variable is set in the URL, we know that we need to edit a record

if (isset($_GET['id']))
{

// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{

// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))
{

// get variables from the URL/form
$id = $_POST['id'];
$Speler = htmlentities($_POST['Speler'], ENT_QUOTES);
$Gespeeld = htmlentities($_POST['Gespeeld'], ENT_QUOTES);

// check that firstname and lastname are both not empty
if ($Speler == '' || $Gespeeld == '')
{

// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($Speler, $Gespeeld, $error, $id);
}

else
{
// if everything is fine, update the record in the database
if ($stmt = $mysqli->prepare("UPDATE seizoen16 SET Speler = ?, Gespeeld = ?
WHERE id=?"
))
{

$stmt->bind_param("ssi", $Speler, $Gespeeld, $id);
$stmt->execute();
$stmt->close();
}

// show an error message if the query has an error
else
{
echo "ERROR: could not prepare SQL statement.";
}


// redirect the user once the form is updated
header("Location: view.php");
}
}

// if the 'id' variable is not valid, show an error message
else
{
echo "Error!";
}
}

// if the form hasn't been submitted yet, get the info from the database and show the form
else
{
// make sure the 'id' value is valid
if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{

// get 'id' from URL
$id = $_GET['id'];

// get the recod from the database
if($stmt = $mysqli->prepare("SELECT * FROM seizoen16 WHERE id=?"))
{

$stmt->bind_param("i", $id);
$stmt->execute();

$stmt->bind_result($id, $Speler, $Gespeeld);
$stmt->fetch();

// show the form
renderForm($Speler, $Gespeeld, NULL, $id);

$stmt->close();
}

// show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement";
}
}

// if the 'id' value is not valid, redirect the user back to the view.php page
else
{
header("Location: view.php");
}
}
}




/*

NEW RECORD

*/
// if the 'id' variable is not set in the URL, we must be creating a new record

else
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{

// get the form data
$Speler = htmlentities($_POST['Speler'], ENT_QUOTES);
$Gespeeld = htmlentities($_POST['Gespeeld'], ENT_QUOTES);

// check that firstname and lastname are both not empty
if ($Speler == '' || $Gespeeld == '')
{

// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($Speler, $Gespeeld, $error);
}

else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT seizoen16 (Speler, Gespeeld) VALUES (?, ?)"))
{

$stmt->bind_param("ss", $Speler, $Gespeeld);
$stmt->execute();
$stmt->close();
}

// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}


// redirec the user
header("Location: view.php");
}

}

// if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
}


// close the mysqli connection
$mysqli->close();
?>




De code niet werkende code met vijf velden:

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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/*
Allows the user to both create new records and edit existing records
*/

// connect to the database

include("connect-db.php");

// creates the new/edit record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable

function renderForm($Spelerr = '', $Gespeeldd ='', $honderdtachtigg ='', $Puntenn ='', $bovenhonderdd ='', $error = '', $id = '')
{
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
<?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
<?php if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
. "</div>";
}
?>


<form action="" method="post">
<div>
<?php if ($id != '') { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>ID: <?php echo $id; ?></p>
<?php } ?>

<strong>Speler: *</strong> <input type="text" name="Speler"
value="<?php echo $Spelerr; ?>"/><br/>
<strong>Gespeeld: *</strong> <input type="text" name="Gespeeld"
value="<?php echo $Gespeeldd; ?>"/>
<strong>180: *</strong> <input type="text" name="honderdtachtig"
value="<?php echo $honderdtachtigg; ?>"/><br/>
<strong>Punten: *</strong> <input type="text" name="Punten"
value="<?php echo $Puntenn; ?>"/>
<strong>100+: *</strong> <input type="text" name="bovenhonderd"
value="<?php echo $bovenhonderdd; ?>"/>
<p>* required</p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>

<?php }



/*

EDIT RECORD

*/
// if the 'id' variable is set in the URL, we know that we need to edit a record

if (isset($_GET['id']))
{

// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{

// make sure the 'id' in the URL is valid
if (is_numeric($_POST['id']))
{

// get variables from the URL/form
$id = $_POST['id'];
$Speler = htmlentities($_POST['Speler'], ENT_QUOTES);
$Gespeeld = htmlentities($_POST['Gespeeld'], ENT_QUOTES);
$honderdtachtig = htmlentities($_POST['honderdtachtig'], ENT_QUOTES);
$Punten = htmlentities($_POST['Punten'], ENT_QUOTES);
$bovenhonderd = htmlentities($_POST['bovenhonderd'], ENT_QUOTES);


// check that firstname and lastname are both not empty
if ($Speler == '' || $Gespeeld == '' || $honderdtachtig == '' || $Punten == '' || $bovenhonderd == '')
{

// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($Speler, $Gespeeld, $honderdtachtig, $Punten, $bovenhonderd, $error, $id);
}

else
{
// if everything is fine, update the record in the database
if ($stmt = $mysqli->prepare("UPDATE seizoen16 SET Speler = ?, Gespeeld = ?, honderdtachtig = ?, Punten = ?, bovenhonderd = ?
WHERE id=?"
))
{

$stmt->bind_param("ssi", $Speler, $Gespeeld, $honderdtachtig, $Punten, $bovenhonderd, $error, $id);
$stmt->execute();
$stmt->close();
}

// show an error message if the query has an error
else
{
echo "ERROR: could not prepare SQL statement.";
}


// redirect the user once the form is updated
header("Location: view.php");
}
}

// if the 'id' variable is not valid, show an error message
else
{
echo "Error!";
}
}

// if the form hasn't been submitted yet, get the info from the database and show the form
else
{
// make sure the 'id' value is valid
if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{

// get 'id' from URL
$id = $_GET['id'];

// get the recod from the database
if($stmt = $mysqli->prepare("SELECT * FROM seizoen16 WHERE id=?"))
{

$stmt->bind_param("i", $id);
$stmt->execute();

$stmt->bind_result($id, $Speler, $Gespeeld, $honderdtachtig, $Punten, $bovenhonderd);
$stmt->fetch();

// show the form
renderForm($Speler, $Gespeeld, $honderdtachtig, $Punten, $bovenhonderd, NULL, $id);

$stmt->close();
}

// show an error if the query has an error
else
{
echo "Error: could not prepare SQL statement";
}
}

// if the 'id' value is not valid, redirect the user back to the view.php page
else
{
header("Location: view.php");
}
}
}




/*

NEW RECORD

*/
// if the 'id' variable is not set in the URL, we must be creating a new record

else
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{

// get the form data
$Speler = htmlentities($_POST['Speler'], ENT_QUOTES);
$Gespeeld = htmlentities($_POST['Gespeeld'], ENT_QUOTES);
$honderdtachtig = htmlentities($_POST['honderdtachtig'], ENT_QUOTES);
$Punten = htmlentities($_POST['Punten'], ENT_QUOTES);
$bovenhonderd = htmlentities($_POST['bovenhonderd'], ENT_QUOTES);

// check that firstname and lastname are both not empty
if ($Speler == '' || $Gespeeld == '' || $honderdtachtig == '' || $Punten == '' || $bovenhonderd == '')
{

// if they are empty, show an error message and display the form
$error = 'ERROR: Please fill in all required fields!';
renderForm($Speler, $Gespeeld, $honderdtachtig, $Punten, $bovenhonderd, $error);
}

else
{
// insert the new record into the database
if ($stmt = $mysqli->prepare("INSERT seizoen16 (Speler, Gespeeld, honderdtachtig, Punten, bovenhonderd) VALUES (?, ?, ?, ?, ?)"))
{

$stmt->bind_param("ss", $Speler, $Gespeeld, $honderdtachtig, $Punten, $bovenhonderd);

$stmt->execute();
$stmt->close();
}

// show an error if the query has an error
else
{
echo "ERROR: Could not prepare SQL statement.";
}


// redirec the user
header("Location: view.php");
}

}

// if the form hasn't been submitted yet, show the form
else
{
renderForm();
}
}


// close the mysqli connection
$mysqli->close();
?>


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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>View Records</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>

<h1>View Records</h1>

<p><b>View All</b> | <a href="view-paginated.php">View Paginated</a></p>

<?php
// connect to the database
include('connect-db.php');

// get the records from the database
if ($result = $mysqli->query("SELECT * FROM seizoen16 ORDER BY id"))
{

// display records if there are records to display
if ($result->num_rows > 0)
{

// display records in a table
echo "<table border='1' cellpadding='10'>";

// set table headers
echo "<tr><th>ID</th><th>Speler</th><th>Gespeeld</th><th>180</th><th>Punten</th><th>Bovenhonderd</th><th></th><th></th></tr>";

while ($row = $result->fetch_object())
{

// set up a row for each record
echo "<tr>";
echo "<td>" . $row->id . "</td>";
echo "<td>" . $row->Speler . "</td>";
echo "<td>" . $row->Gespeeld . "</td>";
echo "<td>" . $row->honderdtachtig . "</td>";
echo "<td>" . $row->Punten . "</td>";
echo "<td>" . $row->bovenhonderd . "</td>";
echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}


echo "</table>";
}

// if there are no records in the database, display an alert message
else
{
echo "No results to display!";
}
}

// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}


// close database connection
$mysqli->close();

?>


<a href="records.php">Add New Record</a>
</body>
</html>
 
PHP hulp

PHP hulp

16/11/2024 00:41:26
 
Ivo P

Ivo P

22/11/2016 12:48:23
Quote Anchor link
Je vraag heefte betrekking op een query met 5 kolommen.

Dan mis ik de logica om dan níet die query te posten, maar in plaats daarvan een wel werkende applicatie met een stuk of 5 scripts.

Zoiets als "he garage, mijn auto rammelt. Maar hier heb je net zo'n auto staan die niet rammelt."
 
Aad B

Aad B

22/11/2016 13:23:40
Quote Anchor link
Kortom, een beetje teveel code om te onderzoeken. Post wast minder code en als het even kan wel de foutmelding die je ontvangt en een klein stukje code waarvan jij denkt dat het mis gaat.
 
Thomas van den Heuvel

Thomas van den Heuvel

22/11/2016 16:39:45
Quote Anchor link
Check je bind_param() regels; het aantal letters voor de typehints van de kolommen komt niet overeen met het aantal kolommen. Daardoor mislukken de bind_param() aanroepen.

Wanneer het melden + weergeven van fouten ingeschakeld was geweest was je hier meteen achter gekomen. Op het moment dat je code aan het schrijven/aanpassen bent zouden dit soort instellingen te allen tijde aan moeten staan.
 
Jeroen Nick

Jeroen Nick

23/11/2016 12:16:38
Quote Anchor link
Ik heb de fout gevonden, bij de bind_param moest ik aangeven welke paramaters gebruikt worden. Bij de vijf velden stond er alleen SS (string, string), maar daar moest ik dus nog meerdere parameters aangeven voor de INT.
 



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.