Jump to content

[SOLVED] Displaying results of query


mjgdunne

Recommended Posts

Hi all, i have some php code which displays all the contents of a database table, after each reault i need to be able to add a button so the user can view the full details, is there any way of doing this.

Here is my code for displaying the results:

<html>
<head>
<title>Car Rentals & Returns</title>
<meta http-equiv="Content-Type" content="text/html" />
<link href="style2.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper3">
<img src="images/cars.jpg" width="996" height="100"></a>

<TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%>
<tr>
<td align="center">
<H1>View Cases:</H1>
<form method="post" action="login_success2.php">
</td>
</table>

<TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%>
<tr>
<TD ALIGN=CENTER>



<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
$tbl_name="reff"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "root", "root")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$myq=$_POST['myq'];


$result = mysql_query("SELECT * FROM reff ORDER BY make;")
or die(mysql_error());

while($row = mysql_fetch_array( $result )) {


echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>Make</th>";
	echo "<td>";
        echo $row['make'];
echo "</td>";


echo "<tr><th>Model</th>";
	echo "<td>";
        echo $row['model'];
echo "</td>";

echo "<tr><th>Registration</th>";
	echo "<td>";
    echo $row['registration'];
echo "</td>";

echo "<tr><th>Image</th>";
	echo "<td>";
    	echo $row['imgdata'];
echo "</td>";

echo "<tr>";
echo "<br>";
echo "<br>";
echo "</tr>";

}



if (mysql_num_rows($result) == 0) {
    echo "No records found";
    exit;
}
?>


<TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=30%>
<tr>
<td align="center"><input type="submit" value="Home"/></td>
</tr>
</table>
</table>
</td>
</form>
</tr>
</table>
</BODY>
</HTML>

Any help would be great thanks.  ???

Link to comment
https://forums.phpfreaks.com/topic/102913-solved-displaying-results-of-query/
Share on other sites

First you  should change how your tables is outputted. Your tags are a little messed up. But to answer yourr question try this

 

echo "<tr><th>Image</th>";
	echo "<td>";
    	echo $row['imgdata'];
echo "</td>";
echo "<td><button onclick=\"window.location.href='detail.php?id=".$row['id']."'\">Details</button></td>[/code

Ray

Hi thanks for your reply, i changed the code as bellow:

<html>
<head>
<title>Car Rentals & Returns</title>
<meta http-equiv="Content-Type" content="text/html" />
<link href="style2.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper3">
<img src="images/cars.jpg" width="996" height="100"></a>

<TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%>
<tr>
<td align="center">
<H1>View Cases:</H1>
<form method="post" action="view_2.php">
</td>
</table>
<TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=100%>
<tr>
<TD ALIGN=CENTER>



<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
$tbl_name="reff"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "root", "root")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


$result = mysql_query("SELECT * FROM reff ORDER BY make;");

$_SESSION['reff'] = array();
while ($row = mysql_fetch_assoc($result)) {
$_SESSION['reff'][] = $row;
}

foreach ($_SESSION['reff'] as $row) {



echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>ID</th>";
	echo "<td>";
        echo $row['pid'];
echo "</td>";


echo "<tr><th>Make</th>";
	echo "<td>";
        echo $row['make'];
echo "</td>";


echo "<tr><th>Model</th>";
	echo "<td>";
        echo $row['model'];
echo "</td>";

echo "<tr><th>Registration</th>";
	echo "<td>";
    echo $row['registration'];
echo "</td>";

echo "<tr><th>Image</th>";
	echo "<td>";
    	echo $row['imgdata'];
echo "</td>";

echo "<tr><th></th>";
	echo "<td>";
    	echo "<button onclick=\"window.location.href='view_2.php?id=".$row['pid']."'\">Details</button>";
echo "</td>";


echo "<tr>";
echo "<br>";
echo "<br>";
echo "</tr>";

}



if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}


if (mysql_num_rows($result) == 0) {
    echo "No records found";
    exit;
}
?>


<TABLE BGCOLOR="#F0F8FF" BORDER=0 CELLPADDING=10 WIDTH=30%>
</form>
<tr><form method="post" action="login_success2.php">
<td align="center"><input type="submit" value="Home"/></td>
</tr>
</table>
</table>
</td>
</tr>
</table>
</BODY>
</HTML>

 

My question is, how do i get the details in my next php script, i was using sessions to store results in an array and retrieving them, although i see you are passing through the id.

What would i need to do to display that particular result. Thanks.

On the page "view2.php" you would grab the id of that row

$id = $_GET['id'];

 

They query the database to show the results

$sql = "SELECT * FROM reff WHERE `pid` = '$id'";
$result = mysql_query($sql) or die(mysql-error());
$row = mysql_fetch_assoc($result);
// show the details below

 

Ray

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.