I am making a page which fetch all my database value. I want the page to get the value of Driver_id and Vehicle_id automatically from the database, users need to know what’s the id and key in themselves. But I am stuck at here.
Tools im using is phpMyAdmin.
For which below is my code of the table:
<!doctype html>
<html>
<style>
<table>
<th>Vehicle ID</th>
<th>Vehicle Model</th>
<th>Vehicle Color</th>
<th>Plate Number</th>
<th>Seats</th>
<th>Driver ID</th>
<th> </th>
<?php
$link=mysqli_connect("localhost","root","","jomsewa");
mysqli_select_db($link,"jomsewa") or die(mysqli_error($link));
$select = "SELECT * FROM vehicle";
$row = mysqli_query($link,$select);
while ($array = mysqli_fetch_array($row)){
echo "<tr><td>".$array['Vehicle_id']."</td>
<td>".$array['Vehicle_model']."</td>
<td>".$array['Vehicle_color']."</td>
<td>".$array['Vehicle_model']."</td>
<td>".$array['Vehicle_seats']."</td>
<td>".$array['Driver_id']."</td>
<td><a href='Dmaintenance.php?Driverid=".$array['Driver_id']."'>Select</a></td>"."</tr>";
}
mysqli_close($link);
?>
</table>
</body>
</html>
The link is linked to Dmaintenance.php:
<?php
$link=mysqli_connect("localhost","root","","jomsewa");
if (!$link)
{
echo "Failed to connect to database: " . mysqli_connect_error();
}
mysqli_select_db($link,"jomsewa") or die(mysqli_error($link));
?>
<h3>Please update your maintenance details in the form below.</h3>
<form action="maintenance.php" method="post">
<fieldset>
<legend>Vehicle Maintenance Information:</legend>
<table cellpadding="10">
<tr>
<td>
<?php
if(isset($GET['Driver_id']))
{
$txt = $GET['Driver_id'];
while($row = mysqli_fetch_array($result))
{
echo "<td>".$row['Vehicle_id']."</td>";
echo "<td>".$row['Driver_id']."</td>";
}
}?></td>
</tr>
What i want is when click on one particular row link on the next page it must display my selected row contents automatically.
2
Answers
Use
$_GET['Driverid]
instead of$_GET['Driver_id]
There is no SQL query on
Dmaintenance.php
to fetch row based onDriverid
. There should beFor example
and only use following in
Dmaintenance.php
, you will see the parameter valueWhenever dealing with user supplied data, as you are in
Dmaintenance.php
, you need to take extra precautions to ensure that your scripts are not rendered vulnerable to SQL injection or other nasty surprises. In this instance because you are using user supplied data directly in your SQL ( or would be if you adopt the style of query given by @Rakesh where you directly embed the GET data into the query ) the SQL is vulnerable toSQL Injection
and could spell disaster.The following shows how you might use
prepared statements
to help avoid sql injection. There are liberal comments throughout, hope it helps.The use of
try/catch
above allows you to specify your own messages if/when you encounter problems with the flow through the programme. If you use things likemysqli_error($link)
to depict an error in production code you unintentionally reveal potentially sensitive information about your server / app than you should.