我正在尝试连接到数据库,它已成功连接但无法使用数据库。我对PHP的这个概念很新。如果我在某个地方出错,请帮帮我,让我知道
<?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();
?>
答案 0 :(得分:0)
我尝试执行您的代码段。一切都很好。我确保照顾以下事项:
create database bvh;
php <filename.php>
输出显示“已成功连接”。以下是输出:
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
PHP Notice是因为数据没有发布到PHP。但是连接成功了,我可以通过确保上述步骤来访问数据库。