i created main.php page. this page includes these following php codes,

<?php
$user = "";
$pass = "";
$db = "";
$connect = mysql_connect("", $user, $pass);
if(!$connect)
{
    die ("Could not connect to MySQL");
}
mysql_select_db($db, $connect) or die ("Could not open $db: " .mysql_error());
$sql = mysql_query ( "SELECT slno, refno, gname, aname, dob, cid, cod, status FROM add_details" );
$num_rows = mysql_num_rows( $sql );
print "<table border=1 align=center cellpadding=8>\n";
print "<tr>\n";
print "<th>Serial No</th>";
print "<th>Reference No</th>";
print "<th>Guest Name</th>";
print "<th>Agent Name</th>";
print "<th>Date of Booking</th>";
print "<th>Check in Date</th>";
print "<th>Check out Date</th>";
print "<th>Status</td>";
print "<th>Generate Bill</td>";
print "</tr>\n";
while($a_row = mysql_fetch_row($sql))
{
    print "<tr>\n";
    foreach($a_row as $field)
    print "\t<td>$field</td>\n";
    print "</tr>\n";
}
print "</table>\n";
mysql_close($connect);
?>

this coding fetch all details from database perfectly. but when i click one value (like gname) it should be show all details of that specific row in new tab. in my db includes 20+ fields. please if anyone know help me. i'm just a beginner...

Dani AI

Generated

A concise, safe pattern to add a "Generate Bill" button per row: put a small form or an anchor in the Generate Bill column that sends the row primary key (for example slno) to a dedicated view page which opens in a new tab. This follows ' advice to pass an ID and addresses 's point about making the table columns line up (you must output something for that extra TH). Use a GET form with target="_blank" (or an <a> with target="_blank") so the details open in a new tab, and add rel="noopener noreferrer" to avoid tabnabbing. (developer.mozilla.org)

Example table-cell markup (put this inside your row loop):

<form action="view.php" method="get" target="_blank" style="margin:0;">
  <input type="hidden" name="id" value="<?php echo htmlspecialchars($row['slno'], ENT_QUOTES); ?>">
  <button type="submit">Generate Bill</button>
</form>

In view.php, fetch and validate the incoming id, then use a modern DB API with prepared statements (PDO or mysqli) to retrieve the full row and htmlspecialchars() when you print values to the page. Prepared statements stop SQL injection; escaping output prevents XSS. Example (trimmed):

<?php
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4', 'user', 'pass', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if ($id) {
  $stmt = $pdo->prepare('SELECT * FROM add_details WHERE slno = ?');
  $stmt->execute([$id]);
  $data = $stmt->fetch(PDO::FETCH_ASSOC);
  if ($data) {
    echo htmlspecialchars($data['gname'], ENT_QUOTES);
    // print other fields safely...
  } else {
    echo 'Record not found.';
  }
}
?>

Move away from the old mysql_* functions in your posted main.php — they were deprecated and removed in recent PHP releases; choose PDO or MySQLi instead and use prepared statements as shown. Also ensure your table headers match the columns you output (you had an extra <th>). For secure, detailed guidance see the PHP migration notes, PDO docs and the OWASP SQL injection cheat sheet. (php.net)

Recommended Answers

All 6 Replies

You'll have to create a link to a page that shows the details, by passing it an ID.

how to do that? can u pls give me some example?

First try to create a new script that gets an ID via $_GET to show all details of a single row.

First off, you've created 9 <th>s but have only 8 fields you're pulling from the DB. You're not outputting anything to your Generate Bill column.

My recommendation is rather than a foreach loop for outputting your fields, do it the long way. I.e.,

// Inside your loop...
print "<tr>\n";
print "\t<td>$a_row['slno']</td>\n";
print "\t<td>$a_row['refno']</td>\n";
//etc., etc. Don't forget your Generate Bill column!
print "</tr>\n";

Now it's also easier to follow pritaeas' advice. Decide which column in your table you want to contain the clickable link to go to the View page. You might use the Serial No column, for example.

// replace the "slno" line I put above with:
print "\t<td><a href='/view.php?id=$a_row['slno']'>$a_row['slno']</a></td>\n";

That done, you create a script called view.php and add a line with:

if(isset($_GET['id'])) {
    $id = (num) mysql_real_escape_string($_GET['id']);
    // Add to code to search your database for an entry with this serial number
}

in my generate bill column i need to put a simple button and then if we click that it should show all details of specific row.. that is my idea.. but, is there any way to do that?
1. i added user details (20+ values)
2. then i retrieved the few (6) values from db but. in that generate bill column i want to add the button when we update the page. instead of clicking any specific field i want to put a button in that column. so only... is there any way to do that?

thanks to both of u. now i got what i wanted. ur comments given me some good ideas so i searched on the web and cleared it...
but, i need to know lot of things in php..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.