I am confused with my logic here… I have read that I should use insert when the table is emptied, otherwise to use update but I have the following page, where the user can upgrade to a premium membership. Currently, he or she has not opted for memberships, they just have a free member section. Should I do an insert in this case or update?
I am also wondering if I can get this to work using update because I can get the user to select one plan at a time but not two plans or three plans…
Here are my codes but I can’ get the last part to work…
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
session_start();
if(!isset($_SESSION['u_uid']) || !isset($_POST['submit'])) {
header("Location: update.php?subscription=notset");
exit ();
} else {
include_once 'includes/dbh.php';
$subscriptionplan = mysqli_real_escape_string($conn, $_POST['subscriptionplan']);
$subscriptionplan2 = mysqli_real_escape_string($conn, $_POST['subscriptionplan2']);
$subscriptionplan3 = mysqli_real_escape_string($conn, $_POST['subscriptionplan3']);
$sql = "SELECT * FROM memberships WHERE user_uid = '".$_SESSION['u_uid']."'";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while($row = mysqli_fetch_assoc($result)) {
// Select from the database
// insert into plan A
$fees = 100;
$fees2 = 150;
$fees3 = 200;
$totalfees = 0;
$paid = 0;
$overdue = 0;
if ($subscriptionplan == '' || $subscriptionplan2 == '' || $subscriptionplan3 == '') {
header("Location: update.php?subscription=mustchooseallfields");
exit();
} else {
if ($subscriptionplan == 'Level 1' && $subscriptionplan2 == 'Level 1' && $subscriptionplan3 == 'Level 1') {
header("Location: update.php?subscription=mustchoosedifferentlevels");
exit();
} else {
if ($subscriptionplan == 'Level 2' && $subscriptionplan2 == 'Level 2' && $subscriptionplan3 == 'Level 2') {
header("Location: update.php?subscription=mustchoosedifferentlevels");
exit();
} else {
if ($subscriptionplan == 'Level 3' && $subscriptionplan2 == 'Level 3' && $subscriptionplan3 == 'Level 3') {
header("Location: update.php?subscription=mustchoosedifferentlevels");
exit();
} else {
if ($row['activate'] == 0) {
header("Location: update.php?subscription=notactivated");
exit();
} else {
if ($row['subscriptionplan'] != 'None') {
header("Location: update.php?subscription=planAtaken");
exit();
} else {
if ($row['subscriptionplan2'] != 'None') {
header("Location: update.php?subscription=planBtaken");
exit();
} else {
if ($row['subscriptionplan3'] != 'None') {
header("Location: update.php?subscription=planCtaken");
exit();
} else {
if ($subscriptionplan == 'Level 1' && $row['subscriptionplan'] == 'None') {
$totalfees += $fees;
$sql = "UPDATE memberships
SET subscriptionplan = '$subscriptionplan', subscriptionplandate = now(), fees = $fees, paid = $paid, expirydate = now() + interval '1' day, paidbydate = now() + interval '1' day, overdue = $overdue, totalfees = $totalfees
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
header("Location: update.php?subscription=success");
exit();
} else {
if ($subscriptionplan == 'Level 2' && $row['subscriptionplan'] == 'None') {
$totalfees += $fees2;
$sql = "UPDATE memberships
SET subscriptionplan = '$subscriptionplan', subscriptionplandate = now(), fees = $fees2, paid = $paid, expirydate = now() + interval '1' day, paidbydate = now() + interval '1' day, overdue = $overdue, totalfees = $totalfees
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
header("Location: update.php?subscription=success");
exit();
} else {
if ($subscriptionplan == 'Level 3' && $row['subscriptionplan'] == 'None') {
$totalfees += $fees3;
$sql = "UPDATE memberships
SET subscriptionplan = '$subscriptionplan', subscriptionplandate = now(), fees = $fees3, paid = $paid, expirydate = now() + interval '1' day, paidbydate = now() + interval '1' day, overdue = $overdue, totalfees = $totalfees
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
header("Location: update.php?subscription=success");
exit();
} else {
if ($subscriptionplan2 == 'Level 1' && $row['subscriptionplan2'] == 'None') {
$totalfees += $fees;
$sql = "UPDATE memberships
SET subscriptionplan2 = '$subscriptionplan2', subscriptionplandate2 = now(), fees2 = $fees, paid2 = $paid, expirydate2 = now() + interval '1' day, paidbydate2 = now() + interval '1' day, overdue2 = $overdue, totalfees = $totalfees
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
header("Location: update.php?subscription=success");
exit();
} else {
if ($subscriptionplan2 == 'Level 2' && $row['subscriptionplan2'] == 'None') {
$totalfees += $fees2;
$sql = "UPDATE memberships
SET subscriptionplan2 = '$subscriptionplan2', subscriptionplandate2 = now(), fees2 = $fees2, paid2 = $paid, expirydate2 = now() + interval '1' day, paidbydate2 = now() + interval '1' day, overdue2 = $overdue, totalfees = $totalfees
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
header("Location: update.php?subscription=success");
exit();
} else {
if ($subscriptionplan2 == 'Level 3' && $row['subscriptionplan2'] == 'None') {
$totalfees += $fees3;
$sql = "UPDATE memberships
SET subscriptionplan2 = '$subscriptionplan2', subscriptionplandate2 = now(), fees2 = $fees3, paid2 = $paid, expirydate2 = now() + interval '1' day, paidbydate2 = now() + interval '1' day, overdue2 = $overdue, totalfees = $totalfees
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
header("Location: update.php?subscription=success");
exit();
} else {
if ($subscriptionplan3 == 'Level 1' && $row['subscriptionplan3'] == 'None') {
$totalfees += $fees;
$sql = "UPDATE memberships
SET subscriptionplan3 = '$subscriptionplan3', subscriptionplandate3 = now(), fees3 = $fees, paid3 = $paid, expirydate3 = now() + interval '1' day, paidbydate3 = now() + interval '1' day, overdue3 = $overdue, totalfees = $totalfees
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
header("Location: update.php?subscription=success");
exit();
} else {
if ($subscriptionplan3 == 'Level 2' && $row['subscriptionplan3'] == 'None') {
$totalfees += $fees2;
$sql = "UPDATE memberships
SET subscriptionplan3 = '$subscriptionplan3', subscriptionplandate3 = now(), fees3 = $fees2, paid3 = $paid, expirydate3 = now() + interval '1' day, paidbydate3 = now() + interval '1' day, overdue3 = $overdue, totalfees = $totalfees
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
header("Location: update.php?subscription=success");
exit();
} else {
if ($subscriptionplan3 == 'Level 3' && $row['subscriptionplan3'] == 'None') {
$totalfees += $fees3;
$sql = "UPDATE memberships
SET subscriptionplan3 = '$subscriptionplan3', subscriptionplandate3 = now(), fees3 = $fees3, paid3 = $paid, expirydate3 = now() + interval '1' day, paidbydate3 = now() + interval '1' day, overdue3 = $overdue, totalfees = $totalfees
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
header("Location: update.php?subscription=success");
exit();
} else {
if ($subscriptionplan == 'Level 1' && $row['subscriptionplan'] == 'None' && $subscriptionplan2 == 'Level 2' && $row['subscriptionplan2'] == 'None' && $subscriptionplan3 == 'None' && $row['subscriptionplan3'] == 'None') {
$sql = "UPDATE memberships
SET subscriptionplan = '$subscriptionplan', subscriptionplandate = now(), fees = $fees, paid = $paid, expirydate = now() + interval '1' day, paidbydate = now() + interval '1' day, overdue = $overdue
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql);
$sql2 = "UPDATE memberships
SET subscriptionplan2 = '$subscriptionplan2', subscriptionplandate2 = now(), fees2 = $fees2, paid2 = $paid, expirydate2 = now() + interval '1' day, paidbydate2 = now() + interval '1' day, overdue2 = $overdue
WHERE user_uid = '".$_SESSION['u_uid']."'
";
mysqli_query($conn, $sql2);
header("Location: update.php?subscription=success");
exit();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
I have named the first sql as sql1 and the 2nd s sql2