I’m am successfully echoing a MySQL result set to the screen in a table element. I am playing with the idea of adding a link in the table for each row to delete that row. Before I tried this idea, the table returned correctly.
This is the main code in my index.php:
[code]
<?php include_once "includes/datafunctions.php" ?> Glucose Tracking <?php if (isset($_POST["glucose_submit"]) && $_POST["glucose_submit"] == "Add Record") { add_glucose(); }?>
<p>
<form name="glucose" action="index.php" method="post" onsubmit="return validateForm(this)">
<label>Date and Time
<input type="text" name="glucose_datetime"></label><br>
<label>Reading
<input type="text" name="glucose_reading" size="3" maxlength="3"></label><br />
<label>Tag
<select name="glucose_tag">
<option>Out of Bed</option>
<option>Before Bed</option>
<option>Before Meal</option>
<option>After Meal</option>
<option>Before Exercise</option>
<option>After Exercise</option>
<option>Random Reading</option>
</select></label><br>
<label>Comment
<textarea name="glucose_comment" cols="25" rows="4"></textarea></label><br />
<input type="submit" name="glucose_submit" value="Add Record">
<input type="reset" name="glucose_reset" value="Clear Fields">
<input type="submit" name="glucose_delete" value="Delete Selected Rows">
</form>
</p>
<p>
<table border="1">
<?php //Display rows returned from the get_glucose() function in datafunctions.php
//get_data("select * from glucose_log");
get_glucose();
?>
</table>
<br />
Row IDs to delete:
</p>
</body>
[/code]
This is the function in my datafunctions.php file that contains the link I’m trying to add. It is commented out in this example to show you that it works correctly without it:
[php]function get_glucose()
{
global $connection, $db;
$query = mysql_query("SELECT * FROM glucose_log") or die("Error was: " . mysql_error());
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>".$row["GlucoseLogID"]."</td>";
echo "<td>".$row["GlucoseReading"]."</td>";
//echo "<td><a href='index.php?GlucoseLogID=".$row["GlucoseLogID"].">Delete Row</a></td>";
echo "</tr>";
}
}[/php]
It correctly returns all 6 rows that are in my MySQL table.
(If you want to see a screenshot of the results, I save a screenshot in my Dropbox at http://dl.dropbox.com/u/2090992/Pictures/Hosted/pic1.jpg). Otherwise, trust me that is shows all 6 rows.
But, when I add the Delete link column to the table (uncomment out the line in the function), it doesn’t display all of the rows. It actually only shows every other row (Rows 1, 3, and 5).
(Again, I saved a screenshot of the new results in my Dropbox for you to see at http://dl.dropbox.com/u/2090992/Pictures/Hosted/pic2.jpg)
What am I missing?