Hi. I'm trying to write a simple scheduling script, but have run into a problem. I'm fairly new to PHP, and so have no idea how to update multiple rows in a mysql database.
Currently, my code connects to the database and reads each row. If "class" is empty, it shows a dropdown box with choices. Otherwise, it displays the current value as text and shows a checkbox.
If the user picks an item from the dropdown menu, class should be updated to that value (If they don't, the value of "Not Booked" is ""). If the checkbox is checked, class should be updated to "". Each row in the html table will display either a dropdown menu or a checkbox, never both.
Here's my function to display the editable schedule:
function edit_schedule($period) {
$sql = "SELECT * FROM schedule WHERE id = '$period'";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
if($row['class'] != ""): //if class is not empty
$class = $row['class'];
echo $class;
echo '</td><td><input type="checkbox" value="delete" id="' . $row['id'] . '">';
else://if class is empty
?><select name="class" id="<? $row['id'] ?>"><option value="">Not Booked</option><option value="Option 1">Option 1</option><option value="Option 2">Option 2</option><option value="Option 3">Option 3</option><option value="Option 4">Option 4</option><option value="Option 5">Option 5</option></select></td>
<td>
<?
endif;
};
}
Here is the HTML code:
<form method="post" action="edit-schedule.php" style="text-align:center;">
<input type="hidden" name="update" value="true">
<table style="width: 500px; text-align:center; margin-left:auto; margin-right:auto;">
<tr>
<td style="width: 158px"><strong>Time</strong></td>
<td style="width: 399px"><strong>Class</strong></td>
<td><strong>Delete</strong></td>
</tr>
<tr>
<td style="width: 158px">9:10-9:40</td>
<td style="width: 399px"><? echo $obj->edit_schedule(1); ?>
</td>
</tr>
<tr>
<td style="width: 158px">9:40-10:10</td>
<td style="width: 399px"><? echo $obj->edit_schedule(2); ?>
</td>
</tr>
<tr>
<td style="width: 158px">10:25-10:55</td>
<td style="width: 399px"><? echo $obj->edit_schedule(3); ?>
</td>
</tr>
<tr>
<td style="width: 158px">10:55-11:25</td>
<td style="width: 399px"><? echo $obj->edit_schedule(4); ?>
</td>
</tr>
<tr>
<td style="width: 158px">12:05-12:35</td>
<td style="width: 399px"><? echo $obj->edit_schedule(5); ?>
</td>
</tr>
<tr>
<td style="width: 158px">12:35-1:05</td>
<td style="width: 399px"><? echo $obj->edit_schedule(6); ?>
</td>
</tr>
<tr>
<td style="width: 158px">1:05-1:35</td>
<td style="width: 399px"><? echo $obj->edit_schedule(7); ?>
</td>
</tr>
<tr>
<td style="width: 158px">1:35-1:55</td>
<td style="width: 399px"><? echo $obj->edit_schedule(8); ?>
</td>
</tr>
</table>
<br><button type="submit" value="Submit!">Submit!</button>
</form>
I have a demo here: http://goo.gl/MseVJ
This code displays on the html table perfectly, but I'm not sure how to go about updating. I assume it would be somewhat similar to how I pulled the data from the table, but doing that in reverse has me confused.
As I said, I'm fairly new to php, and may have done things terribly wrong or missed something blindingly obvious. :icon_redface:
Thanks in advance!