简体   繁体   English

与本地主机服务器建立PHP连接?

[英]Establishing PHP connection with localhost server?

I am having an issue with connecting my PHP script to the database on my localhost server.我在将 PHP 脚本连接到本地主机服务器上的数据库时遇到问题。 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.作为 PHP 的新手,我对它的了解还不够多,无法发现我可能犯的任何错误,或者它们的含义。 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.任何关于这个主题的帮助将不胜感激,因为网上有很多关于 PHP 的信息,但我宁愿知道是什么导致了这个错误,以便将来防止它发生。

Here are the errors appearing in the browser console:以下是浏览器控制台中出现的错误:

Failed to load resource: the server responded with a status of 404 (Not Found)加载资源失败:服务器响应状态为 404(未找到)

ReferenceError: Can't find variable: $参考错误:找不到变量:$

And the UNIX socket code from MAMP (I don't know where this would fit in):还有来自 MAMP 的 UNIX 套接字代码(我不知道这适合放在哪里):

$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:和 PHP 代码:

    //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.首先,您使用的旧 mysql 函数已在最新版本的 PHP 中完全删除。 Use the mysqli functions instead.请改用mysqli函数。 The old functions like mysql_connect and mysql_query have been deprecated . mysql_connect 和 mysql_query 等旧函数已被弃用 You need to look for all occurrences of mysql_ in this code and think about replacing each command with its new counterpart.您需要在此代码中查找所有出现的mysql_并考虑将每个命令替换为新的对应命令。

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 .您应该始终检查mysqli_connect返回的值以查看它是否确实有效或返回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.这样做时,您将 $db 重新定义为其他内容。

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.您应该检查 mysqli_query 的结果(而不是代码中的 mysql_query)以查看它返回的内容。 It should be TRUE if the INSERT query worked.如果 INSERT 查询有效,它应该是 TRUE。

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.并且在您重定向之后,您无法调用 exit,这意味着您的重定向尝试之后的所有代码最终可能最终实际执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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