Data toevoegen aan een database
Code (php)
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
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
<?php
$action=isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
//include database connection
include("includes/connect.php");
try{
//write query
$query="INSERT INTO todo SET begindatum = ?, todobefore = ?, todo = ?, beschrijving , opdrachtklaar = ?";
[b]//prepare query for excecution
$stmt=$con->prepare($query)[/b]
//bind the parameters
//this is the first question mark
$stmt->bindParam(1, $_POST['begindatum']);
//this is the second question mark
$stmt->bindParam(2, $_POST['todobefore']);
//this is the third question mark
$stmt->bindParam(3, $_POST['todo']);
//this is the fourth question mark
$stmt->bindParam(4, $_POST['beschrijving']);
//this is the fourth question mark
$stmt->bindParam(5, $_POST['opdrachtklaar']);
// Execute the query
if($stmt->execute()){
echo"Record was saved.";
}else{
die('Unable to save record.');
}
}catch(PDOException$exception){ //to handle error
echo"Error: ".$exception->getMessage();
}
}
?>
$action=isset($_POST['action']) ? $_POST['action'] : "";
if($action=='create'){
//include database connection
include("includes/connect.php");
try{
//write query
$query="INSERT INTO todo SET begindatum = ?, todobefore = ?, todo = ?, beschrijving , opdrachtklaar = ?";
[b]//prepare query for excecution
$stmt=$con->prepare($query)[/b]
//bind the parameters
//this is the first question mark
$stmt->bindParam(1, $_POST['begindatum']);
//this is the second question mark
$stmt->bindParam(2, $_POST['todobefore']);
//this is the third question mark
$stmt->bindParam(3, $_POST['todo']);
//this is the fourth question mark
$stmt->bindParam(4, $_POST['beschrijving']);
//this is the fourth question mark
$stmt->bindParam(5, $_POST['opdrachtklaar']);
// Execute the query
if($stmt->execute()){
echo"Record was saved.";
}else{
die('Unable to save record.');
}
}catch(PDOException$exception){ //to handle error
echo"Error: ".$exception->getMessage();
}
}
?>
hij geeft aan Notice: Undefined variable: con in C:\xampp\htdocs\deplanner\add.PHP on line 23
Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\xampp\htdocs\deplanner\add.PHP:23 Stack trace: #0 {main} thrown in C:\xampp\htdocs\deplanner\add.PHP on line 23.
alleen ik snap niet waar de fout kan zitten. ik heb geprobeerd aan te passen en weg te gooien maar krijg het niet voor elkaar jullie zijn mij laatste hoop.
met vriendelijke groet,
Robin
Toevoeging op 07/03/2017 13:05:50:
hij gaat hier fout in op regel 13 en 14
Ward:
Titel en [code][/code]-tags aangepast.
Gewijzigd op 07/03/2017 13:54:02 door Ward van der Put
Notice: Undefined variable: con in C:\xampp\htdocs\deplanner\add.PHP on line 23
Dit houdt in dat $con niet bestaat (niet gedeclareerd is).
Er vanuitgaande dat $con gecreëerd wordt in includes/connect.php
wil dit zeggen dat
$action blijkbaar niet gelijk is aan "create" (en daarmee wordt de include niet geladen)
dus blijkbaar heeft
$_POST['action'] niet de goede waarde.
Dit alles uitgaande van een connect.php zonder bugs of andere rare zaken.
Heb je de inhoud van $_POST al eens naar het scherm gedumpt?
Nog veel belangrijker dan het opstellen van specifieke code is het belangrijk dat je in staat bent om te herleiden waar fouten in willekeurige code vandaan komen, vooral als je deze code zelf schrijft... anders zou dit inhouden dat je je eigen code niet begrijpt. Alles start bij het snappen van wat foutmeldingen jou vertellen. Deze zijn in PHP vaak redelijk to-the-point: ze leggen doorgaans meteen de vinger op de zere plek. De rest is gewoon een kwestie van het kruimelpad terugvolgen, zoals hierboven gebeurt.
Gewijzigd op 07/03/2017 13:43:09 door Thomas van den Heuvel