josephbupe Posted February 13, 2013 Share Posted February 13, 2013 (edited) 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 February 13, 2013 by josephbupe Quote Link to comment Share on other sites More sharing options...
GKWelding Posted February 13, 2013 Share Posted February 13, 2013 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. Quote Link to comment Share on other sites More sharing options...
josephbupe Posted February 13, 2013 Author Share Posted February 13, 2013 I have replaced that line, but I am getting a blank page with the following line in the address bar without the value of the variable being passed: details.php?c_id= Joseph Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 1. On your main page, where is $c_id defined? Post that page's relevant code. 2. <?php echo ".urlencode($c_id)." ?> will echo a string containing .urlencode(whatever cid was). You don't want those quotes or .s. Bad. Quote Link to comment Share on other sites More sharing options...
josephbupe Posted February 13, 2013 Author Share Posted February 13, 2013 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 1. On your main page, where is $c_id defined? Post that page's relevant code. Quote Link to comment Share on other sites More sharing options...
josephbupe Posted February 13, 2013 Author Share Posted February 13, 2013 (edited) 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 February 13, 2013 by josephbupe Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 13, 2013 Share Posted February 13, 2013 (edited) // test value for $c_id $c_id = 12; Did you just ADD that now? Your links should all be linking to # 12. FYI: You should add quotes around your links' HREF attribute. Edited February 13, 2013 by Jessica Quote Link to comment Share on other sites More sharing options...
josephbupe Posted February 14, 2013 Author Share Posted February 14, 2013 (edited) 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 February 14, 2013 by josephbupe Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 Oh my god dude. You select CID from the database. You are using the selected fields to display the image. Are you REALLY confused about how to echo the id when you know how to echo the image filename???? Quote Link to comment Share on other sites More sharing options...
josephbupe Posted February 14, 2013 Author Share Posted February 14, 2013 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 Quote Link to comment Share on other sites More sharing options...
josephbupe Posted February 14, 2013 Author Share Posted February 14, 2013 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> Quote Link to comment Share on other sites More sharing options...
josephbupe Posted February 14, 2013 Author Share Posted February 14, 2013 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> Quote Link to comment Share on other sites More sharing options...
josephbupe Posted February 14, 2013 Author Share Posted February 14, 2013 Hi GKWelding and Jessica, I am so greateful for the assistance you have given me regarding my questions here. The link to the details page is now working fine. Stay well. Joseph Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 14, 2013 Share Posted February 14, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.