anton_1 Posted August 11, 2011 Share Posted August 11, 2011 Hey guys any help will be greatly appreciated! What im trying to do is display the selected row from database when clicked but i cannot get php to get the id number? Here is the code: <?php mysql_connect('localhost', 'web101-db1-1', 'mypassword'); mysql_select_db('web101-db1-1'); $query="SELECT * FROM Tickets"; $result=mysql_query($query); $num=mysql_num_rows($result); mysql_close(); ?> <table border="1"> <tr> <th><font face="Arial, Helvetica, sans-serif">TicketID</font></th> <th><font face="Arial, Helvetica, sans-serif">Date Created</font></th> <th><font face="Arial, Helvetica, sans-serif">Status</font></th> <th><font face="Arial, Helvetica, sans-serif">Assigned To</font></th> <th><font face="Arial, Helvetica, sans-serif">Requested By</font></th> <th><font face="Arial, Helvetica, sans-serif">Description</font></th> <th><font face="Arial, Helvetica, sans-serif">View Ticket</font></th> </tr> <?php $i=0; while ($i < $num) { $f1=mysql_result($result,$i,"TicketID"); $f2=mysql_result($result,$i,"DateCreated"); $f3=mysql_result($result,$i,"Status"); $f4=mysql_result($result,$i,"AssignedTo"); $f5=mysql_result($result,$i,"RequestedBy"); $f6=mysql_result($result,$i,"Description"); ?> <tr> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f6; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo "<a href='view.php?id={$row['id']}'>View Ticket</a>" ?></font></td> //where i try and get the id </tr> <?php $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/244502-fetch-data-of-selected-row/ Share on other sites More sharing options...
TeNDoLLA Posted August 11, 2011 Share Posted August 11, 2011 With the method you are using. This line <?php echo "<a href='view.php?id={$row['id']}'>View Ticket</a>" ?> should be <?php echo "<a href='view.php?id={$f1}'>View Ticket</a>" ?> Link to comment https://forums.phpfreaks.com/topic/244502-fetch-data-of-selected-row/#findComment-1255892 Share on other sites More sharing options...
cunoodle2 Posted August 11, 2011 Share Posted August 11, 2011 Take another look at your code. You are trying to echo "$row['id']" but you never set it any where. You could do this in the fetch area.. $current_id=mysql_result($result,$i,"id"); And then this with the echo.. <td><font face="Arial, Helvetica, sans-serif"><?php echo "<a href='view.php?id=$current_id'>View Ticket</a>" ?></font></td> //where i try and get the id </tr> Link to comment https://forums.phpfreaks.com/topic/244502-fetch-data-of-selected-row/#findComment-1255893 Share on other sites More sharing options...
anton_1 Posted August 11, 2011 Author Share Posted August 11, 2011 Hey Gys that was awesome advice. it now get's the id but when I proceed it doesnt fetch the row according to the id : <html> <head> <title>Retrieve data from database</title> </head> <body> <dl> <?php $f1 = $_GET['f1']; echo $f1; mysql_connect('localhost', 'web101-db1-1', 'mypassword'); mysql_select_db('web101-db1-1'); $id = mysql_real_escape_string($_GET['$currentid']); // or $id = (int) $_GET['id']; if($id){ $strSQL = "SELECT * FROM Tickets WHERE TicketID=" . $_GET["$id"]; // Get data from the database depending on the value of the id in the URL $rs = mysql_query($strSQL); // Loop the recordset $rs while($row = mysql_fetch_array($rs)) { // Write the data of the person echo "<dt>Id Number:</dt><dd>" . $row["TicketID"] . "</dd>"; } }else { echo "No ID Found"; } // Close the database connection mysql_close(); ?> </dl> <p><a href="login_success.php">Return to Helpdesk</a></p> </body> </html> Link to comment https://forums.phpfreaks.com/topic/244502-fetch-data-of-selected-row/#findComment-1255901 Share on other sites More sharing options...
cunoodle2 Posted August 11, 2011 Share Posted August 11, 2011 Please use the "code" or "php" tags when showing your code. Here is your code corrected.. <?php $f1 = intval($_GET['f1']); //I added "intval()" so the passed item can ONLY be a number echo $f1; //good use of debugging. an echo to the screen is the easiest way to figure out what is going on mysql_connect('localhost', 'web101-db1-1', 'mypassword'); mysql_select_db('web101-db1-1'); //next line seems unnecessary so I will just comment it out. You DON'T need "$id" as it is ALREADY in "$f1" //$id = mysql_real_escape_string($_GET['$currentid']); // or $id = (int) $_GET['id']; //if($id){ This line not needed. You need to check "$f1" NOT "$id" if($f1) { $strSQL = "SELECT * FROM Tickets WHERE TicketID=".$f1." LIMIT 1;"; // Get data from the database depending on the value of the id in the URL $rs = mysql_query($strSQL); // Loop the recordset $rs while($row = mysql_fetch_array($rs)) { // Write the data of the person echo "<dt>Id Number:</dt><dd>" . $row["TicketID"] . "</dd>"; } } else echo "No ID Found"; // Close the database connection mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/244502-fetch-data-of-selected-row/#findComment-1255971 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.