0

i need mysql database to only contain 20 records per certain date and if a user tries to enter more data on a given that has 20 records already ...i want the program to refuse that entry and prompt the user for another date.i'm using php mysql

this is what i have tried

select count($hopeid)from clinics 
Where $date > midnight today and $date < 1159pm tonight and $hopeid =(hopeid)
if results < 20 then 

    mysql_query("INSERT INTO clinics(hopeid, date, time, appttype, newonARV,newfile,txtname)
    VALUES
('$hopeid','$date','$time','$appttype','$newonARV','$newfile','$txtname')") or die(mysql_errno());

    else {

        Echo"this date is full please choose another date";(mysql_errno();
    }
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**pink box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – h2ooooooo Mar 28 '14 at 17:48
  • Maybe have something check right away using a combo of Javascript and AJAX? Make a call and check to see if there are 20 records and return an alert if it is true – Chitowns24 Mar 28 '14 at 17:49

3 Answers3

0

You can place WHERE condition in your SQL query, like

check this

After executing query, test successful insertion using mysql_affected_rows()

if(mysql_affected_rows()==1){
      echo "Inserted..!";
}else if(mysql_affected_rows()==0){
      echo "this date is full please choose another date";
}else{
      echo "error:".mysql_error();
}
Community
  • 1
  • 1
Ravi Dhoriya ツ
  • 4,435
  • 8
  • 37
  • 48
  • it will be better you can do code for **midnight today** and **1159pm tonight**! ... And, how can the error message **"this date is full please choose another date"** be echo-ed/printed out?? – Tun Zarni Kyaw Mar 28 '14 at 18:07
  • Specially for printing message they need to do some extra code, like first verify `count()` and then insert. But for that it has to execute two queries separately. I thought to make it simple. Moreover you can test `if(mysql_affected_rows()==1)` then its inserted, and failed otherwise – Ravi Dhoriya ツ Mar 28 '14 at 18:10
0

after selecting the date call Ajax find the number records in this date if is more than 20 records with that day display some message already 20 records are there in a message and add this new record to take it as date+1 and so it is go to next date so user easily understand

mahesh
  • 1
  • 1
0
$result = mysql_fetch_array(mysql_query("select hopeid from clinics
WHERE " . $date . " BETWEEN '18/03/2011' AND '18/09/2011'"));
$result = sizeOf($result);
if ($result < 20){
    mysql_query("INSERT INTO ... bla bla ...);
    } else {
        echo "this date is full please choose another date";
    }
Anri
  • 1,706
  • 2
  • 17
  • 36