Jump to content

Recommended Posts

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

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>

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>

 

 

 

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();
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.