drza10046 2017-01-13 22:59
浏览 50

与localhost服务器建立PHP连接?

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

        }



    }


?>
  • 写回答

1条回答 默认 最新

  • duanduo7400 2017-01-13 23:07
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)