简体   繁体   中英

How can I limit or stop a form submission based upon a mysql count for a database drop-drop list?

I have a career day registration system that allow a student to select 3 unique career choices and submit the results to a mysql database. However, I would like to limit the selections to 90 people per career choice.

Example career day schedule:

Session 1: Nursing (limit to 90) Session 2: Military (limit to 90) Session 3: Pharmacy (limit to 90)

Is it possible to do a mysql count and pass the count value to a javascript variable? Also If the count is > 90 a javascript validation happens and doesn't allow you to save to database.

<?php
$sql = "SELECT * FROM mytable WHERE session1 = 'Nursing';
$query = mysql_query($sql);
$num_rows = mysql_num_rows($query);
?>

<script>
var count = <?php echo $num_rows; ?>
function Form_Validator(editform)
{
if((editform.value_session2.value == "Nursing" && count > 90)) { alert("Nursing is closed!"); return (false); }

return (true);
}
</script>

I've tried this method in several different ways... but the values are not being passed. Please help!

You shouldn't be doing this in javascript as any client side script can be bypassed.

What you need to do is do a count on the number of students enrolled in that course before insertion into the database. If count >= 90 , then return an error saying the course is full. If count <= 90 , then proceed with a statement that inserts the data, then return a success message.

I understand this post is old, but here is what I did. When the user clicks on the Nursing session:

mysql_select_db($database_mySQLConnection, $mySQLConnection);
$query_registrant_count = sprintf("SELECT user_id FROM user_signup WHERE class_id = %s", GetSQLValueString($colname_registrant_count, "int"));
$registrant_count = mysql_query($query_registrant_count, $mySQLConnection) or die(mysql_error());
$row_registrant_count = mysql_fetch_assoc($registrant_count);
$totalRows_registrant_count = mysql_num_rows($registrant_count);

if ($totalRows_registrant_count > 19) {
    $returnToCourseList = "../courseListFileName.php";
   header(sprintf("Location: %s", $returnToCourseList . "?alert=" . urlencode("We're sorry, but this class is full. Please select another class or contact us for assistance.")));

exit; }

Then in courseListFileName.php, use Pekka's suggestion for Handling alerts in php

I would do a simple and structured algorithm to work this out:

1) During page load I would only show the avaliable options. Do a sql "group by", having all the "counts" and restrict the form appearence.

2) During the form submission check again about the possibility for a new applicant. Do a simple count again, check if it is possible to receive a new submission. That's it. This would save some concurrency problems and other means of bypassing.

This sounds more secure and clear.

Of course you would script a REST service to chek application avaiability during a form filling moment, but that is more work into something it could be simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM