php script geeft een melding
Ik heb het volgende script bedacht:
<form action="" method="POST">
<h3>Total Amount is:</h3>
<hr>
<label for="fromdate">From Date:</label>
<input size="4" id="fromdate" name="fromdate" value="" maxlength="4" type="text">
- <input size="2" id="dmonth" name="dmonth" value="" maxlength="2" type="text">
- <input size="2" id="dday" name="dday" value="" maxlength="2" type="text">
<label for="todate">To Date:</label>
<input size="4" id="todate" name="todate" value="" maxlength="4" type="text">
- <input size="2" id="dmonth" name="tdmonth" value="" maxlength="2" type="text">
- <input size="2" id="dday" name="tdday" value="" maxlength="2" type="text">
<input type="submit" name="calculateFormSubmit" value="Get" />
<h3>Total Amount in Date Duration is: </h3>
</form>
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
45
46
47
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
<?php
if($_SERVER["REQUEST_METHOD"] == "POST" && array_key_exists("calculateFormSubmit", $_POST)) {
$connectionData = [
"server" => "localhost",
"user" => "root",
"pass" => "",
"database" => "administratie"
];
$db = null;
try {
$db = new PDO("mysql:host=" . $connectionData["server"] . ";dbname=" . $connectionData["database"], $connectionData["user"], $connectionData["pass"]);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
}
$dateFrom = (int)$_POST["fromdate"] . "-" . (int)$_POST["dmonth"] . "-" . (int)$_POST["dday"];
$dateTo = (int)$_POST["todate"] . "-" . (int)$_POST["tdmonth"] . "-" . (int)$_POST["tdday"];
$stmt = $db->prepare("SELECT adm_post, sum(adm_bedrag)
FROM adm_uitgave
WHERE adm_datum BETWEEN STR_TO_DATE(:date_from, '%Y-%c-%e') AND STR_TO_DATE(:date_to, '%Y-%c-%e')
group by adm_post;");
$stmt->bindValue(":date_from", $dateFrom);
$stmt->bindValue(":date_to", $dateTo);
$stmt->execute();
$userData = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($userData != false) {
foreach ($userData as $current) {
echo "<b>" . $current["adm_post"] . "</b> - " . $current["adm_bedrag"] . "<br />";
}
}
if($db !== null) {
$db = null;
}
}
?>
if($_SERVER["REQUEST_METHOD"] == "POST" && array_key_exists("calculateFormSubmit", $_POST)) {
$connectionData = [
"server" => "localhost",
"user" => "root",
"pass" => "",
"database" => "administratie"
];
$db = null;
try {
$db = new PDO("mysql:host=" . $connectionData["server"] . ";dbname=" . $connectionData["database"], $connectionData["user"], $connectionData["pass"]);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo $e->getMessage();
}
$dateFrom = (int)$_POST["fromdate"] . "-" . (int)$_POST["dmonth"] . "-" . (int)$_POST["dday"];
$dateTo = (int)$_POST["todate"] . "-" . (int)$_POST["tdmonth"] . "-" . (int)$_POST["tdday"];
$stmt = $db->prepare("SELECT adm_post, sum(adm_bedrag)
FROM adm_uitgave
WHERE adm_datum BETWEEN STR_TO_DATE(:date_from, '%Y-%c-%e') AND STR_TO_DATE(:date_to, '%Y-%c-%e')
group by adm_post;");
$stmt->bindValue(":date_from", $dateFrom);
$stmt->bindValue(":date_to", $dateTo);
$stmt->execute();
$userData = $stmt->fetchAll(PDO::FETCH_ASSOC);
if($userData != false) {
foreach ($userData as $current) {
echo "<b>" . $current["adm_post"] . "</b> - " . $current["adm_bedrag"] . "<br />";
}
}
if($db !== null) {
$db = null;
}
}
?>
Het probleem is dat ik op tabel amd_bedrag "Undefined index: adm_bedrag" krijg.
Terwijl als ik de query op Mysql server draai "versie: 5.7.36" deze goed is en ik krijg te zien wat de query moet laten zien.
Mijn vraag is, wat doe ik verkeerd en hoe krijg ik het werkend?
SELECT adm_post, SUM(adm_bedrag) AS 'het_bedrag' FROM ...
$current["het_bedrag"]
Dan krijg ik de volgende melding: Parse error: syntax error, unexpected '$stmt' (T_VARIABLE)
Dat is een fout in je syntax. Laat eens zien wat je hebt aangepast.
Het werkt, super bedankt. Had de commando verkeerd geplaatst.
Mooi zo :-) Graag gedaan.