使用userform输入Database PHP连接[关闭]
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?
A.) You probably don't want your users to connect to MySql through your server
These should be YOUR login details from MySql, typically you create a user in MySql (maybe phpmyadmin?) and fill the details in here:
$mysqli_connection = new MySQLi('localhost', $login, 'secret', 'edgeserver');
Then you would select from a table you create that holds your users.
B.) You can store user-names in sessions, but that's getting complicated, and I’d recommend you get it working where you just login first.
Possibly this ( I just googled it ) might help you.