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); ?>
			&nbsp;</td>
		</tr>
		<tr>
			<td style="width: 158px">10:25-10:55</td>
			<td style="width: 399px"><? echo $obj->edit_schedule(3); ?>
			&nbsp;</td>
		</tr>
		<tr>
			<td style="width: 158px">10:55-11:25</td>
			<td style="width: 399px"><? echo $obj->edit_schedule(4); ?>
			&nbsp;</td>
		</tr>
		<tr>
			<td style="width: 158px">12:05-12:35</td>
			<td style="width: 399px"><? echo $obj->edit_schedule(5); ?>
			&nbsp;</td>
		</tr>
		<tr>
			<td style="width: 158px">12:35-1:05</td>
			<td style="width: 399px"><? echo $obj->edit_schedule(6); ?>
			&nbsp;</td>
		</tr>
		<tr>
			<td style="width: 158px">1:05-1:35</td>
			<td style="width: 399px"><? echo $obj->edit_schedule(7); ?>
			&nbsp;</td>
		</tr>
		<tr>
			<td style="width: 158px">1:35-1:55</td>
			<td style="width: 399px"><? echo $obj->edit_schedule(8); ?>
			&nbsp;</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!

Recommended Answers

All 2 Replies

this code looks ok, look for the variable id coming over in the form post set on your select box line.

// line 10 of your function code ?><select name="class" id="<? $row['id'] 
// somewhere in $edit-schedule.php
$id = '';
$id = $_POST['id'];
if ($id != '') {
     // pseudo code
     update db set variables = somevalue where id = $id;
}
// of course, you can get the option that is selected in that select box and update the appropriate db field.

Thanks! With your help and a little ingenuity, I was able to get the code working!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.