doulian4762 2012-10-07 05:08
浏览 47

无法使用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();
    ?>
  • 写回答

1条回答 默认 最新

  • douyong1908 2012-10-07 05:53
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错