PHP实现点击按钮更新数据库状态及到期日输入字段功能需求
Alright, let's get this expiry date functionality sorted out for your Aadhaar card verification system. Here's a step-by-step breakdown to implement it properly:
Step 1: Make Sure Your Database Table Supports Expiry Dates
First, confirm your adhar_card table has a column to store the expiry date. If it doesn't, run this SQL to add it:
-- Add a nullable expiry date column (for existing records that don't have this data) ALTER TABLE adhar_card ADD COLUMN expiry_date DATE NULL; -- If you want to enforce that expiry dates are provided for new verifications, use NOT NULL instead: -- ALTER TABLE adhar_card ADD COLUMN expiry_date DATE NOT NULL;
Step 2: Add the Expiry Date Input to Your Frontend
Add a date input field to your form where users can enter the expiry date. Here are examples for common setups:
Plain HTML:
<!-- Insert this near your Verify/Reject buttons --> <div class="form-field"> <label for="aadhaarExpiry">Expiry Date:</label> <input type="date" id="aadhaarExpiry" name="expiry_date" required> </div>
React Example:
import { useState } from 'react'; function AadhaarVerification() { const [formData, setFormData] = useState({ adhard_card_id: '', expiry_date: '' }); const handleInputChange = (e) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; return ( <div> <div className="form-group"> <label htmlFor="expiry_date">Expiry Date:</label> <input type="date" name="expiry_date" value={formData.expiry_date} onChange={handleInputChange} required /> </div> {/* Your Verify/Reject buttons go here */} </div> ); }
Modify your backend code to include the expiry date when updating the adhar_card table. Below are examples for popular backend languages:
PHP with PDO:
// Get values from the submitted form $adharId = $_POST['adhard_card_id']; $expiryDate = $_POST['expiry_date']; // Use prepared statements to avoid SQL injection $query = "UPDATE adhar_card SET status = 2, expiry_date = ? WHERE Adhard_card_id = ?"; $stmt = $pdo->prepare($query); $stmt->execute([$expiryDate, $adharId]); // Return success response to frontend echo json_encode(["message" => "Verification successful, expiry date updated"]);
Node.js (Express + MySQL):
app.post('/verify-aadhaar', (req, res) => { const { adhard_card_id, expiry_date } = req.body; const updateQuery = ` UPDATE adhar_card SET status = 2, expiry_date = ? WHERE Adhard_card_id = ? `; db.query(updateQuery, [expiry_date, adhard_card_id], (err, result) => { if (err) { console.error(err); return res.status(500).json({ error: 'Failed to update record' }); } res.status(200).json({ message: 'Aadhaar verified and expiry date saved' }); }); });
Step 4: Add Optional (But Recommended) Validation
- Frontend Validation: Ensure users can't enter a past expiry date:
const verifyBtn = document.getElementById('verifyBtn'); const expiryInput = document.getElementById('aadhaarExpiry'); verifyBtn.addEventListener('click', (e) => { const selectedDate = new Date(expiryInput.value); const today = new Date(); today.setHours(0, 0, 0, 0); // Ignore time for date-only comparison if (selectedDate < today) { e.preventDefault(); alert('Expiry date cannot be in the past!'); } }); - Backend Validation: Double-check the date format and validity before saving to the database to avoid invalid data.
Quick Notes
- The Reject button's logic stays unchanged—it only needs to update the
statusto 5. - Always use prepared statements (like the examples above) to prevent SQL injection attacks.
- Ensure your form submits both the
adhard_card_idandexpiry_datevalues to the backend endpoint.
内容的提问来源于stack exchange,提问作者Shabarish Shetty
相关产品推荐
相关产品推荐

