How to connect HTML/PHP to MySQL server?

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?
11 answers Last reply
More about connect html php mysql server
  1. I mean, obviously the closing selectors and stuff and the formating didn't get copied correctly here....
  2. Just follow the w3c example and it should work fine. When you connect to a database, you get a connection object back that you use to perform the query against.

    See this example:

    http://www.w3schools.com/php/php_mysql_select.asp

    For your usage, you would just modify it accordingly. Remember to check and see if the connection is valid and return the connection at the end.
  3. 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?
  4. I'm a PHP and SQL nuub, I'm more of a HTML and CSS guy, lol.
  5. *noob
  6. 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();
    ?>
  7. 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?
  8. What tthe heck.... now, when I look and click 'view source' for the file, there is no code in here. Why is it hidden/not there?
  9. wildfire707 said:
    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();
    ?>


    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...
  10. 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!
  11. wildfire707 said:
    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!


    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!
Ask a new question

Read More

Servers Connection HTML MySQL Login PHP