Jump to content

How to create an item link to a details page in php


josephbupe

Recommended Posts

Hi,

 

Can you pleasehelp me create a working link on an item to open a details page in php. I am trying with the following link on the main page:

 

<td><a href=details.php?c_id=<?php echo ".urlencode($c_id)." ?> ><img src="./images/<?php echo $row['cfilename']; ?>" width="90" height="120" alt="" /></a></td>

 

And the details.php page:

 

<?php
$mysqli = new mysqli("localhost", "joseph", " ", "collectionsdb");

if(!isset($_GET['c_id']))

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

// get value of object id that was sent from address bar

/* Create the prepared statement */
if ($stmt = $mysqli->prepare("SELECT c_id,ctitle,csubject,creference,cyear,cobjecttype,cmaterial,ctechnic,cwidth,cheight,cperiod,cmarkings,cdescription,csource,cartist,cfilename FROM collections WHERE c_id='$c_id'")) {
/* Execute the prepared Statement */
$stmt->execute();

/* Bind results to variables */
$stmt->bind_result($c_id,$ctitle,$csubject,$creference,$cyear,$cobjecttype,$cmaterial,$ctechnic,$cwidth,$cheight,$cperiod,$cmarkings,$cdescription,$csource,$cartist,$cfilename);

$c_id=mysql_real_escape_string($_GET['c_id']);
// get value of object id that sent from address bar


/* fetch values */
while ($rows = $stmt->fetch()) {
 // display records in a table

// image table
?>
<table border="1" align="left">
<tr>
<td rowspan=16 valign=top> <?php echo '<img src="./images/'.$cfilename.'" width="300" height="400" />'; ?> </td>
</tr>

<tr><td>ID</td><td><?php echo $c_id; ?></td></tr>
<tr><td>TITLE</td><td><?php echo $ctitle; ?></td></tr>
<tr><td>SUBJECT</td><td><?php echo $csubject; ?></td></tr>
<tr><td>REFERENCE No.</td><td><?php echo $creference; ?></td></tr>
<tr><td>YEAR</td><td><?php echo $cyear; ?></td></tr>
<tr><td>OBJECT TYPE</td><td><?php echo $cobjecttype; ?></td></tr>
<tr><td>MATERIAL USED</td><td><?php echo $cmaterial; ?></td></tr>
<tr><td>TECHNIC</td><td><?php echo $ctechnic; ?></td></tr>
<tr><td>WIDTH</td><td><?php echo $cwidth; ?></td></tr>
<tr><td>HEIGHT</td><td><?php echo $cheight; ?></td></tr>
<tr><td>PERIOD</td><td><?php echo $cperiod; ?></td></tr>
<tr><td>MARKINGS</td><td><?php echo $cmarkings; ?></td></tr>
<tr><td>DESCRIPTION</td><td width=300><?php echo $cdescription; ?></td></tr>
<tr><td>SOURCE</td><td><?php echo $csource; ?></td></tr>
<tr><td>ARTIST</td><td><?php echo $cartist; ?></td></tr>
</table>

<?php
}

/* Close the statement */
$stmt->close();
}
else {
/* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}

/* close our connection */
$mysqli->close();
?>
<!-- Column 1 end -->
</div>

</div>
</div>
</div>
<div id="footer">

</div>

</body>
</html>

 

Unfortunately, when the link is pressed, I get an error Undefined variable: c_id ... on line 16 of the details.php page.

 

Kindly help me resolve this error.

 

Joseph

Edited by josephbupe
Link to comment
Share on other sites

For a start, what does the line

 

if(!isset($_GET['c_id']))

 

do? Nothing by the looks of it. And your error message is correct. $c_id isn't defined anywhere. I would replace the above line with

 

$c_id = $_GET['c_id'];

 

And that page will work. However, the mix of PHP and display code isn't nice, if I were you I'd do some reading on design patterns.

Link to comment
Share on other sites

Ok,

 

I have now defined it as follows:

 

$c_id = $_GET['c_id'];

 

And the linked as follows:

 

<td><a href=details.php?c_id=<?php echo $c_id; ?> ><img src="./images/<?php echo $row['cfilename']; ?>" width="90" height="120" alt="" /></a></td> 

 

But now I am getting a blank details.php page with the following line in the address bar:

 

details.php?c_id=

 

Joseph

Link to comment
Share on other sites

Here is my main page:

 

<?php

//include our awesome pagination
//class (library)
include 'libs/ps_pagination.php';

//open database
include 'db_connect.php';

function html_escape($raw_input) {
   return htmlspecialchars($raw_input, ENT_QUOTES | ENT_HTML401, 'UTF-8');
} 

// test value for $c_id
$c_id = 12;

//query all data anyway you want
$sql = "SELECT c_id, ctitle, cfilename FROM collections ORDER BY c_id DESC";

$pager = new PS_Pagination( $mysqli, $sql, 16, 6, null );

//our pagination class will render new
//recordset (search results now are limited
//for pagination)

$rs = $pager->paginate();

//get retrieved rows to check if
//there are retrieved data
$num = $rs->num_rows;

if($num >= 1 ) {
   //creating our table header
?>
<table>
<tr>
<?php
   //looping through the records retrieved
   $count = 0;
   while( $row = $rs->fetch_assoc() ){

?>

  <td valign=top>

   <table width=150 border=0 align=left class=allborder>
   <tr><td width=70><?php echo $row['ctitle']; ?></a></td></tr>

   <tr>
       <td><a href=details.php?c_id=<?php echo urlencode($c_id) ?> ><img src="./images/<?php echo $row['cfilename']; ?>" width="90" height="120" alt="" /></a></td>
   </tr>

   </table>
</td>
<?php $count++;
 if ($count %8 == 0 && $count < $num) {
?>
</tr><tr>
<?php }
} 
?>
</tr>
</table>
<?php } ?> 

<div id="footer">

<?php
//page-nav class to control
//the appearance of our page
//number navigation
echo "<div class='page-nav'>";
   //display our page number navigation
   echo $pager->renderFullNav();
echo "</div>";
?>

</div>
</html>

 

Joseph

Edited by josephbupe
Link to comment
Share on other sites

Quotes added to link attributes:

 

<td><a href="details.php?c_id=<?php echo urlencode($c_id) ?>" ><img src="./images/<?php echo $row['cfilename']; ?>" width="90" height="120" alt="" /></a></td> 

 

I am not sure how I should define the variable in the main page. This

$c_id = $_GET['c_id'];

generates

undefined index
error. And still the details.php page returns no data. Here is the updated main page code:

 


<?php

//include our awesome pagination
//class (library)
include 'libs/ps_pagination.php';

//open database
include 'db_connect.php';

function html_escape($raw_input) {
return htmlspecialchars($raw_input, ENT_QUOTES | ENT_HTML401, 'UTF-8');
}

// test value for $c_id
//$c_id = 12;

//query all data anyway you want
$sql = "SELECT c_id, ctitle, cfilename FROM collections ORDER BY c_id DESC";

$pager = new PS_Pagination( $mysqli, $sql, 16, 6, null );

//our pagination class will render new
//recordset (search results now are limited
//for pagination)

$rs = $pager->paginate();

//get retrieved rows to check if
//there are retrieved data
$num = $rs->num_rows;

if($num >= 1 ) {
//creating our table header
?>
<table>
<tr>
<?php
//looping through the records retrieved
$count = 0;
while( $row = $rs->fetch_assoc() ){

?>

<td valign=top>

<table width=150 border=0 align=left class=allborder>
<tr><td width=70><?php echo $row['ctitle']; ?></a></td></tr>

<tr>
 <td><a href=details.php?c_id=<?php echo urlencode($c_id) ?> ><img src="./images/<?php echo $row['cfilename']; ?>" width="90" height="120" alt="" /></a></td>
</tr>

</table>
</td>
<?php $count++;
if ($count %8 == 0 && $count < $num) {
?>
</tr><tr>
<?php }
}
?>
</tr>
</table>
<?php } ?>

<div id="footer">

<?php
//page-nav class to control
//the appearance of our page
//number navigation
echo "<div class='page-nav'>";
//display our page number navigation
echo $pager->renderFullNav();
echo "</div>";
?>

</div>
</html>

 

Joseph

Edited by josephbupe
Link to comment
Share on other sites

Yes, I am totally confused now because even when I did what I new was right I got an error: Undefined index c_id when I used the following to echo an id:

 

<a href="details.php?c_id=<?php echo $row['c_id'] ?>" >

 

I have tried the following definitions, too, in the main page, but getting same error message:

 

if(!isset($_GET['c_id']))
$c_id = $_GET['c_id'];

 

Can you still help?

 

Joseph

Link to comment
Share on other sites

If I use a prepared statement to select records,I would echo only the variables. But with the prepared statement, all I see on the page is an error:

 

Parse error: syntax error, unexpected $end in C:\Program Files\Abyss Web Server\htdocs\Lusaka-MUSEUM - MySQLi\Lusaka-MUSEUM - pagination\gallery.php on line 163

 

Line 163 is the an html close tag, which is

</html>

 

<?php
function html_escape($raw_input) {
   return htmlspecialchars($raw_input, ENT_QUOTES | ENT_HTML401, 'UTF-8');
}  

$mysqli = new mysqli("localhost", "joseph", "encrypted", "collectionsdb");

// get value of object id that sent from address bar
//if(!isset($_GET['c_id']))
$c_id = $_GET['c_id'];

/* check connection */
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

// get value of object id that was sent from address bar

   /* Create the prepared statement */
   if ($stmt = $mysqli->prepare("SELECT c_id,ctitle,cfilename FROM collections WHERE c_id='$c_id'")) {    
   /* Execute the prepared Statement */
   $stmt->execute();

   /* Bind results to variables */
   $stmt->bind_result($c_id,$ctitle,$cfilename);

   /* fetch values */
   while ($rows = $stmt->fetch()) {

$pager = new PS_Pagination( $mysqli, $sql, 16, 6, null );

//our pagination class will render new
//recordset (search results now are limited
//for pagination)

$rs = $pager->paginate();

//get retrieved rows to check if
//there are retrieved data
$num = $rs->num_rows;

if($num >= 1 ) {
   //creating our table header
?>
<table>
<tr>
<?php
   //looping through the records retrieved
   $count = 0;
   while( $row = $rs->fetch_assoc() ){

?>

  <td valign=top>

   <table width=150 border=0 align=left class=allborder>
   <tr><td width=70 bgcolor=#eee><?php echo $ctitle; ?></a></td></tr>

   <tr>
    <td><a href="details.php?c_id=<?php echo $c_id ?>" ><img src="./images/<?php echo $cfilename; ?>" width="90" height="120" alt="" /></a></td>
   </tr>

   </table>
</td>
<?php $count++;
 if ($count %8 == 0 && $count < $num) {
?>
</tr><tr>
<?php }
}  
?>
</tr>
</table>
<?php } ?>  

<div id="footer">

<?php
//page-nav class to control
//the appearance of our page
//number navigation
echo "<div class='page-nav'>";
   //display our page number navigation
   echo $pager->renderFullNav();
echo "</div>";
?>

</div>
</html>

Link to comment
Share on other sites

Here is my updated main page code with a prepared statement but does not display content from database, except an error:

 

Notice: Undefined index: c_id in C:\Program Files\Abyss Web Server\htdocs\Lusaka-MUSEUM - MySQLi\Lusaka-MUSEUM - pagination\gallery.php on line 71 

 

Main page:

 

 

<?php
function html_escape($raw_input) {
   return htmlspecialchars($raw_input, ENT_QUOTES | ENT_HTML401, 'UTF-8');
}  

$mysqli = new mysqli("localhost", "joseph", "encrypted", "collectionsdb");

// get value of object id that sent from address bar
//if(!isset($_GET['c_id']))
$c_id = $_GET['c_id'];

/* check connection */
if (mysqli_connect_errno()) {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

// get value of object id that was sent from address bar

   /* Create the prepared statement */
   if ($stmt = $mysqli->prepare("SELECT c_id,ctitle,cfilename FROM collections WHERE c_id='$c_id'")) {    
   /* Execute the prepared Statement */
   $stmt->execute();

   /* Bind results to variables */
   $stmt->bind_result($c_id,$ctitle,$cfilename);

   /* fetch values */
   while ($rows = $stmt->fetch()) {

$pager = new PS_Pagination( $mysqli, $sql, 16, 6, null );

//our pagination class will render new
//recordset (search results now are limited
//for pagination)

$rs = $pager->paginate();

//get retrieved rows to check if
//there are retrieved data
$num = $rs->num_rows;

if($num >= 1 ) {
   //creating our table header
?>
<table>
<tr>
<?php
   //looping through the records retrieved
   $count = 0;
   while( $row = $rs->fetch_assoc() ){

?>

  <td valign=top>

   <table width=150 border=0 align=left class=allborder>
   <tr><td width=70 bgcolor=#eee><?php echo $ctitle; ?></a></td></tr>

   <tr>
    <td><a href="details.php?c_id=<?php echo $c_id ?>" ><img src="./images/<?php echo $cfilename; ?>" width="90" height="120" alt="" /></a></td>
   </tr>

   </table>
</td>
<?php $count++;
 if ($count %8 == 0 && $count < $num) {
?>
</tr><tr>
<?php }
}  
?>
</tr>
</table>
<?php }  ?>  

<div id="footer">

<?php
//page-nav class to control
//the appearance of our page
//number navigation
echo "<div class='page-nav'>";
   //display our page number navigation
   echo $pager->renderFullNav();
echo "</div>";
}
/* Close the statement */
   $stmt->close();
}
else {
   /* Error */
   printf("Prepared Statement Error: %s\n", $mysqli->error);
}

/* close our connection */
$mysqli->close();
?>

</div>
</html>

Link to comment
Share on other sites

Every time you post the code it's the SAME CODE.

 

You added $c_id = $_GET['c_id'] to the main page, but does THAT page have a GET variable?

 

You need to learn how the query string / $_GET superglobal works, and I think some basic knowledge on variables and logic. 

Link to comment
Share on other sites

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.