-5

My php script for registry is not working, its returning

Warning: mysql_query() [function.mysql-query]: Access denied for user '**'@'localhost' (using password: NO) in /home/**/public_html/register.php on line 10

and

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/**/public_html/register.php on line 10

Register.php

    <?php
    require('connect.php');
    // If the values are posted, insert them into the database.
    if (isset($_POST['username']) && isset($_POST['password'])){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];

        $query = "INSERT INTO user (username, password, email) VALUES ('$username', '$password', '$email')";
        $result = mysql_query(!$query);
        if(!$result){
            $msg = "User Created Successfully.";
        }
    }
    ?>
<!DOCTYPE html>
<html>
<head>
<title>InfamousBurns - Register</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
    <!-- Form for logging in the users -->
<div>
<?php
    if(isset($msg) & !empty($msg)){
        echo $msg;
    }
 ?>
<h1 id="post">Register</h1>
<form action="" method="POST">
    <p id="post"><label>User Name : </label>
    <input id="username" type="text" name="username" placeholder="username" />
    </p>

    <p id="post"><label>E-Mail&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : </label>
     <input id="password" type="email" name="email" required placeholder="vivek@email.com" />           </p>

     <p id="post"><label>Password&nbsp;&nbsp; : </label>
     <input id="password" type="password" name="password" placeholder="password" /></p>

    <a id="button" href="login.php">Login</a>
    <input  id="post" type="submit" name="submit" value="Register" />
    </form>

</div>
</body>
</html>
ElectricRouge
  • 1,239
  • 3
  • 22
  • 33
Thomas
  • 1

2 Answers2

0

This happening because in "connect.php" your details to the MySQL database are not correct, because you haven't posted the actual connect.php details I can only assume that you've misspelt something.

EDIT 1:

Where you have

$selected = mysql_select_db($dbhandle);

change it to

$selected = mysql_select_db($mysql_database);

EDIT 2

Can I also suggest using PDO instead... example below for db connection below

<?php
    $host = "host";
    $user = "username";
    $pass = "pass";
    $db = "database name";

    try {
        $link = new PDO("mysql:host=$host;dbname=$db",$user,$pass);
    } catch (PDOException $e) {
        die();
    }

Of course this means having to change your SQL queries around a little bit and use prepared statements instead, I'm sure you can find some help for it online though

user3263978
  • 193
  • 1
  • 2
  • 14
  • I would assume from `using password: NO` that the password is not getting passed to `mysql_connect`. – Mike Feb 25 '14 at 19:27
  • I wouldn't echo `$e->getMessage()` to the browser. With a failed connection it will echo your connection credentials in plain text. – Mike Feb 25 '14 at 19:37
  • @Mike good point... it is merely for testing purposes to check the credentials upon fail. Will take that part out – user3263978 Feb 25 '14 at 19:43
0

If the login information is correct check the location of the file. It looks like connect.php should be in the public_html directory based on the way you are including it in Register.php. If its in a subdirectory or something make sure you are using the correct path in the require() function. And make sure any capital letters are capitalized as normal when using file locations.

JStephen
  • 1,059
  • 3
  • 13
  • 28