Scenario: HTML page with simple form (action = “post”). At top of page is PHP section that initialises the variables and processes the $_POST.
The initialisation, which consists of setting up blank variables, and retreiving data from database and assigning to variables if found, is contained in if (! isset($loop) ) {…} structure and within that structure the $loop variable is defined, so that the structure is ignored in future step throughs of the PHP code (e.g. when the submit button is pressed)
It appears to work ok, except everytime the submuit button is pressed the construct is enterred.(see call to alert in construct that displays everytime SUBMIT is pressed)
The $_POST does some validation and displays error messages and returns focus to form if problems, otherwise saves data to database, and closes page. This bit appears to work ok.
Having come from other (desktop) programming languages I am struggling to see why the $looped is being ignored.
<?php
include 'inc/accsessconn.php';
if (!isset($looped)) {
// define variables and set to empty values
$type = $username = $password = $userlevel = "";
$inpname = $inplevel = $inppass1 = $inppass2 = "";
$looped = "In setup";
echo "<script>alert('$looped');</script>";
if (isset($_GET["edit_id"])) {
$edit_id = $_GET["edit_id"];
if ($edit_id > 0) {
$acc_query = $conn->query("SELECT * FROM `admin` WHERE `admin_id` = '$edit_id'") or die(mysqli_error());
$acc_fetch = $acc_query->fetch_array();
$username = $acc_fetch['username'];
$userlevel = $acc_fetch['level'];
$type = "Amend User: " . $username;
$inpname = $username;
$inplevel = $userlevel;
} else {
$type = "Add New User";
$userlevel = "0";
}
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
//...............
}
function tidy_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>