bickyz
bickyz

Reputation: 49

PHP MySQL allow users to insert record X times a day

I have a php mysql cms where users logon to add/insert daily stock value. Currently users can add the stock value unlimited times.

How will the PHP code be if I want to restrict them to add XX times (quota) a day ? Lets say If I want company abc to add two times a day or company def three times a day.

Following is my table:

http://imageshack.us/a/img69/5440/vhwd.png

Any help would be much appreciated, thank you.

Upvotes: 0

Views: 583

Answers (1)

Martin Bean
Martin Bean

Reputation: 39389

Simple. When they attempt post a stock value do a SELECT query to see how many times they’ve posted that day.

<?php
// set quota as a variable so it can easily be changed
$quota = 1;

if (isset($_POST['submit'])) {
    // look up 
    $sql = "SELECT COUNT(*) AS count
            FROM tblstock
            WHERE comid = ? AND DATE(updated) = CURDATE()";

    $stmt = $db->prepare($sql);
    $stmt->execute(array($comid));

    $row = $stmt->fetchObject();

    if (intval($row->count) < $quota) {
        // insert stock
    }
    else {
        // quota has been hit
    }
}

Upvotes: 1

Related Questions