简体   繁体   中英

Establishing PHP connection with localhost server?

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";

        }



    }


?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM