sql server - MSSQL and PHP: Select specific row based on which button is clicked -


i making website mock book database using mssql users can search different books , select particular books might add list of favorites under account name. problem having have no idea how differentiate book selection want add favorites because can't figure out how set isbn of book, uniquely identifies it, php session variable. if can shed light on appreciate it, have been trying figure out day.

//set 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 run if searching book title if(isset($_get['searchbook'])){ $searchbook = $_get['searchbook']; $query = "select book.isbn, title, author, publisher, numberofpages, language, locationname, listprice book, price, location title '%$searchbook%' , book.isbn = price.isbn , price.locationid = location.locationid"; }  //search run searching book author if(isset($_get['searchauthor'])){ $searchauthor = $_get['searchauthor']; $query = "select book.isbn, title, author, genre, publisher, numberofpages, language, locationname, listprice book, price, location author '%$searchauthor%' , book.isbn = price.isbn , price.locationid = location.locationid"; }  //store query result $query_result = mssql_query($query, $connection) or die( "error: query wrong");  //set table 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 $eachline) {     echo "<td> $eachline </td>"; } echo "<td><input name=\"".$line['index']."\" type=\"submit\" value=\"add favorites\"></td>"; echo "</tr>\n"; } echo "</table>"; echo "</form>"; 

not sure if relevant following code best attempt @ getting value of isbn corresponds row of button being clicked, doesn't work had hope.

//get isbn  $data = mssql_fetch_assoc($query_result); $isbn = $data['isbn']; echo $isbn; 

here code addfavorite.php form action set to. file needs know user adding book favorite , book adding list.

//set 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 query $query = "insert favorites values(\"$user\",\"**i need session variable or go here\")";  //store query result $query_result = mssql_query($query, $connection) //or die( "error: query wrong"); 

any appreciated. know it's alot of information , if there doesn't make sense or have forgotten provide please let me know. thanks.

edit have tried using button instead of using input value of button not setting reason.

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 $eachline) {     echo "<td> $eachline </td>"; } echo "<td><button name=\"favoritebutton\" type=\"submit\" value=\"".$line['isbn']."\">add favorites</button></td>"; echo "</tr>\n"; } echo "</table>"; echo "</form>"; 

edit 2 got working, helping! partial code problematic posted below in working condition.

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 $eachline) {     echo "<td> $eachline </td>"; } echo "<td><button name=\"favoritebutton\" type=\"submit\" value=\"".$line[0]."\">add favorites</button></td>"; echo "</tr>\n"; } echo "</table>"; echo "</form>"; 

use button-element instead of input-element. way, can use 'value'-attribute of element pass correct value.

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

although suggest using ajax instead of above approach this: use onclick event button execute javascript calls seperate php-file , passes correct isbn-number. added database , original page should refreshed or part of page reloaded.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -