-3

I am having an issue with connecting my PHP script to the database on my localhost server. I have posted the code below, it is to enable user registration on the site. The input boxes appear as they should when I run the code, but nothing updates to the database when I try and complete a sign up. As a novice with PHP I don't know enough about it to spot any errors I might be making, or what they mean. Any help on this subject would be appreciated as there is a lot of info about PHP online, but I would rather know what was causing this error in order to prevent it in the future.

Here are the errors appearing in the browser console:

Failed to load resource: the server responded with a status of 404 (Not Found)

ReferenceError: Can't find variable: $

And the UNIX socket code from MAMP (I don't know where this would fit in):

$user = 'root';
$password = 'root';
$db = 'inventory';
$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';

$link = mysql_connect(
   $socket, 
   $user, 
   $password
);
$db_selected = mysql_select_db(
   $db, 
   $link
);

And the PHP code:

    //connect to database
    $db = mysql_connect("localhost", "root", "", "authentication");

    if (isset($_POST['register_btn'])) {
        session_start();
        $username =mysql_real_escape_string($_post['username']);
        $email =mysql_real_escape_string($_post['email']);
        $password =mysql_real_escape_string($_post['password']);
        $password2 =mysql_real_escape_string($_post['password2']);


        if ($password == $password2) {
            //create user
            $password = md5($password); //hash password before storing for security 
            $sql = "INSERT INTO users(username, email, password) VALUES('$username', '$email', '$password')";
            mysql_query($db, $sql);
            $_SESSION['message'] = "Find a Table";
            $_SESSION['username'] = $username;
            header("location: HomePage.html"); //redirect to homepage 
        }else{
            $_SESSION['message'] = "Your passwords must match to proceed";

        }



    }


?>
Phiter
  • 14,570
  • 14
  • 50
  • 84
Andrew
  • 33
  • 1
  • 7
  • 4
    please dont use mysql. That Functions are deprecated in PHP 7.0. Use mysqli: http://php.net/manual/de/class.mysqli.php – Sysix Jan 13 '17 at 23:03
  • my MAMP server is set to PHP 5.6, and I would like to learn about mysql as it is so pervasive on the web. – Andrew Jan 13 '17 at 23:05
  • 2
    I think you want to use `$_POST` instead of `$_post` – Phiter Jan 13 '17 at 23:05
  • my experience is more with sqlsrv so i could be way off base, but where you have socket on mine i just use "LOCALHOST/SQLEXPRESS" (obviously different to suit yours) run a connection test first, ie. ` if($db) { echo "sql connections successful"; } else { die!; } this will tell you if its connection, secondly, check that your sql ports are listening (should be 1433) in the sql manager – Craig B Jan 13 '17 at 23:06
  • Sry. Mysql is since PHP 5.5 deprected in in PHP 7.0 removed. Its not a problem to learn it, but learn it right. The Syntax has changed a little bit, so you learn right for the future :) – Sysix Jan 13 '17 at 23:09
  • @Andrew it's not mysql that's outdated, it's just the PHP library with functions that starts with mysql_. Instead of mysql or mysqli you should really learn PDO. It's simple, awesome and there's a lot of tutorials. – Evert Jan 14 '17 at 00:05
  • **WARNING**: Writing your own access control layer is not easy and there are many opportunities to get it severely wrong. Please, do not write your own authentication system when any modern [development framework](http://codegeekz.com/best-php-frameworks-for-developers/) like [Laravel](http://laravel.com/) comes with a robust [authentication system](https://laravel.com/docs/5.3/authentication) built-in. At the absolute least follow [recommended security best practices](http://www.phptherightway.com/#security) and **never store passwords with a uselessly weak hash like MD5**. – tadman Jan 14 '17 at 01:19
  • **WARNING**: If you're just learning PHP, please, do not learn the obsolete [`mysql_query`](http://php.net/manual/en/function.mysql-query.php) interface. It's awful and has been removed in PHP 7. A replacement like [PDO is not hard to learn](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/) and a guide like [PHP The Right Way](http://www.phptherightway.com/) helps explain best practices. Make **sure** your user parameters are [properly escaped](http://bobby-tables.com/php) or you will end up with severe [SQL injection bugs](http://bobby-tables.com/). – tadman Jan 14 '17 at 01:19

1 Answers1

0

Where to start? So many problems.

First off, you are using the OLD mysql functions which have been removed entirely in recent versions of PHP. Use the mysqli functions instead. The old functions like mysql_connect and mysql_query have been deprecated. You need to look for all occurrences of mysql_ in this code and think about replacing each command with its new counterpart.

You define this code to connect:

$user = 'root';
$password = 'root';
$db = 'inventory';
$socket = 'localhost:/Applications/MAMP/tmp/mysql/mysql.sock';

$link = mysql_connect(
   $socket, 
   $user, 
   $password
);
$db_selected = mysql_select_db(
   $db, 
   $link
);

and then you don't use the resulting connection -- even check if it worked. You should always check the value returned by mysqli_connect to see if it actually worked or if it returned FALSE. You reconnect again and don't bother checking to see if it worked:

//connect to database
$db = mysql_connect("localhost", "root", "", "authentication");

And in doing so, you redefine $db to something else.

Also, you run a query without checking whether it succeeded or not:

        mysql_query($db, $sql);
        $_SESSION['message'] = "Find a Table";
        $_SESSION['username'] = $username;
        header("location: HomePage.html"); //redirect to homepage 

You should be checking the result of mysqli_query (not mysql_query as you have in your code) to see what it returned. It should be TRUE if the INSERT query worked.

And after you redirect, you fail to call exit, which means that all the code that follows your redirect attempt may end up actually executing anyway.

Community
  • 1
  • 1
S. Imp
  • 2,833
  • 11
  • 24
  • Okay so do you have any suggestions on how to fix the problem? – Andrew Jan 13 '17 at 23:17
  • I understand what you're saying about the mysqli, but as I say in the question, I didn't include the top piece of code in my script, that is the code given to me on the MAMP site. I didn't include it because I don't know where it fits. Everyone has to start somewhere bro, it might seem elementary to some people, but I don't know what connection you're referring to, or how to check it. – Andrew Jan 14 '17 at 01:01
  • @andrew i believe S. Imp is talking about the connection to the sql database, so if you throw in an If statement to check the database connection i.e. `if (!$link) { echo "Connection Failed!"; }` you will get a message stating that youre not connecting to the database, The second mention is about ensuring the query was successfully executed, again with an if block, assign your query (mysqli_query) to a variable and use the if statement on the variable. You get a true / false boolean response to the query statement. – Craig B Jan 14 '17 at 01:11
  • @Andrew You only need to connect to the database once in any given script unless you are dealing with extremely unusual circumstances. Your script connects twice and uses the wrong function and also the wrong parameters. Use the **mysqli_connect** function. Take a look at the php documentation. It's really helpful and there will be an example there: http://php.net/mysqli_connect – S. Imp Jan 14 '17 at 17:24
  • Okay thank you guys this gives me something to work with! – Andrew Jan 17 '17 at 19:51