下面是我的创建表,html文件,其中包含一个包含艺术家的下拉菜单,以及一个基于艺术家的php文件显示该艺术家的所有专辑标题。
我似乎无法让php文件工作。我可以从下拉菜单中获取值,但我不认为我可以使用该值从数据库中获取值。
连接数据库
<?php
// connect.php
// include file providing a function to connect to a mysql database
// include this file on every page that needs to connect to the db.
// usage:
// include('connect.php');
// declare the connection function
function dbConnect ($name, $pw) {
// Create a mysql connection
$connection = mysql_connect("localhost", $name, $pw);
// select the correct database
mysql_select_db("usr_".$name."_1", $connection);
}
// Call the connect function
// First argument is the user name
// second argument is the password
dbConnect('yourUserName', 'yourPassword');
?>
表
create table CD (
CDnum int not null,
Title varchar(40),
Artist varchar(40),
Category varchar(20),
Price numeric(6,2)
) engine = InnoDB;
create table Customer (
Cnum int not null,
CName varchar(25),
CCity varchar(25)
) engine = InnoDB;
create table Buys (
Cnum int not null,
CDnum int not null,
Qty int not null
) engine = InnoDB;
带有下拉菜单的HTML文件。
<HTML>
<HEAD>
<TITLE>
listArtist
</TITLE>
<META NAME="author" CONTENT="">
</HEAD>
<BODY>
<H1>Find Artist's Albums Page</H1>
From a dropdown list of artists, a user should be able to pick an artist and see a list of albums (all fields in the CD table) by that artist.
<HR>
<FORM ACTION="listArtist.php" METHOD="POST"/>
Artist:
<select name="mydropdown">
<option value="Pink Floyd">Pink Floyd</option>
<option value="Pearl Jam">Pearl Jam</option>
<option value="Shania Twain">Shania Twain</option>
<option value="Gwen Stefani">Gwen Stefani</option>
<option value="Vince Gill">Vince Gill</option>
<option value="Dixie Chicks">Dixie Chicks</option>
</select>
<BR>
<BR>
<INPUT TYPE="SUBMIT" Value="List Artist"/>
</FORM>
</BODY>
</HTML>
PHP文件,假设从用户那里获取艺术家并显示所有专辑的标题。
<HTML>
<HEAD>
<title>listArtist.php</title>
</HEAD>
<BODY>
<H1>PrintSchedule FORM ACTION PHP PAGE </H1>
<p>
<?php
// include files:
// connection function
include('connect.php');
$dd = $_POST['mydropdown'];
// used fixed-width font
echo "<pre>\n";
// print ID (name)
echo "Albums of Artist: $dd\n";
// set format string
$fmt = "%40s\n";
// print header line
printf($fmt, "Album Name");
// Get person likes for
$r1 = mysql_query("SELECT Title FROM CD WHERE Artist = '$dd'");
echo($r1[0]);
// loop over courses, printing each one
while ($r = mysql_fetch_row($r1))
{
printf($fmt,$r[0]);
echo($r[0]);
}
// free result set
mysql_free_result($r1);
echo "</pre>\n";
?>
</BODY>
</HTML>