Adding a button and displaying information

Team,

Again, forgive me in my ignorance. With help from some of you, I have successfully created a form that inputs into the mysql database, and a page that displays the 50 most recent entries.

What I am needing is a way for a user to click a “View” button and have the data for that particular row displayed on an HTML page. I am aware I need to create the page.

What I am lost on is how to create this button for each row, and how this button will carry over the specific record’s data to the HTML page.

Each entry has a unique “ID” which is AI.

Code for my page is below:

[php]<?php

{ // Secure Connection Script
include(’…/htconfig/dbConfig.php’);
$tbl_name=“members”; // Table name

// Connect to server and select databse.
mysql_connect("$hostname","$username","$password");
mysql_select_db("$database")or die(“cannot select DB”);

// username and password sent from form
$myusername=$_POST[‘myusername’];
$mypassword=$_POST[‘mypassword’];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql=“SELECT * FROM $tbl_name WHERE username=’$myusername’ and password=’$mypassword’”;
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file “login_success.php”
session_register(“myusername”);
session_register(“mypassword”);
?>



    <label><a href="newpo.htm" target="_blank" onClick="window.open('newpo.htm', 'NewTextWindowName','width=510,height=800')"> New PO</a>
  </form>      </td>
  <td width="705">&nbsp;</td>
  <td width="130">&nbsp;</td>
  <td width="130">&nbsp;</td>
  <td width="124"><div align="right"></div></td>
</tr>
<tr>
  <td colspan="5"><p align="right"><?php echo "$myusername"; ?></p>
    <p align="center"><img style="width: 261px; height: 155px;" alt="Rental Xpress Logo" src="../rxtrans.gif"></p>
    <p align="center">Rental Xpress Purchase Order System</p>      </td>
</tr>

 

<?php $table = 'POs';

if (!mysql_select_db($database))
die(“Can’t select database”);

// sending query
$result = mysql_query(“SELECT * FROM {$table} ORDER BY ID DESC LIMIT 50”);
if (!$result) {
die(“Query to show fields from table failed”);
}

$fields_num = mysql_num_fields($result);
$bg_shade = ($i++ % 2 == 0) ? ‘#EfEfEf’ : ‘#dddddd’;
echo “

”;
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo “”;
}
echo “\n”;
// printing table rows
while($row = mysql_fetch_row($result))
{
$bg_shade = ($i++ % 2 == 0) ? '#EfEfEf' : '#99CCFF'; 
 echo "<tr bgcolor=".$bg_shade.">";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
    echo "<td>$cell</td>";

echo "</tr>\n";

}

mysql_free_result($result);
?>

<?php } else { echo "Wrong Username or Password"; } }

?>
[/php]

Thank you again for your help.

{$field->name}

use the row id.

change this line:
[php]echo “

{$field->name}”;[/php]
to this:
[php]echo “<a href=“view_singleton.php?id={$row[‘id’]}” title=“view”>{$field->name}”;[/php]
now when clicked will send you to a page called view_singleton.php (you will need to create this page)
and the id will be passed as a query string.

NEW PAGE:
[php]<?php
if(isset($_GET[‘id’])) {
$id = $_GET[‘id’];
}
else {
exit();
}

// do something here…
// example:

“SELECT * FROM database WHERE id = $id LIMIT 1”

blah blah blah…
?>
[/php]

Hope that helps.
Red :wink:

Edit:
As a sidenote, please stop using mysql_etc and switch to either mysqli or pdo (make this the very next thing you do after fixing this script, may take a couple of days to get used to the switch but I promise it will be worth it as mysql_etc is depreciated and will cease to work at some point in the future rendering every single one of your scripts useless!! Seriously! :o

i prefer mysqli (very similar syntax to mysql)
JimL prefers PDO (i think) and has written a very good article here.

Thank you for your help. I changed the string as instructed, but it makes the headers a link as opposed to each line.

Thoughts?

Sponsor our Newsletter | Privacy Policy | Terms of Service