无法使用database.DB_NAME

I'm trying to connect to the database, it is connected successfully but not able to use database. I;m very new to this concept of PHP. Please help me out and let me know if I'm going wrong somewhere

<?php
    define('DB_NAME', 'bvh');
    define('DB_USER', 'root');
    define('DB_PASSWORD', '');
    define('DB_HOST', 'localhost');

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

    if(!$link){
        die('could not connect: '.mysql_error());
    }
    echo 'Connected Successfully<br>';
    $db_selected = mysql_select_db(DB_NAME, $link);

    if(!$db_selected){
        die('Cannot use '.DB_NAME.':'. mysql_error());
    }
    $yname=$_POST['yname'];
    $faculty=$_POST['faculty'];
    $class=$_POST['class'];
    $uname=$_POST['uname'];
    $email=$_POST['email'];
    $pwd=$_POST['pwd'];
    $rpwd=$_POST['rpwd'];

    $sql = "INSERT INTO students (yname, faculty, class, uname, email, pwd, rpwd) VALUES('$yname', '$faculty', '$class', '$uname', '$email', '$pwd', '$rpwd')";

    if(!mysql_query($sql)){
        die('Error: ' .mysql_error());
    }
    mysql_close();
    ?>

I tried executing your code snippet. Everything works well. I ensured to take care of the following:

  • Database bvh exists Logged into MySQL prompt, created database bvh using command create database bvh;
  • Then created the table students within the database bvh
  • Ran your php code from my command line using php interpreter. php <filename.php>
  • The output showed "Connected Successfully". Below is the output:

    Connected Successfully<br>PHP Notice:  Undefined index: yname in /var/www/test.php on line 18
    PHP Notice:  Undefined index: faculty in /var/www/test.php on line 19
    PHP Notice:  Undefined index: class in /var/www/test.php on line 20
    PHP Notice:  Undefined index: uname in /var/www/test.php on line 21
    PHP Notice:  Undefined index: email in /var/www/test.php on line 22
    PHP Notice:  Undefined index: pwd in /var/www/test.php on line 23
    PHP Notice:  Undefined index: rpwd in /var/www/test.php on line 24
    

The PHP Notice is because the data is not posted to the PHP. But the connection was successful and I could access the database by ensuring the above steps.