MSSQL和PHP:根据单击的按钮选择特定行

时间:2013-04-05 21:13:54

标签: php sql-server session button row

我正在使用MSSQL创建一个模拟书籍数据库的网站,用户可以搜索不同的书籍,并选择他们可能希望添加到其帐户名下的收藏列表中的特定书籍。我遇到的问题是,我不知道如何区分他们想要添加到他们的收藏夹的书籍选择,因为我无法弄清楚如何设置书籍的ISBN,它唯一地标识它,到php会话变量。如果有人能够对此有所了解,我会很感激,一直试图弄清楚它。

//Set up connection
$connection = mssql_connect("$hostName", "$sqlUsername", "$sqlPassword")
or die("ERROR: selecting database server failed.");

//Select database
mssql_select_db($databaseName, $connection)
or die("ERROR: Selecting database failed");

//Search to run if searching for book title
if(isset($_GET['searchBook'])){
$searchBook = $_GET['searchBook'];
$query = "SELECT BOOK.ISBN, Title, Author, Publisher, NumberOfPages, Language, LocationName, ListPrice FROM BOOK, PRICE, LOCATION WHERE Title LIKE '%$searchBook%' AND BOOK.ISBN = PRICE.ISBN AND PRICE.LocationID = LOCATION.LocationID";
}

//Search to run is searching for a book author
if(isset($_GET['searchAuthor'])){
$searchAuthor = $_GET['searchAuthor'];
$query = "SELECT BOOK.ISBN, Title, Author, Genre, Publisher, NumberOfPages, Language, LocationName, ListPrice FROM BOOK, PRICE, LOCATION WHERE Author LIKE '%$searchAuthor%' AND BOOK.ISBN = PRICE.ISBN AND PRICE.LocationID = LOCATION.LocationID";
}

//Store query result
$query_result = mssql_query($query, $connection)
or die( "ERROR: Query is wrong");

//Set up table to display search results
echo "<form action=\"addFavorite.php\" method=\"POST\" name=\"table\">";
echo "<table border=1 align=\"center\">";
echo "<tr>";

// fetch attribute names
while ($filed = mssql_fetch_field($query_result)) {
echo "<th>".$filed->name."</th>";
}
echo "<th>Favorite</th>";
echo "</tr>";

// fetch table records
while ($line = mssql_fetch_row($query_result)) {
echo "<tr>\n";
foreach ($line as $eachline) {
    echo "<td> $eachline </td>";
}
echo "<td><input name=\"".$line['index']."\" type=\"submit\" value=\"Add To Favorites\"></td>";
echo "</tr>\n";
}
echo "</table>";
echo "</form>";

不确定这是否相关,但以下代码是我获得与所点击的按钮行相对应的ISBN值的最佳尝试,这并不像我希望的那样完全正常工作。

//Get the ISBN 
$data = mssql_fetch_assoc($query_result);
$ISBN = $data['ISBN'];
echo $ISBN;

以下是我的addFavorite.php的代码,它是表单操作设置的位置。这个文件需要知道哪些用户正在添加一本书作为收藏,以及他们将哪些书添加到该列表中。

//Set up connection
$connection = mssql_connect("$hostName", "$sqlUsername", "$sqlPassword")
or die("ERROR: selecting database server failed.");

//Select database
mssql_select_db($databaseName, $connection)
or die("ERROR: Selecting database failed");
$User = $_SESSION['userID'];

//Set up query
$query = "INSERT INTO FAVORITES VALUES(\"$User\",\"**I NEED A SESSION VARIABLE OR SOMETHING TO GO HERE\")";

//Store query result
$query_result = mssql_query($query, $connection)
//or die( "ERROR: Query is wrong");

非常感谢任何帮助。我知道很多信息,如果有什么不合理或者我忘了提供,请告诉我。感谢。

修改 的 我尝试过使用BUTTON而不是使用INPUT,但由于某种原因,按钮的值没有设置为任何东西。

echo "<form action=\"addFavorite.php\" method=\"POST\" name=\"table\">";
echo "<table border=1 align=\"center\">";
echo "<tr>";

// fetch attribute names
while ($filed = mssql_fetch_field($query_result)) {
echo "<th>".$filed->name."</th>";
}
echo "<th>Favorite</th>";
echo "</tr>";

// fetch table records **PROBLEM IN HERE since $line['ISBN'] returns nothing**
while ($line = mssql_fetch_row($query_result)) {
echo "<tr>\n";
foreach ($line as $eachline) {
    echo "<td> $eachline </td>";
}
echo "<td><button name=\"FavoriteButton\" type=\"submit\" value=\"".$line['ISBN']."\">Add To Favorites</button></td>";
echo "</tr>\n";
}
echo "</table>";
echo "</form>";

编辑2 最后得到它的工作,感谢大家的帮助!有问题的部分代码在工作状态下发布。

echo "<form action=\"addFavorite.php\" method=\"POST\" name=\"table\">";
echo "<table border=1 align=\"center\">";
echo "<tr>";

// fetch attribute names
while ($filed = mssql_fetch_field($query_result)) {
echo "<th>".$filed->name."</th>";
}
echo "<th>Favorite</th>";
echo "</tr>";

// fetch table records
while ($line = mssql_fetch_row($query_result)) {
echo "<tr>\n";
foreach ($line as $eachline) {
    echo "<td> $eachline </td>";
}
echo "<td><button name=\"FavoriteButton\" type=\"submit\" value=\"".$line[0]."\">Add To Favorites</button></td>";
echo "</tr>\n";
}
echo "</table>";
echo "</form>";

1 个答案:

答案 0 :(得分:1)

使用BUTTON - 元素而不是INPUT - 元素。这样,您可以使用此元素的&#39; -attribute来传递正确的值。

echo "<td><button name=\"$line['index']\" value=\"$line['ISBN']\" type=\"submit\">Add to favorites</button></td>";

虽然我建议使用AJAX而不是上面的方法:使用按钮上的onclick事件来执行调用单独的php文件的javascript并传递正确的ISBN号。然后将其添加到数据库中,并刷新原始页面或重新加载页面的一部分。