How to connect HTML/PHP to MySQL server?

Ironarmygeneral

Estimable
Jan 21, 2015
42
0
4,580
0
I tried to make a simple login from, and here is what I have. But whenever I try to open the file on my site, it gives me a 500 error.

Code:

<!DOCTYPE html PUBLIC>
<html>
<head>
<meta charset="utf-8" />
<title>Login</title>

<body>

<?php

$host = "localhost";
$user = "(My username)";
$pass = "(my password)";
$db = "(My database name)";

if {isset($_POST['username'])) {
mysql_connect($host, $user, $pass);
mysql_select_db($db);

$username = $_POST['username'];
$password = $_POST['password'];
$sql = SELECT * FROM users WHERE username'".$username."' AND password='".$password."' LIMIT 1";
$res = mysql_query($sql);
if {mysql_num_rows($res) == 1) {
echo "You have successfully logged in.";
exit();
} else }
echo "Invalid login info.";
exit();
}
}

?>

<form method="post" action="login.php">
Username: <input type="text" name="username" /><br /><br />
Password: <input type="password" name="password" /><br /><br />
<input type="submit" name="submit" value="Log In" />
</form>
</body>
</html>

Anyone know how I can fix this?
 

Ironarmygeneral

Estimable
Jan 21, 2015
42
0
4,580
0
My problem is I don't understand much that w3 schools says. So that example didin;t work well unfortunately. Minnd maybe giving an example code and we can work from that?
 

wildfire707

Distinguished
Dec 29, 2011
59
0
18,610
17
For your usage, it would just be a simple matter of changing the action slightly. By the way, were a couple of errors in your SQL - your select statement should have started with a quote and you did not have an equals sign after the username field.

This should work after you adjust it to match your database info (set the variables at the start):

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$table = "users"
$loginname = $_POST['username'];
$loginpw = $_POST['password'];

// verify that the login data was sent in
if ((!isset($loginname))||(!isset($loginpw))) {
die("Login information is missing");
}

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM " . $table . " WHERE username='" . $loginname . "' AND password='" . $loginpw . "'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// we got a user name and password match
echo "You have successfully logged in.";
} else {
// no match occurred
echo "Invalid login info.";
}
$conn->close();
?>
 

Ironarmygeneral

Estimable
Jan 21, 2015
42
0
4,580
0
Thanks. This code iis more familiar, and is also more easy tto understand. :) Also, itt still gives me a 500 error, butt I'm guessinng tthat's due to incorrect SQL DB info. Is the servername the "sql(Blah blah blah"? I'm guessing?
 

Ironarmygeneral

Estimable
Jan 21, 2015
42
0
4,580
0


It displays:

connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "SELECT * FROM " . $table . " WHERE username='" . $loginname . "' AND password='" . $loginpw . "'"; $result = $conn->query($sql); if ($result->num_rows > 0) { // we got a user name and password match echo "You have successfully logged in."; } else { // no match occurred echo "Invalid login info."; } $conn->close(); ?>

when I open it as a file from my PC. But under that it has the login form area in correct format...
 

wildfire707

Distinguished
Dec 29, 2011
59
0
18,610
17
The PHP code should not appear to a client. It gets processed on the server.

You may just want to do a sanity check on the PHP running on the server. Try changing the page so it just reads:

<?php

phpinfo();

?>

If this works, it should report all of the PHP information on the system, including the SQL libraries that are being used.

From there, you could try the simplest version of the code that I sent, to just test a simple connection:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

echo "Database connection succeeded.";

$conn->close();
?>

Good luck!
 

Ironarmygeneral

Estimable
Jan 21, 2015
42
0
4,580
0


Thanks, I'll try that. I tried contacting my server administrators and hosting service, and they said it was happening to a lot of people where it would give odd errors, like it was giving me a 500 error. They said it was just issues with the servers, and they were getting it fixed. Thanks!
 
Thread starter Similar threads Forum Replies Date
E Apps General Discussion 5
D Apps General Discussion 3
C Apps General Discussion 1
B Apps General Discussion 0
R Apps General Discussion 0
P Apps General Discussion 1
D Apps General Discussion 7
V Apps General Discussion 3
G Apps General Discussion 1
T Apps General Discussion 12
S Apps General Discussion 0
dexter254 Apps General Discussion 0
A Apps General Discussion 6
D Apps General Discussion 3
S Apps General Discussion 1
Q Apps General Discussion 1
L Apps General Discussion 2
E Apps General Discussion 5
D Apps General Discussion 6
D Apps General Discussion 3

Similar threads


ASK THE COMMUNITY