syntax error, unexpected '$_GET' (T_VARIABLE)
Momenteel ben ik bezig met een master/detail page.
Uit de DB haal ik enkele info op zoals naam, productie en verloop datum op.
Van deze naam heb ik een link gemaakt. En wanneer ik klik op naam, wil ik meer gegevens uit DB halen die daarbij horen. Helaas ben ik hiermee 2 dagen mee bezig en het lukt me niet om het op te lossen. Ikzelf heb het idee dat het een syntax issue is, maar ik kom er niet uit.
Hopelijk kan iemand mij voorzien van een waardevolle feedback.
Tnx!
Index.php
Code (php)
1
2
3
4
2
3
4
<?php
$query2 = "SELECT * FROM medicines";
$result2 = mysqli_query($connection,$query2);
?>
$query2 = "SELECT * FROM medicines";
$result2 = mysqli_query($connection,$query2);
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
echo "<table width='70%' border='solid 1px'>";
echo "<tr><th>Name</th><th>Production_Date</th><th>Expire_Date</th><th>Edit</th><th>Delete</th></tr>";
while ($row2 = mysqli_fetch_assoc($result2)){
$medicine_id = $row2['med_id'];
$medicine_name = $row2['med_name'];
$medicine_create_date = $row2['med_create_date'];
$medicine_expire_date = $row2['med_expire_date'];
echo "<tr>";
echo "<td><a href='med_detail.php?id={$medicine_name}'>{$medicine_name}</a></td>" .
"<td>{$medicine_create_date}</td>" .
"<td>{$medicine_expire_date}</td>" .
"<td><a href='edit.php?id={$medicine_id}'>Edit</a></td>" .
"<td><a href='delete.php?id={$medicine_id}'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
echo "<table width='70%' border='solid 1px'>";
echo "<tr><th>Name</th><th>Production_Date</th><th>Expire_Date</th><th>Edit</th><th>Delete</th></tr>";
while ($row2 = mysqli_fetch_assoc($result2)){
$medicine_id = $row2['med_id'];
$medicine_name = $row2['med_name'];
$medicine_create_date = $row2['med_create_date'];
$medicine_expire_date = $row2['med_expire_date'];
echo "<tr>";
echo "<td><a href='med_detail.php?id={$medicine_name}'>{$medicine_name}</a></td>" .
"<td>{$medicine_create_date}</td>" .
"<td>{$medicine_expire_date}</td>" .
"<td><a href='edit.php?id={$medicine_id}'>Edit</a></td>" .
"<td><a href='delete.php?id={$medicine_id}'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
detail pagina
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php include "includes/db-config.php"; ?>
<?php
if(isset($_GET['$medicine_name'])){
$_GET['$medicine_name'];
}
$query = "SELECT * from categories";
$result = mysqli_query($connection,$query);
$query3 = "SELECT * FROM medicines ";
$query3 .="WHERE ID="$_GET['$medicine_name']" ";
$result3 = mysqli_query($connection,$query3);
?>
<?php
if(isset($_GET['$medicine_name'])){
$_GET['$medicine_name'];
}
$query = "SELECT * from categories";
$result = mysqli_query($connection,$query);
$query3 = "SELECT * FROM medicines ";
$query3 .="WHERE ID="$_GET['$medicine_name']" ";
$result3 = mysqli_query($connection,$query3);
?>
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
echo "<table width='70%' border='solid 1px'>";
echo "<tr><th>Name</th><th>Production_Date</th><th>Expire_Date</th><th>Created by factory</th><th>Added to database</th><th>Edit</th><th>Delete</th></tr>";
while ($row2 = mysqli_fetch_assoc($result3)){
$medicine_id = $row2['med_id'];
$medicine_name = $row2['med_name'];
$medicine_create_date = $row2['med_create_date'];
$medicine_expire_date = $row2['med_expire_date'];
$medicine_factory = $row2['med_factory_id'];
$medicine_register_date = $row2['med_register_date'];
echo "<tr>";
echo "<td>{$medicine_name}</td>" .
"<td>{$medicine_create_date}</td>" .
"<td>{$medicine_expire_date}</td>" .
"<td>{$medicine_factory}</td>" .
"<td>{$medicine_register_date}</td>" .
"<td><a href='edit.php?id={$medicine_id}'>Edit</a></td>" .
"<td><a href='delete.php?id={$medicine_id}'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
echo "<table width='70%' border='solid 1px'>";
echo "<tr><th>Name</th><th>Production_Date</th><th>Expire_Date</th><th>Created by factory</th><th>Added to database</th><th>Edit</th><th>Delete</th></tr>";
while ($row2 = mysqli_fetch_assoc($result3)){
$medicine_id = $row2['med_id'];
$medicine_name = $row2['med_name'];
$medicine_create_date = $row2['med_create_date'];
$medicine_expire_date = $row2['med_expire_date'];
$medicine_factory = $row2['med_factory_id'];
$medicine_register_date = $row2['med_register_date'];
echo "<tr>";
echo "<td>{$medicine_name}</td>" .
"<td>{$medicine_create_date}</td>" .
"<td>{$medicine_expire_date}</td>" .
"<td>{$medicine_factory}</td>" .
"<td>{$medicine_register_date}</td>" .
"<td><a href='edit.php?id={$medicine_id}'>Edit</a></td>" .
"<td><a href='delete.php?id={$medicine_id}'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
HTML gebruikt dubbele quotes (").
Hou vars buiten de php-quotes.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
echo '<table width="70%" border="solid 1px">';
echo '<tr><th>Name</th><th>Production_Date</th><th>Expire_Date</th><th>Edit</th><th>Delete</th></tr>';
while ($row2 = mysqli_fetch_assoc($result2))
{
echo '<tr>';
echo'<td><a href="med_detail.php?id='.$row2['med_id'].'">'.$row2['med_name'].'</a></td>' .
'<td>'.$row2['med_create_date'].'</td>' .
'<td>'.$row2['med_expire_date'].'</td>' .
'<td><a href="edit.php?id='.$row2['med_id'].'">Edit</a></td>' .
'<td><a href="delete.php?id='.$row2['med_id'].'">Delete</a></td>';
echo '</tr>';
}
echo '</table>';
?>
echo '<table width="70%" border="solid 1px">';
echo '<tr><th>Name</th><th>Production_Date</th><th>Expire_Date</th><th>Edit</th><th>Delete</th></tr>';
while ($row2 = mysqli_fetch_assoc($result2))
{
echo '<tr>';
echo'<td><a href="med_detail.php?id='.$row2['med_id'].'">'.$row2['med_name'].'</a></td>' .
'<td>'.$row2['med_create_date'].'</td>' .
'<td>'.$row2['med_expire_date'].'</td>' .
'<td><a href="edit.php?id='.$row2['med_id'].'">Edit</a></td>' .
'<td><a href="delete.php?id='.$row2['med_id'].'">Delete</a></td>';
echo '</tr>';
}
echo '</table>';
?>
Ik heb bij de eerste <a> ook het id gebruikt, waar jij de name gebruikte.
Gewijzigd op 17/08/2016 13:22:16 door - SanThe -
Daarbij probeert hij deze aan te roepen, zonder er iets mee te doen. (toewijzen, echo,of wat dan ook)
Dit hierboven doet niks, en zorgt voor de foutmelding.
Verbetering hieronder.
detail pagina:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<?php include "includes/db-config.php";
if(isset($_GET['id'])){
$query = "SELECT * from categories";
$result = mysqli_query($connection,$query);
$query3 = "SELECT * FROM medicines ";
$query3 .="WHERE ID="$_GET['id']" ";
$result3 = mysqli_query($connection,$query3);
} else {
echo 'Geen medicijn gekozen.';
}
?>
if(isset($_GET['id'])){
$query = "SELECT * from categories";
$result = mysqli_query($connection,$query);
$query3 = "SELECT * FROM medicines ";
$query3 .="WHERE ID="$_GET['id']" ";
$result3 = mysqli_query($connection,$query3);
} else {
echo 'Geen medicijn gekozen.';
}
?>
In het overzicht tabel noem je:
Dus je gaat naar med_detail.php?id=medicijn_naam
De _GET is dan ['id']
Gewijzigd op 17/08/2016 13:30:48 door Dennis WhoCares
Je kunt PHP blokken openen en sluiten waar je wilt.
Vergelijk:
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
echo "<table width='70%' border='solid 1px'>";
echo "<tr><th>Name</th><th>Production_Date</th><th>Expire_Date</th><th>Created by factory</th><th>Added to database</th><th>Edit</th><th>Delete</th></tr>";
while ($row2 = mysqli_fetch_assoc($result3)){
$medicine_id = $row2['med_id'];
$medicine_name = $row2['med_name'];
$medicine_create_date = $row2['med_create_date'];
$medicine_expire_date = $row2['med_expire_date'];
$medicine_factory = $row2['med_factory_id'];
$medicine_register_date = $row2['med_register_date'];
echo "<tr>";
echo "<td>{$medicine_name}</td>" .
"<td>{$medicine_create_date}</td>" .
"<td>{$medicine_expire_date}</td>" .
"<td>{$medicine_factory}</td>" .
"<td>{$medicine_register_date}</td>" .
"<td><a href='edit.php?id={$medicine_id}'>Edit</a></td>" .
"<td><a href='delete.php?id={$medicine_id}'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
echo "<table width='70%' border='solid 1px'>";
echo "<tr><th>Name</th><th>Production_Date</th><th>Expire_Date</th><th>Created by factory</th><th>Added to database</th><th>Edit</th><th>Delete</th></tr>";
while ($row2 = mysqli_fetch_assoc($result3)){
$medicine_id = $row2['med_id'];
$medicine_name = $row2['med_name'];
$medicine_create_date = $row2['med_create_date'];
$medicine_expire_date = $row2['med_expire_date'];
$medicine_factory = $row2['med_factory_id'];
$medicine_register_date = $row2['med_register_date'];
echo "<tr>";
echo "<td>{$medicine_name}</td>" .
"<td>{$medicine_create_date}</td>" .
"<td>{$medicine_expire_date}</td>" .
"<td>{$medicine_factory}</td>" .
"<td>{$medicine_register_date}</td>" .
"<td><a href='edit.php?id={$medicine_id}'>Edit</a></td>" .
"<td><a href='delete.php?id={$medicine_id}'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
?>
Met:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<table style="width: 70%; border: 1px solid;">
<tr>
<th>Name</th>
<th>Production Date</th>
<th>Expire Date</th>
<th>Created by Factory</th>
<th>Added to Database</th>
<th>Edit</th>
<th>Delete</th>
</tr><?php
while ($row = mysqli_fetch_assoc($result)) {
?><tr>
<td><?php echo $row['med_name'] ?></td>
<td><?php echo $row['med_create_date'] ?></td>
<td><?php echo $row['med_expire_date'] ?></td>
<td><?php echo $row['med_factory_id'] ?></td>
<td><?php echo $row['med_register_date'] ?></td>
<td><a href="edit.php?id=<?php echo $row['med_id'] ?>">Edit</a></td>
<td><a href="delete.php?id=<?php echo $row['med_id'] ?>">Delete</a></td>
</tr><?php
}
?></table>
<tr>
<th>Name</th>
<th>Production Date</th>
<th>Expire Date</th>
<th>Created by Factory</th>
<th>Added to Database</th>
<th>Edit</th>
<th>Delete</th>
</tr><?php
while ($row = mysqli_fetch_assoc($result)) {
?><tr>
<td><?php echo $row['med_name'] ?></td>
<td><?php echo $row['med_create_date'] ?></td>
<td><?php echo $row['med_expire_date'] ?></td>
<td><?php echo $row['med_factory_id'] ?></td>
<td><?php echo $row['med_register_date'] ?></td>
<td><a href="edit.php?id=<?php echo $row['med_id'] ?>">Edit</a></td>
<td><a href="delete.php?id=<?php echo $row['med_id'] ?>">Delete</a></td>
</tr><?php
}
?></table>
Daarnaast doe je er misschien verstandig aan om je output te escapen, zowel in queries (middels een real_escape() functie) alsook in je HTML (via htmlspecialchars()).
En wat @Dennis zegt: je hebt weinig aan een doorgegeven medicijnnaam als je verder alles op grond van id doet.
Bedankt voor jullie snelle reacties.
Ik heb deels jullie verbeteringen aangebracht, helaas krijg ik nog steeds dezelfde foutmelding, nl:
Parse error: syntax error, unexpected '$_GET' (T_VARIABLE) in D:\xampp\htdocs\inventory_test\med_detail.php on line 5
index.php: gewijzigde code:
Code (php)
1
2
3
2
3
<?php
echo "<td><a href='med_detail.php?id={$medicine_id}'>{$medicine_name}</a></td>"
?>
echo "<td><a href='med_detail.php?id={$medicine_id}'>{$medicine_name}</a></td>"
?>
Tussen haakjes gebruik ik juist variabelen, omdat ik het makkelijker vindt met alle die syntaxis.
med_detail.php: Code wat bij lijn 5 hoort is:
Code (php)
@Dennis, je hebt gelijk wat het betreft security in de code.
Wanneer de kern van de code werkt, dan ga ik security als stap 2 aanpakken
Toevoeging op 17/08/2016 15:09:41:
Update;
Mijn probleem heb ik opgelost door de output van $_GET['id']; later toe te kennen aan een variabele.
Code (php)
Helaas heb ik nu een andere foutmelding en dat is:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in D:\xampp\htdocs\inventory_test\med_detail.php on line 21
Ik ga nog even verder zoeken.
Wanneer ik een update heb, zal ik het hier plaatsen.
tnx!
Toevoeging op 17/08/2016 15:26:36:
Update:
Het tweede probleem heb ik nu ook opgelost:
Door de query te controleren of het gelukt is of niet :-)
vernieuwde code:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
if(isset($_GET['id'])){
$id = $_GET['id'];
$query3 = "SELECT * FROM medicines ";
$query3 .="WHERE med_id=$id";
$result3 = mysqli_query($connection,$query3);
} else {
echo "No medicine has been chosen";
}
if($result3 === false){
die (mysqli_error($connection));
}
?>
Bedankt allemaal voor het meedenken!
if(isset($_GET['id'])){
$id = $_GET['id'];
$query3 = "SELECT * FROM medicines ";
$query3 .="WHERE med_id=$id";
$result3 = mysqli_query($connection,$query3);
} else {
echo "No medicine has been chosen";
}
if($result3 === false){
die (mysqli_error($connection));
}
?>
Bedankt allemaal voor het meedenken!
goed dat het opgelost is :)
Kijk eens goed naar deze 2 regels:
Zoek de verschillen, en je zult zien waarom de 2e regel wel werkt ;-)
Tip, let op je quotes
Tevens kloppen je quotes niet, en mist er goede beveiliging m.b.v. escaping in je query. ZOnder dit ben je heel vatbaar voor SQL-injection wat veel gebruikt wordt bij hacking.
Ik heb de code even verbeterd.
Code (php)
Een tipje: Vermijd nummers in je variabelen. Weet jij straks nog wat $query3 doet? Geen gewoon een duidelijke naam die aangeeft wat je ophaalt.