I'm trying to enter a username into a text field and click submit then this should connect me to my database. A) What is wrong with the code below:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["login"]))
{
$nameErr = "login is required";
}
else
{
$login = test_input($_POST["login"]);
// check if login only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$login))
{
$nameErr = "Only letters and white space allowed";
}
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Edge User Login</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Login: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
$mysqli_connection = new MySQLi('localhost', $login, 'secret', 'edgeserver');
if ($mysqli_connection->connect_error)
{
echo "Not connected, error: " . $mysqli_connection->connect_error;
}
?>
B) I dont want the user to have to enter a username everytime they run a PHP command. Is there a way of using a master sheet that can be referred back to?