因此,当我运行文件 register.php(其中包含一些要发送到数据库的基本 html 表单)时,它似乎无法找到它正在寻找的特定数据库。所有文件都在同一个文件夹中,拼写正确。
所以我将在我的 register.php 中粘贴所有 html/css 代码之上的 php 代码
<?php
$link=mysql_connect("localhost","root","");
$database='salesinformation';
if (!$link)
die('Failed to connect to Server'.mysql_error());
$db=mysql_select_db($database, $link);
session_start();
if(!$db)
die('Failed to select Data Base '.mysql_error());
if(isset($_GET['process']))
{
$query = "Insert INTO `sales` (Username, Email, Price, Condition, RegisterDate) values('$_POST[Username]', '$_POST[Email]','$_POST[Price]','$_POST[Condition]','$_POST[RegisteredDate]')";
//echo $query; exit;
$result = mysql_query($query) or die(mysql_error());
if(!$result){
$msg = "not Inserted";
}
else
{
$msg = "Inserted";
header("location:ClientList.php?m=".$msg);
}
}
?>
我在包含此代码的同一文件夹中有数据库文件 salesinformation.sql..
CREATE TABLE IF NOT EXISTS `sales` (
`Username` varchar(25) NOT NULL,
`Email` varchar(25) NOT NULL,
`Price` int(10) NOT NULL,
`Condition` varchar(25) NOT NULL,
`RegisterDate` date NOT NULL,
PRIMARY KEY (`Username`)
);
所以当我运行 xampp "http://localhost/register.php
"时会发生这种情况
未能选择数据库未知数据库“销售信息”
最佳答案
And I have the database file salesinformation.sql in the same folder which contains this code..
这并不意味着您拥有 MySQL 数据库。
1) 创建一个名为salesinformation
的MySQL 数据库.
2) 将您的 SQL 文件导入该数据库。
3) 现在你有了一个。现在运行您的代码。
Could you write what to type in the terminal (Since I cant find any GUI of the xampp while running it in ubuntu)? How do I create the "MySQL" database? and how to I import a SQL file into it?
- 使用终端,输入
CREATE DATABASE salesinformation;
- 使用外壳
cd
转到您的salesinformation.sql
所在的目录文件驻留。 - 发出此命令
mysql -u root -p password salesinformation < salesinformation.sql
.
注意:由于我从您的代码中注意到您的 root 密码为空,您可以删除 password
上述命令中的术语。如果您有密码,请在此处输入密码
关于php - 找不到数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20233523/