JBLC
08-18-2007, 10:46 PM
Hello
I hope someone can give me some advice on the following code.
The script is called register_ops.php. The script is nothing more than a register to assign the date that soemone is in work within a database table. The script display's a list of names with a checkbox next to each. The user can then check the checkbox(s) (to confirm that someone is in work that day) and then submit the entire form which then updates each record that has been checked with the current date. I have stripped out all of the mysql connect "type stuff" to keep the code shorter and everything else works fine except that I cannot get the table column to update those records that have been checked.
Currently the table from which the list of names is generated and the column updated (to record the date) is the same. Called opsv10.
If anyone can give me some help, I would really appreciate it as I have been trying to work this one out for the last 5 days!
<?php
if (isset($_POST['submitted'])) {
foreach ($_POST as $key) {
$id=$key[ops_id];
$query = "UPDATE opsv10 SET on_site= NOW() where ops_id = $id";
}
} else {
// Number of records to show per page:
$display = 10;
// Determine how many pages there are.
if (isset($_GET['np'])) { // Already been determined.
$num_pages = $_GET['np'];
} else { // Need to determine.
$c = addslashes($_SESSION['company']);
// Count the number of records
$query = "SELECT COUNT(*) AS Num FROM opsv10 WHERE company = '$c'";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];
// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}
} // End of np IF.
// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
// Make the query.
$query = "SELECT ops_id, first_name, last_name, position FROM opsv10 WHERE company = '$c' ORDER BY last_name ASC LIMIT $start, $display";
$result = mysql_query ($query); // Run the query.
?>
<div id="contractor_table_holder">
<form action="register_ops.php" method="post">
<?php
// Table header.
echo '
<table align="center" cellspacing="0" cellpadding="8" bgcolor="">
<tr>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b></b></font></td>
<td align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Name</b></font></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Position</b></font></td>
</tr>
';
// Fetch and print all the records.
$bg = ''; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['ops_id'];
$bg = ($bg=='' ? '' : ''); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td style="border-top: 1px solid #000000;" align="center"><input name="checkbox[]" type="checkbox" value="$id" /></td>
<td style="border-top: 1px solid #000000;" align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['first_name'] . ' ' . $row['last_name'] . '</strong></font></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['position'] . '</strong></font></td>
<td><td>
</tr>
';
}
echo '</table>';
?>
<input name="submit" type="button" value="send"/>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name =".$id.[ops_id]" value =".$id.">
</form>
</div>
<?php
mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
?>
<div id="cont_list_pages">
<?php
// Make the links to other pages, if necessary.
if ($num_pages > 1) {
echo '<p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo '<a href="register_ops.php?s=' . ($start - $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><<< </font></a> ';
}
// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><a href="register_ops.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">' . $i . '</font></a> ';
} else {
echo $i . ' ';
}
}
// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="register_ops.php?s=' . ($start + $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"> >>></a></font>';
}
echo '</p>';
}
} // End of links section.
?>
</div>
</div>
</div>
</div>
</div>
<?php // Include the HTML footer.
include ('./includes/footer.html');
?>
I hope someone can give me some advice on the following code.
The script is called register_ops.php. The script is nothing more than a register to assign the date that soemone is in work within a database table. The script display's a list of names with a checkbox next to each. The user can then check the checkbox(s) (to confirm that someone is in work that day) and then submit the entire form which then updates each record that has been checked with the current date. I have stripped out all of the mysql connect "type stuff" to keep the code shorter and everything else works fine except that I cannot get the table column to update those records that have been checked.
Currently the table from which the list of names is generated and the column updated (to record the date) is the same. Called opsv10.
If anyone can give me some help, I would really appreciate it as I have been trying to work this one out for the last 5 days!
<?php
if (isset($_POST['submitted'])) {
foreach ($_POST as $key) {
$id=$key[ops_id];
$query = "UPDATE opsv10 SET on_site= NOW() where ops_id = $id";
}
} else {
// Number of records to show per page:
$display = 10;
// Determine how many pages there are.
if (isset($_GET['np'])) { // Already been determined.
$num_pages = $_GET['np'];
} else { // Need to determine.
$c = addslashes($_SESSION['company']);
// Count the number of records
$query = "SELECT COUNT(*) AS Num FROM opsv10 WHERE company = '$c'";
$result = mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$num_records = $row[0];
// Calculate the number of pages.
if ($num_records > $display) { // More than 1 page.
$num_pages = ceil ($num_records/$display);
} else {
$num_pages = 1;
}
} // End of np IF.
// Determine where in the database to start returning results.
if (isset($_GET['s'])) {
$start = $_GET['s'];
} else {
$start = 0;
}
// Make the query.
$query = "SELECT ops_id, first_name, last_name, position FROM opsv10 WHERE company = '$c' ORDER BY last_name ASC LIMIT $start, $display";
$result = mysql_query ($query); // Run the query.
?>
<div id="contractor_table_holder">
<form action="register_ops.php" method="post">
<?php
// Table header.
echo '
<table align="center" cellspacing="0" cellpadding="8" bgcolor="">
<tr>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b></b></font></td>
<td align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Name</b></font></td>
<td align="center"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><b>Position</b></font></td>
</tr>
';
// Fetch and print all the records.
$bg = ''; // Set the background color.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['ops_id'];
$bg = ($bg=='' ? '' : ''); // Switch the background color.
echo '<tr bgcolor="' . $bg . '">
<td style="border-top: 1px solid #000000;" align="center"><input name="checkbox[]" type="checkbox" value="$id" /></td>
<td style="border-top: 1px solid #000000;" align="center"><img src="program_images/spacer_gif.gif" border="0" /></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['first_name'] . ' ' . $row['last_name'] . '</strong></font></td>
<td style="border-top: 1px solid #000000;" align="left"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2" color="#FFFFFF"><strong>' . $row['position'] . '</strong></font></td>
<td><td>
</tr>
';
}
echo '</table>';
?>
<input name="submit" type="button" value="send"/>
<input type="hidden" name="submitted" value="TRUE" />
<input type="hidden" name =".$id.[ops_id]" value =".$id.">
</form>
</div>
<?php
mysql_free_result ($result); // Free up the resources.
mysql_close(); // Close the database connection.
?>
<div id="cont_list_pages">
<?php
// Make the links to other pages, if necessary.
if ($num_pages > 1) {
echo '<p>';
// Determine what page the script is on.
$current_page = ($start/$display) + 1;
// If it's not the first page, make a Previous button.
if ($current_page != 1) {
echo '<a href="register_ops.php?s=' . ($start - $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><<< </font></a> ';
}
// Make all the numbered pages.
for ($i = 1; $i <= $num_pages; $i++) {
if ($i != $current_page) {
echo '<font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"><a href="register_ops.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2">' . $i . '</font></a> ';
} else {
echo $i . ' ';
}
}
// If it's not the last page, make a Next button.
if ($current_page != $num_pages) {
echo '<a href="register_ops.php?s=' . ($start + $display) . '&np=' . $num_pages . '"><font face="Geneva, Arial, Helvetica, sans-serif, Century Gothic" size="2"> >>></a></font>';
}
echo '</p>';
}
} // End of links section.
?>
</div>
</div>
</div>
</div>
</div>
<?php // Include the HTML footer.
include ('./includes/footer.html');
?>