trainmania100 trainmania100 - 1 year ago 68
HTML Question

Deleting a record in a PHP MySQL generated table

I am creating a system where records in a database need to be displayed in a HTML table.
The HTML table and getting data is working fine.
However I need a column with a delete icon/link for each row.
For example, row one will have a row of data and in the final column a X for delete.
It was working fine previously, however now I need to update everything from MySQL to MySQLi.
The old way of generating my table no longer works (using a for loop and php) and so I must use a while loop.

Please see my code.

<table align="center" cellpadding="10px">
echo "<table align=center; width=833; height=102; border=1>"; //begin table tag...

echo "<tr bgcolor=#eeeeee>";
echo "<td>" . "Locomotive" . "</td>";
echo "<td>" . "Headcode" . "</td>";
echo "<td>" . "Seen At" . "</td>";
echo "<td>" . "Train Origin" . "</td>";
echo "<td>" . "Train Destination" . "</td>";
echo "<td>" . "On" . "</td>";
echo "<td>" . "At" . "</td>";
echo "<td>" . "Action" . "</td>";
echo "</tr>";

while ($rowitem = mysqli_fetch_array($dataresults)) {
echo "<tr>";
echo "<td>" . $rowitem['Class'] . $rowitem['Number'] . "</td>";
echo "<td>" . $rowitem['Headcode'] . "</td>";
echo "<td>" . $rowitem['SightedLocation'] . "</td>";
echo "<td>" . $rowitem['TrainOrigin'] . "</td>";
echo "<td>" . $rowitem['TrainDestination'] . "</td>";
echo "<td>" . $rowitem['SightingDate'] . "</td>";
echo "<td>" . $rowitem['SightingTime'] . "</td>";
echo "<td>" . <a href = "SightingHome.php?d=1&id=<?php echo $rowitem['SightingID'] ?>" <img src = 'Images/delete.png' title = "Delete This Sighting" width = "25" height = "24" /> </td>;
echo "</tr>";
echo "</table>";

The problem is I am struggling to get the PHP inside the row for the action, the last echo in the bottom table.

If anyone needs the full code I can provide it.

PS: dataresults is the following

$UserName = $_SESSION['UserID'];
$SightingData = "SELECT * from tblsightings WHERE UserID = '$UserName' ORDER BY SightingDate DESC" ;

$dataresults = mysqli_query($connection,$SightingData);

I am just rebuilding the new system off a old system I already had however is been a few years since I did PHP.
New site

Answer Source

I think the problem is you are mixing html and php the wrong way.

Like your are using echo on <a href=...> and this can't be understand as php code because it should be a string. So you have some struggling with the " that are understand as something to stop the string, and not as a part of the string. So you have to add a \ before so it is read as a part of the string.

Change this line :

 echo "<td>" . <a href = "SightingHome.php?d=1&id=<?php echo $rowitem['SightingID'] ?>" <img src = 'Images/delete.png' title = "Delete This Sighting" width = "25" height = "24" /> </td>;

to :

echo "<td> <a href =\"SightingHome.php?d=1&id=" . $rowitem['SightingID'] . "<img src = 'Images/delete.png' title = \"Delete This Sighting\" width = \"25\" height = \"24\" /></td>";

The answer from moni_dragu make it cleaner, using ' to start the string and include the " as part of the string, so you won't need to use \ in front of each ".

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download