The Agri-Tech Builder

Coding smart solutions for off-grid farming

Coding a Custom Database to Track Crop Rotation and Yields

Author: Marcial Rey (In Between Bamboos Farm)


Whether you are managing a small vegetable garden or hectares of rice and corn, data is your most valuable asset. Knowing exactly when a specific plot was planted, what fertilizer was applied, and how much it yielded determines your strategy for the next season. For years, farmers have relied on paper notebooks, which can easily be lost or ruined in the rain.

As I continue to build out the "Uni-Farm Hub" application, my goal is to transition my farm's history into a secure digital database. Using my experience in coding HR systems, I am building a custom crop-tracking ledger using PHP and MySQL.

From Employee Data to Farm Data

The logic behind tracking crop cycles is surprisingly similar to human resources. Instead of logging an employee's "Time-In" and "Time-Out," the database logs "Date Planted" and "Date Harvested." Instead of tracking a daily wage, it tracks the "Yield in Kilograms."

By structuring this data in a relational database, I can write software queries to instantly answer questions like: "Which fertilizer produced the highest corn yield in Field B over the last three years?"

The Code: Inserting Harvest Logs

For my fellow developers learning how to connect agricultural interfaces to a backend database, here is a look at the raw PHP logic I use to capture a new harvest record and save it securely to the MySQL database.

<?php
// 1. Connect to the Farm Database
$conn = new mysqli('localhost', 'admin_user', 'password123', 'agritech_db');

// Check for connection errors
if ($conn->connect_error) {
    die("Database Connection Failed: " . $conn->connect_error);
}

// 2. Capture the data (usually from a mobile app or web form)
$crop_type = "Sweet Corn";
$plot_location = "Field B";
$yield_kg = 450.5;
$harvest_date = date("Y-m-d");

// 3. Prepare the SQL Statement to insert the record
$sql = "INSERT INTO harvest_logs (crop, plot, yield_weight, log_date) 
        VALUES ('$crop_type', '$plot_location', '$yield_kg', '$harvest_date')";

// 4. Execute the query and confirm success
if ($conn->query($sql) === TRUE) {
    echo "Success: Harvest log saved to the secure database!";
} else {
    echo "Error logging harvest: " . $conn->error;
}

$conn->close();
?>

Why Build It Yourself?

There are commercial farm management software platforms out there, but they often come with expensive monthly subscription fees that cut into farm profits. By writing the PHP and managing the database myself, I own 100% of my data and pay zero subscription fees.

The next step for this project is visualizing this data. I plan to use JavaScript charts to graph my crop yields season-over-season, allowing me to see the financial health of the farm at a single glance!