When running this code I get get the message telling me that "New User Successfully Added" or "Existing User Data Successfully Updated" but nothing changes in the database. Please help!
<?php
error_reporting (E_ALL ^ E_NOTICE);
$a = session_id();
if(empty($a)) session_start();
include "MySQLConnector.txt";
$LocalCustomerID = 0;
$WhatAction = "";
// Get the data from the form:
$firstname = trim($_REQUEST['firstname']);
$middleinitial = trim($_REQUEST['middleinitial']);
$lastname = trim($_REQUEST['lastname']);
$address = trim($_REQUEST['address']);
$address2 = trim($_REQUEST['address2']);
$city = trim($_REQUEST['city']);
$state = trim($_REQUEST['state']);
$zipcode = trim($_REQUEST['zipcode']);
$username = trim($_REQUEST['username']);
$password = trim($_REQUEST['password']);
$email = trim($_REQUEST['email']);
$phone = trim($_REQUEST['phone']);
$birthdate = trim($_REQUEST['birthdate']);
// $CustomerID2Update = $_REQUEST['CustomerID2Update'];// Hidden
//Check HERE to see if Username already exists.
switch ($_SESSION['SecurityID']) {
case 1:// Existing User UPDATING their own data.
$UsernameQuery = "SELECT COUNT(*) AS UsernameCount FROM customers
WHERE UserName = '" . $username . "' AND CustomerID != " . $_SESSION['CustomerID'] ;// Build the query
$rs = @mysqli_query ($dbc, $UsernameQuery); // Return the Result Set
WHILE ($row = mysqli_fetch_array($rs, MYSQLI_ASSOC)) {// Fetch the data
$UsernameCount = $row['UsernameCount'];
}
if($UsernameCount > 0) {// Username Already Exists...
$_SESSION['Message'] = "Sorry, the User Name: <i><font color=red>" . $Username . "</font></i> is already in use.<br><br>Please try again. <br>";
mysqli_close($dbc);
header("Location: ../index.php");// Bail Out
} else {
$WhatAction = "Existing User";// Good to Go
}
break;
case 2:// Security ID=2. Administrator UPDATING Existing User Data. Check to see if Username Exists anywhere in the database...
$UsernameQuery = "SELECT COUNT(*) AS UsernameCount FROM customers
WHERE UserName = '" . $username . "' AND CustomerID != " . $CustomerID2Update ;// Build the query
$rs = @mysqli_query ($dbc, $UsernameQuery); // Return the Result Set
WHILE ($row = mysqli_fetch_array($rs, MYSQLI_ASSOC)) {// Fetch the data
$UsernameCount = $row['UsernameCount'];
}
if($UsernameCount > 0) {// Username Already Exists...
$_SESSION['Message'] = "Note: The User Name: <i><font color=red>" . $Username . "</font></i> is already in use.<br><br>Please try again. <br>";
mysqli_close($dbc);
header("Location: ../index.php");// Bail Out
} else {
$WhatAction = "Administrator";// Good to Go
}
break;
default:// case else: Security ID=0. NEW USER. Check to See if Username Exists anywhere in the database...
$UsernameQuery = "SELECT COUNT(*) AS UsernameCount FROM customers
WHERE UserName = '" . $username . "'" ;// Build the query
$rs = @mysqli_query ($dbc, $UsernameQuery); // Return the Result Set
WHILE ($row = mysqli_fetch_array($rs, MYSQLI_ASSOC)) {// Fetch the data
$UsernameCount = $row['UsernameCount'];
}
if($UsernameCount > 0) {// Username Already Exists...
$_SESSION['Message'] = "Sorry, the User Name: <i><font color=red>" . $Username . "</font></i> is already in use.<br><br>Please try again. <br>";
mysqli_close($dbc);
header("Location: ../index.php");// Bail Out
} else {
$WhatAction = "New User";// Good to Go
}
break;
}
switch ($WhatAction) {
case "New User":// INSERT New Db Record
$query = "INSERT INTO customers (CustomerID,FirstName,MiddleInitial,LastName,
Address,Address2,City,State,Zip,UserName,Password,Email,Phone,DateOfBirth)
VALUES (NULL, '" . $firstname . "', '" . $middleinitial . "', '" . $lastname . "', '" . $address .
"', '" . $address2 . "', '" . $city . "', '" . $state . "', '" . $zipcode . "', '" .
$username . "', '" . $password . "', '" . $email . "', '" . $phone . "','" . $birthdate . "', NULL , NULL)" ;
case "Existing User":// UPDATE Existing Db Record
$query = "UPDATE customers
SET FirstName = '" . $firstname . "',
MiddleInitial = '" . $middleinitial . "',
LastName = '" . $lastname . "',
Address = '" . $address . "',
Address2 = '" . $address2 . "',
City = '" . $city . "',
State = '" . $state . "',
Zip = '" . $zipcode . "',
UserName = '" . $username . "',
Password = '" . $password . "',
Email = '" . $email . "',
Phone = '" . $phone . "',
DateOfBirth = '" . $birthdate . "',
WHERE CustomerID = '" . $_SESSION['CustomerID'] ;
break;
case "Administrator":// UPDATE Existing Db Record as Edited by Administrator
$query = "UPDATE customers
SET FirstName = '" . $firstname . "',
MiddleInitial = '" . $middleinitial . "',
LastName = '" . $lastname . "',
Address = '" . $address . "',
Address2 = '" . $address2 . "',
City = '" . $city . "',
State = '" . $state . "',
Zip = '" . $zipcode . "',
UserName = '" . $username . "',
Password = '" . $password . "',
Email = '" . $email . "',
Phone = '" . $phone . "',
DateOfBirth = '" . $birthdate . "',
WHERE CustomerID = " . $CustomerID2Update ;
break;
}
mysqli_query($dbc,$query);
$newid = mysqli_insert_id($dbc);
if ($newid!=0 AND $WhatAction = "New User") {
$_SESSION['CustomerID'] = $newid;
$_SESSION['FirstName'] = $firstname;
$_SESSION['SecurityID'] = 1;
}
mysqli_close($dbc);
switch ($WhatAction) {
case "New User":
$_SESSION['Message'] = "New User Successfully Added. <br>";
header("Location: index.php");
print($query);
print("<br>");
break;
case "Existing User":
$_SESSION['Message'] = "Existing User Data Successfully Updated. <br>";
header("Location: index.php");
break;
case "Administrator":
header("Location: index.php?menukey=6");//Maintenance Page
break;
}
?>