How to fix "NOTICE: undefined variable SERVER".
Posted by: Chukwura (197.210.227.---)
Date: September 12, 2017 09:32PM

Hi, I am try to build web registration form to enable users register and then login to the website, but on running the code I get this : Notice: Undefined variable: SERVER in c:\wamp\www\php\register.php on line 10.

The following is the code that lead to the error:

<?php
// Include config file
require_once 'config.php';

// Define variables and initialize with empty values
$username = $password = $confirm_password = "";
$username_err = $password_err = $confirm_password_err = "";

// Processing form data when form is submitted
if ($SERVER["REQUEST_METHOD"] == "POST"winking smiley {

// Validate username
if (empty(trim($_POST["username"]))) {
$username_err = "Please enter a username";
} else {
// Prepare a select statement
$sql = "SELECT id FROM demo_tb WHERE username = ?";

if ($stmt = mysqli_prepare($link, $sql)) {
// Bind variables to the prepared statement as parameters
msqli_stmt_bind_param($stmt, "s", $param_username);

// set parameters
$param_username = trim($_POST["username"]);

// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
/* Store result */
mysqli_stmt_store_result($stmt);

if (mysqli_stmt_num_rows($stmt) == 1) {
$username_err = "This username is already taken.";
} else {
echo "Oops! Something went wrong. Please try again later.";
}
}

// Close statement
mysqli_stmt_close($stmt);
}

// validate password
if (empty(trim($_POST['password'])) < 0 ) {
$password_err = "Password must have at least 6 characters. ";
} else {
$password = trim($_POST['password']);
}

// Validate confirm password
if (empty(trim($_POST["confirm_password"]))) {
$confirm_password_err = "Please confirm password.";
} else {
$confirm_password = trim($POST['confirm_password']);

if ($password != $confirm_password) {
$confirm_password_err = 'Password did not match';
}

// Check input errors before inserting in database
if (empty($username_err) && empty($password_err) && empty($confirm_password_err)) {
// Prepare an insert statement
$sql ="INSERT INTO demo_tb (username, password) VALUES (?, ?)";

if ($stmt = mysqli_prepare($link, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);

// Set parameters
$param_username = $username;
$param_password = password_hash($password, PASSORD_DEFAULT); //Create a pasword hash

// Attempt to execute the prepared statement
if (mysqli_stmt_execute($stmt)) {
// Redirect to login page
header("location: login.php"winking smiley;
} else {
echo "Something went wrong. Please, try again later.";
}
}

// Close statement
mysqli_stmt_close($stmt);
}

//Close connection
mysqli_close($link);
}
}
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
<link rel="stylesheet" href="[maxcdn.bootstrapcdn.com.bootstrap];
<style type="text/css">
body { font: 14px sans-serif; }
.wrapper { width:350px; padding 20px; }
</style>
</head>
<body>
<div class="wrapper">
<h2>Sign Up</h2>
<p>Please fill this form to create an account.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">
<div class="form-group <?php echo (!empty($username_err)) ? 'has-error': '' ?>">
<label>Username:<sup>*</sup></label>
<input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
<span class="help-block"><?php echo $username_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($password_err)) ? 'has-error': '' ?>">
<label>Password:<sup>*</sup></label>
<input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
<span class="help-block"><?php echo $password_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error': '' ?>">
<label>Confirm Password:<sup>*</sup></label>
<input type="password" name="confirm_password" class="form-control" value="<?php
echo $confirm_password; ?>">
<span class="help-block"><?php echo $confirm_password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">
<input type="reset" class="btn btn-primary" value="Reset">
</div>
<p>Already have an account? <a href="login.php">Login here</a></p>
</form>
</div>
</body>
</html>

Please help me find out my mistake(how I did not define "SERVER".

I also have the the config.php file:

<?php
/* Database credential */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'patony');
define('DB_NAME', 'demo');

/* Attempt to connect to MySQL database */
$link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

// check connection
if ($link === false) {
die ("Error: Could not connect . " . mysqli_connect_error());
}
?>

Options: ReplyQuote
Re: How to fix "NOTICE: undefined variable SERVER".
Posted by: Otomatic (Moderator)
Date: September 12, 2017 10:09PM

Hi,

> I get this : Notice: Undefined variable: SERVER in c:\wamp\www\php\register.php on line 10.
- First, this forum is not intended to debug all the errors of the PHP scripts of Wampserver users.
- Second, you have to learn that when PHP says there is an error, there is actually an error in your script.
- Third, learn to read the error messages, but not diagonally.

- Undefined variable: SERVER
Re-read the line of incriminated code again and again, I put you on the way, it is this line:
if ($SERVER["REQUEST_METHOD"] == "POST" ) {

You still do not see why the SERVER variable is undefined?
That is, this variable does not exist!

So read the PHP documentation which I put the link: [php.net] and if after this reading you still do not see where the error is, you must stop making computers and instead go to plant cabbages and turnips.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: How to fix "NOTICE: undefined variable SERVER".
Posted by: Chukwura (197.210.28.---)
Date: September 20, 2017 12:43PM

Thank you very much.

Please bear with me for my carelessness.

I later discovered and corrected the error which should be "$_SERVER" and not "$SERVER".

Kind regards.

Options: ReplyQuote


Sorry, only registered users may post in this forum.