Jump to content

josephbupe

Members
  • Posts

    56
  • Joined

  • Last visited

Everything posted by josephbupe

  1. Hi, Ok, this is what I have now but the search cant return records based on any criteria I use: $criteria = array('ctitle', 'csubject', 'creference', 'cat_id', 'cmaterial', 'ctechnic', 'cartist', 'csource', 'stolen'); $likes = ""; $url_criteria = ''; foreach ( $criteria AS $criterion ) { if ( ! empty($_POST[$criterion]) ) { $value = ($_POST[$criterion]); $likes .= " AND `$criterion` = '%$value%'"; $url_criteria .= '&'.$criterion.'='.htmlentities($_POST[$criterion]); } elseif ( ! empty($_GET[$criterion]) ) { $value = mysql_real_escape_string($_GET[$criterion]); $likes .= " AND `$criterion` = '%$value%'"; $url_criteria .= '&'.$criterion.'='.htmlentities($_GET[$criterion]); } } $sql = "SELECT * FROM collections WHERE c_id>0" . $likes . " ORDER BY c_id ASC"; echo $sql; When I echoed the query I got this: SELECT * FROM collections WHERE c_id>0 AND `cmaterial` = '%wood%' ORDER BY c_id ASC If this works I want to get the variable into the URL using the foreach loop above. I just need a third eye, please. Joseph
  2. Hi, This will be my first time to use a criteria-based search with pagination togather. I want to paginat my search query results and be able to go to next and previous pages of my search results based on the search criteria. For now the pagination returns all records from the table. I have tried to include one search term in the link for NEXT and PREVIOUS buttons but still when I click the button I next page load all the records from the MySQL table. The search term (e.g. gallery.php?cmaterial=$cmaterial) appears in the URL only when I click NEXT and not on the current page. Even then the search results do not match the search term. Here is my code: <?php $s=$_GET['s']; // rows to return $limit=12; // check for a search parameter //if (!isset($var)) // { // echo "<p>We dont seem to have a search parameter!</p>"; // exit; // } $ctitle = mysql_real_escape_string($_POST['ctitle']); $csubject = mysql_real_escape_string($_POST['csubject']); $creference = mysql_real_escape_string($_POST['creference']); $cat_id = ($_POST['cat_id']); $cmaterial = mysql_real_escape_string($_POST['cmaterial']); $ctechnic = mysql_real_escape_string($_POST['ctechnic']); $cartist = mysql_real_escape_string($_POST['cartist']); $csource = mysql_real_escape_string($_POST['csource']); $stolen = mysql_real_escape_string($_POST['stolen']); $sql = "SELECT * FROM collections WHERE c_id>0 AND `ctitle` LIKE '%".$ctitle."%' AND `csubject` LIKE '%".$csubject."%' AND `creference` LIKE '%".$creference."%' AND `cat_id` LIKE '%".$cat_id."%' AND `cmaterial` LIKE '%".$cmaterial."%' AND `ctechnic` LIKE '%".$ctechnic."%' AND `cartist` LIKE '%".$cartist."%' AND `csource` LIKE '%".$csource."%' ORDER BY c_id ASC"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); $numresults=mysql_query($sql); $numrows=mysql_num_rows($numresults); // next determine if s has been passed to script, if not use 0 if (empty($s)) { $s=0; } // get results $sql .= " limit $s,$limit"; $sql_result = mysql_query($sql) or die("Couldn't execute query"); $count = 1 + $s ; while ($row = mysql_fetch_assoc($sql_result)) { $c_id=$row['c_id']; ?> ..............................................HTML RESULT TABLE ................................................... <?php $currPage = (($s/$limit) + 1); //break before paging echo "<br />"; // next we need to do the links to other results if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print " <a href=\"$PHP_SELF?s=$prevs&cmaterial=$cmaterial\"><< Prev 10</a>&nbsp "; } // calculate number of pages needing links $pages=intval($numrows/$limit); // $pages now contains int of pages needed unless there is a remainder from division if ($numrows%$limit) { // has remainder so add one page $pages++; } echo "</table>"; // check to see if last page if (!((($s+$limit)/$limit)==$pages) && $pages!=1) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&cmaterial=$cmaterial\">Next 10 >></a>"; } $a = $s + ($limit) ; if ($a > $numrows) { $a = $numrows ; } $b = $s + 1 ; echo "<p>Showing results $b to $a of $numrows</p>"; ?> What should I do next? Thank you in advance. Joseph
  3. Hi, Thanx for your early response and advice. Please, bear with me as I am still not well vested with PHP MySQL programming. My search now works except for a few issues. 1. By default, I want the query to return all records WHERE c_id > 0 when the page is accessed. c_id is the primary key. Currently, the page does not display any records when I access it. This was not the case before until I added this criteria for category saved as numeric value for a foreign key: AND `cat_id` = ' ".$cat_id." ' 2. Also, I want to be able to paginate search results. I was reading some posts and there was a mention of attaching a search term to the link. I do not know how that should be with multiple criteria like in my case. My current pagination return all records when I click next button after I have done a search based on one of the criterias. Please, here is the whole code I have: Search page: <?php $ctitle = mysql_real_escape_string($_POST['ctitle']); $csubject = mysql_real_escape_string($_POST['csubject']); $creference = mysql_real_escape_string($_POST['creference']); $cat_id = ($_POST['cat_id']); $cmaterial = mysql_real_escape_string($_POST['cmaterial']); $ctechnic = mysql_real_escape_string($_POST['ctechnic']); $cartist = mysql_real_escape_string($_POST['cartist']); $csource = mysql_real_escape_string($_POST['csource']); $sql = "SELECT * FROM collections WHERE c_id>0 AND `ctitle` LIKE '%".$ctitle."%' AND `csubject` LIKE '%".$csubject."%' AND `creference` LIKE '%".$creference."%' AND `cat_id` LIKE '%".$cat_id."%' AND `cmaterial` LIKE '%".$cmaterial."%' AND `ctechnic` LIKE '%".$ctechnic."%' AND `cartist` LIKE '%".$cartist."%' AND `csource` LIKE '%".$csource."%' ORDER BY c_id ASC"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); // pagination code $PAGING=new PAGING($sql); // There are two optional parameters as well: // $records = 16 //Number of records to be displayed per page // $pages = Number of pages to be displayed in the paging $sql=$PAGING->sql; if (mysql_num_rows($sql_result)>0) { // The following line gives us an SQL statement with appropriate limits applied $sql_result=mysql_query($sql) or die($sql." - ".mysql_error()); while ($row = mysql_fetch_assoc($sql_result)) { $c_id=$row['c_id']; ?> search results ..................... <?=$PAGING->show_paging("gallery.php")?> And the paging class: <?php /************************************************ * ======================================== * * Perfect MySQL Paging * * ======================================== * * Script Name: class.paging.php * * Developed By: Khurram Adeeb Noorani * * Email: [email protected] * * My CV: http://www.visualcv.com/kanoorani * * Twitter: http://www.twitter.com/kanoorani * * Date Created: 08-JULY-2009 * * Last Modified: 08-JULY-2009 * ************************************************/ ?> <?php class PAGING { var $sql,$records,$pages; /* Variables that are passed via constructor parameters */ var $page_no,$total,$limit,$first,$previous,$next,$last,$start,$end; /* Variables that will be computed inside constructor */ function PAGING($sql,$records=24,$pages=4) { if($pages%2==0) $pages++; /* The pages should be odd not even */ $res=mysql_query($sql) or die($sql." - ".mysql_error()); $total=mysql_num_rows($res); $page_no=isset($_GET["page_no"])?$_GET["page_no"]:1; /* Checking the current page If there is no current page then the default is 1 */ $limit=($page_no-1)*$records; $sql.=" limit $limit,$records"; /* The starting limit of the query */ $first=1; $previous=$page_no>1?$page_no-1:1; $next=$page_no+1; $last=ceil($total/$records); if($next>$last) $next=$last; /* The first, previous, next and last page numbers have been calculated */ $start=$page_no; $end=$start+$pages-1; if($end>$last) $end=$last; /* The starting and ending page numbers for the paging */ if(($end-$start+1)<$pages) { $start-=$pages-($end-$start+1); if($start<1) $start=1; } if(($end-$start+1)==$pages) { $start=$page_no-floor($pages/2); $end=$page_no+floor($pages/2); while($start<$first) { $start++; $end++; } while($end>$last) { $start--; $end--; } } /* The above two IF statements are kinda optional These IF statements bring the current page in center */ $this->sql=$sql; $this->records=$records; $this->pages=$pages; $this->page_no=$page_no; $this->total=$total; $this->limit=$limit; $this->first=$first; $this->previous=$previous; $this->next=$next; $this->last=$last; $this->start=$start; $this->end=$end; } function show_paging($url,$params="") { $paging=""; if($this->total>$this->records) { $page_no=$this->page_no; $first=$this->first; $previous=$this->previous; $next=$this->next; $last=$this->last; $start=$this->start; $end=$this->end; if($params=="") $params="?page_no="; else $params="?$params&page_no="; $paging.="<ul class='paging'>"; $paging.="<li class='paging-current'>Page $page_no of $last</li>"; if($page_no==$first) $paging.="<li class='paging-disabled'><a href='javascript:void(0)'><<</a></li>"; else $paging.="<li><a href='$url$params$first'><<</a></li>"; if($page_no==$previous) $paging.="<li class='paging-disabled'><a href='javascript:void(0)'><</a></li>"; else $paging.="<li><a href='$url$params$previous'><</a></li>"; for($p=$start;$p<=$end;$p++) { $paging.="<li"; if($page_no==$p) $paging.=" class='paging-active'"; $paging.="><a href='$url$params$p'>$p</a></li>"; } if($page_no==$next) $paging.="<li class='paging-disabled'><a href='javascript:void(0)'>></a></li>"; else $paging.="<li><a href='$url$params$next'>></a></li>"; if($page_no==$last) $paging.="<li class='paging-disabled'><a href='javascript:void(0)'>>></a></li>"; else $paging.="<li><a href='$url$params$last'>>></a></li>"; $paging.="</ul>"; } return $paging; } } ?> Your help is highly appreciated in advance. Joseph
  4. Hi, I want the query initially to return all records where c_id>0 and then filter based on the subsequent criteria supplied through text boxes. However, I am not getting any records printed the first time the page is accessed. When I echo the query I get the following printed instead: SELECT * FROM collections WHERE ( c_id>0 AND `ctitle` = '' AND `csubject` = '' AND `creference` = '' AND `cobjecttype` = '' AND `cmaterial` = '' AND `ctechnic` = '' AND `csource` = '' AND `cartist` = '' )ORDER BY c_id DESCrequest "Could not execute SQL query" SELECT * FROM collections WHERE ( c_id>0 AND `ctitle` = '' AND `csubject` = '' AND `creference` = '' AND `cobjecttype` = '' AND `cmaterial` = '' AND `ctechnic` = '' AND `csource` = '' AND `cartist` = '' )ORDER BY c_id DESC And the code is as follows: $ctitle = mysql_real_escape_string($_POST['ctitle']); $csubject = mysql_real_escape_string($_POST['csubject']); $creference = mysql_real_escape_string($_POST['creference']); $cobjecttype = mysql_real_escape_string($_POST['cobjecttype']); $cmaterial = mysql_real_escape_string($_POST['cmaterial']); $ctechnic = mysql_real_escape_string($_POST['ctechnic']); $cartist = mysql_real_escape_string($_POST['cartist']); $csource = mysql_real_escape_string($_POST['csource']); $sql = "SELECT * FROM collections WHERE ( c_id>0 AND `ctitle` = '{$ctitle}' AND `csubject` = '{$csubject}' AND `creference` = '{$creference}' AND `cobjecttype` = '{$cobjecttype}' AND `cmaterial` = '{$cmaterial}' AND `ctechnic` = '{$ctechnic}' AND `csource` = '{$csource}' AND `cartist` = '{$cartist}' )ORDER BY c_id DESC"; Where have I gone wrong here? Joseph
  5. Hi, I meant for categories in another table, unlike in your case where the list is hard coded.
  6. Hi, How can I use the select option box to display an option value saved to a table and use the same select box to also select any other value If I want to change the saved value?
  7. Resolved. Thanx
  8. Hi, I am attempting to insert a foreign key into a table along with other information, but I am getting an error message: Could not insert data into DataBase: Cannot add or update a child row: a foreign key constraint fails (`collectionsdb2`.`collections`, CONSTRAINT `collections_ibfk_1` FOREIGN KEY (`cat_id`) REFERENCES `categories` (`cat_id`)) The insert statement is as follows: $sql = "INSERT INTO collections (cat_id, ctitle, csubject, creference, cmaterial, ctechnic, cwidth, cheight, cperiod, cmarkings, cdescription, csource, cartist, cfilename) VALUES ($cat_id, '$ctitle', '$csubject', '$creference', '$cmaterial', '$ctechnic', '$cwidth', '$cheight', '$cperiod', '$cmarkings', '$cdescription', '$csource', '$cartist', '" . $image['name'] . "')"; $result = mysql_query($sql) or die ("Could not insert data into DataBase: " . mysql_error()); exit; And my two tables are as folows: /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; DROP TABLE IF EXISTS collectionsdb2.collections; CREATE TABLE `collections` ( `c_id` int(11) NOT NULL AUTO_INCREMENT, `cat_id` int(4) DEFAULT NULL, `ctitle` varchar(65) NOT NULL, `csubject` varchar(65) DEFAULT NULL, `creference` varchar(65) DEFAULT NULL, `cyear` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `cmaterial` varchar(65) DEFAULT NULL, `ctechnic` varchar(255) DEFAULT NULL, `cwidth` varchar(65) DEFAULT NULL, `cheight` varchar(65) DEFAULT NULL, `clength` varchar(65) DEFAULT NULL, `cdiameter` varchar(65) DEFAULT NULL, `cperiod` varchar(65) DEFAULT NULL, `cmarkings` varchar(255) DEFAULT NULL, `cdescription` text, `csource` varchar(65) DEFAULT NULL, `cartist` varchar(65) DEFAULT NULL, `cfilename` varchar(65) DEFAULT NULL, PRIMARY KEY (`c_id`), KEY `cat_id` (`cat_id`), CONSTRAINT `collections_ibfk_1` FOREIGN KEY (`cat_id`) REFERENCES `categories` (`cat_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; and /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; DROP TABLE IF EXISTS collectionsdb2.categories; CREATE TABLE `categories` ( `cat_id` int(4) NOT NULL AUTO_INCREMENT, `category` varchar(25) NOT NULL, PRIMARY KEY (`cat_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; I am using a select option box to insert foreign key (category) into the table form the categories table. Please, how should I resolve this issue? Thanx. joseph
  9. Hi, I found a script written by someone else which works fine for running searches on multiple columns. I have just included in it a category field, which is a foreign key from the categories table, and I want to run queries with this one also, but currently am not getting results when I run queries with the category (foreign key). the code: <?php if ($_REQUEST["string"]<>'') { $search_string = " AND (ctitle LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR csubject LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')"; } if ($_REQUEST["ref"]<>'') { $search_string = " AND (creference LIKE '%".mysql_real_escape_string($_REQUEST["ref"])."%')"; } if ($_REQUEST["category"]<>'') { $search_category = " AND category='".mysql_real_escape_string($_REQUEST["category"])."'"; } if ($_REQUEST["cmaterial"]<>'') { $search_cmaterial = " AND (cmaterial LIKE '%".mysql_real_escape_string($_REQUEST["cmaterial"])."%')"; } if ($_REQUEST["ctechnic"]<>'') { $search_ctechnic = " AND (ctechnic LIKE '%".mysql_real_escape_string($_REQUEST["ctechnic"])."%')"; } if ($_REQUEST["cartist"]<>'') { $search_cartist = " AND (cartist LIKE '%".mysql_real_escape_string($_REQUEST["cartist"])."%')"; } if ($_REQUEST["cperiod"]<>'') { $search_string = " AND (cperiod LIKE '%".mysql_real_escape_string($_REQUEST["cperiod"])."%')"; } if ($_REQUEST["csource"]<>'') { $search_string = " AND (csource LIKE '%".mysql_real_escape_string($_REQUEST["csource"])."%')"; } if ($_REQUEST["cyear"]<>'') { $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE cyear = '".mysql_real_escape_string($_REQUEST["cyear"])."'".$search_string.$search_category; } else { $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE c_id>0".$search_string.$search_category.$search_cmaterial.$search_ctechnic.$search_cartist; } $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); if (mysql_num_rows($sql_result)>0) { // The following line gives us an SQL statement with appropriate limits applied $sql_result=mysql_query($sql) or die($sql." - ".mysql_error()); while ($row = mysql_fetch_assoc($sql_result)) { // and the result table goes below ... ?> This is the select drop down list for categories: <select name="category"> <option value="">--Object Category--</option> <?php $sql = "SELECT cat_id,category FROM categories GROUP BY category ORDER BY category"; $sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql); while ($row = mysql_fetch_assoc($sql_result)) { echo "<option value='".$row["category"]."'".($row["category"]==$_REQUEST["category"] ? " selected" : "").">".$row["category"]."</option>"; } ?> </select> Is it because I am escaping it (foreign key) like a string or something else? joseph
  10. Hi, I also want to ask along the same line. How can I automatically insert ONLY the current year (2013) into mysql database? joseph
  11. Hi, Can some one point me to any existing login script that I can learn from? joseph
  12. Hi, I found a script at http://www.bertramakers.com/moolabs/imagezoom.php that opens an original seized image which I would like to adopt for my site. The problem is that I don't know how I can tweak it to enlarge images which are being fetched from a directory with names saved to database, rather than imbeding their names within the html image tags. Can any one help? Joseph
  13. 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
  14. 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>
  15. 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: Line 163 is the an html close tag, which is <?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>
  16. 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
  17. 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 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. Hi, I found a script http://www.weberdev....?ExampleID=4406 by someone called "Suraj Thapaliya" for doing a multiple search using PHP and MySQL. I am trying to adopt it to my own use for one application am putting up together with the help of different scripts. In my application, if I populate only one of the text boxes or combo boxes the search is executed fine. But the problem is that when I use two or more criterias I am getting an error like the one below: ; in which case I used the cmaterial combo box and ctitle text box as the criteria. The following are combo boxes: cmaterial, cobjecttype and ctechnic, while the rest are text boxes. My code is as follows: <? include("includes/conn.php"); $ctitle=$_POST["ctitle"]; $csubject=$_POST["csubject"]; $creference=$_POST["creference"]; $cyear=$_POST["cyear"]; $cobjecttype=$_POST["cobjecttype"]; $cmaterial=$_POST["cmaterial"]; $ctechnic=$_POST["ctechnic"]; $cwidth=$_POST["cwidth"]; $cheight=$_POST["cheight"]; $cperiod=$_POST["cperiod"]; $cmarkings=$_POST["cmarkings"]; $o=$_POST["o"]; $rest=""; $text="Search Keywords : "; if($ctitle!="") { if($rest=="") { $rest.="where ctitle Like '$ctitle%' "; $text.="Item Title = $ctitle"; } else { $rest.=" $o ctitle = '$ctitle' "; $text.=", Item Title = $ctitle"; } } if($csubject!="") { if($rest=="") { $rest.="where csubject = '$csubject' "; $text.="Subject = $csubject "; } else { $rest.=" $o csubject = '$csubject' "; $text.=", Subject = $csubject "; } } if($creference!="") { if($rest=="") { $rest.="where creference = '$creference' "; $text.="Reference No. = $creference"; } else { $rest.=" $o creference = '$creference' "; $text.=", Reference No. = $creference"; } } if($cyear!="") { if($rest=="") { $rest.="where cyear Like '$cyear%' "; $text.="Year = $cyear"; } else { $rest.=" $o cyear = '$cyear' "; $text.=", Year = $cyear"; } } if($cobjecttype!="") { if($rest=="") { $rest.="where cobjecttype Like '$cobjecttype%' "; $text.="Object = $cobjecttype"; } else { $rest.=" $o cobjecttype = '$cobjecttype' "; $text.=", Object = $cobjecttype"; } } if($cmaterial!="") { if($rest=="") { $rest.="where cmaterial Like '$cmaterial%' "; $text.="Material = $cmaterial"; } else { $rest.=" $o cmaterial = '$cmaterial' "; $text.=", Material = $cmaterial"; } } if($ctechnic!="") { if($rest=="") { $rest.="where ctechnic Like '$ctechnic%' "; $text.="Technic = $ctechnic"; } else { $rest.=" $o ctechnic = '$ctechnic' "; $text.=", Technic = $ctechnic"; } } if($cwidth!="") { if($rest=="") { $rest.="where cwidth Like '$cwidth%' "; $text.="Width = $cwidth"; } else { $rest.=" $o cwidth = '$cwidth' "; $text.=", Width = $cwidth"; } } if($cheight!="") { if($rest=="") { $rest.="where cheight Like '$cheight%' "; $text.="Height = $cheight"; } else { $rest.=" $o cheight = '$cheight' "; $text.=", Height = $cheight"; } } if($cperiod!="") { if($rest=="") { $rest.="where cperiod Like '$cperiod%' "; $text.="Period = $cperiod"; } else { $rest.=" $o cperiod = '$cperiod' "; $text.=", Period = $cperiod"; } } if($cmarkings!="") { if($rest=="") { $rest.="where cmarkings Like '$cmarkings%' "; $text.="Markings = $cmarkings"; } else { $rest.=" $o cmarkings = '$cmarkings' "; $text.=", Markings = $cmarkings"; } } if($rest!="") { $rest=$rest; } else { //die("Enter Search Parameter<br><br><br><br><br><br><br><br><br><br><br>"); } //Number of items to display per page $perpage = 16; if(isset($_GET["page"])) { $page = intval($_GET["page"]); } else { $page = 1; } $calc = $perpage * $page; $start = $calc - $perpage; ////////////////// $sql="select * from $tbl_name $rest Limit $start, $perpage"; $result=mysql_query($sql) or die(mysql_error()); $num=mysql_num_rows($result); ?> Please, what did I do wrong? joseph
  23. Please,does anyone know where I am going wrong with this code to upload three images to a same record??
  24. Thank you muddy_funster for your early response. Here is my upload.php code highlighting additions i made to uploading a second image (image2) and third image (image3) for the same record: <?php // Start a session for error reporting session_start(); // Call our connection file require("includes/conn.php"); // Check to see if the type of file uploaded is a valid image type function is_valid_type($file) { // This is an array that holds all the valid image MIME types $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif"); $valid_types = array("image2/jpg", "image2/jpeg", "image2/bmp", "image2/gif"); $valid_types = array("image3/jpg", "image3/jpeg", "image3/bmp", "image3/gif"); if (in_array($file['type'], $valid_types)) return 1; return 0; } // Just a short function that prints out the contents of an array in a manner that's easy to read // I used this function during debugging but it serves no purpose at run time for this example function showContents($array) { echo "<pre>"; print_r($array); echo "</pre>"; } // Set some constants // This variable is the path to the image folder where all the images are going to be stored // Note that there is a trailing forward slash $TARGET_PATH = "images/"; // Get our POSTed variables $ctitle = $_POST['ctitle']; $csubject = $_POST['csubject']; $creference = $_POST['creference']; $cyear = $_POST['cyear']; $cwidth = $_POST['cwidth']; $cheight = $_POST['cheight']; $cperiod = $_POST['cperiod']; $cdescription = $_POST['cdescription']; $image = $_FILES['image']; $image2 = $_FILES['image2']; $image3 = $_FILES['image3']; // Sanitize our inputs $ctitle = mysql_real_escape_string($ctitle); $csubject= mysql_real_escape_string($csubject); $creference = mysql_real_escape_string($creference); $cyear = mysql_real_escape_string($cyear); $cwidth = mysql_real_escape_string($cwidth); $cheight = mysql_real_escape_string($cheight); $cperiod = mysql_real_escape_string($cperiod); $cdescription = mysql_real_escape_string($cdescription); $image['name'] = mysql_real_escape_string($image['name']); $image2['name2'] = mysql_real_escape_string($image2['name2']); $image3['name3'] = mysql_real_escape_string($image3['name3']); // Build our target path full string. This is where the file will be moved do // i.e. images/picture.jpg $TARGET_PATH .= $image['name']; $TARGET_PATH .= $image2['name2']; $TARGET_PATH .= $image3['name3']; // Make sure all the fields from the form have inputs if ( $ctitle == "" || $csubject == "" || $creference == "" || $cyear == "" || $cwidth == "" || $cheight == "" || $cperiod == "" || $cdescription == "" || $image['name'] == "" || $image2['name2'] == "" || $image3['name3'] == "" ) { $_SESSION['error'] = "All fields are required"; header("Location: index.php"); exit; } // Check to make sure that our file is actually an image // You check the file type instead of the extension because the extension can easily be faked if (!is_valid_type($image)) { $_SESSION['error'] = "You must upload a jpeg, gif, or bmp"; header("Location: insert.php"); exit; } // Here we check to see if a file with that name already exists // You could get past filename problems by appending a timestamp to the filename and then continuing if (file_exists($TARGET_PATH)) { $_SESSION['error'] = "A file with that name already exists"; header("Location: insert.php"); exit; } // Lets attempt to move the file from its temporary directory to its new home if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) { // NOTE: This is where a lot of people make mistakes. // We are *not* putting the image into the database; we are putting a reference to the file's location on the server $sql = "insert into collections (ctitle, csubject, creference, cyear, cwidth, cheight, cperiod, cdescription, cfilename, cfilename2, cfilename3) values ('$ctitle', '$csubject', '$creference', '$cyear', '$cwidth', '$cheight', '$cperiod', '$cdescription', '" . $image['name'] . "', '" . $image2['name2'] . "', '" . $image3['name3'] . "')"; $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); header("Location: index.php"); exit; } else { // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to // Make sure you chmod the directory to be writeable $_SESSION['error'] = "Could not upload file. Check read/write persmissions on the directory"; header("Location: index.php"); exit; } ?> And also part of my insert.php file: <tr> <td>Upload Image1</td><td></td> <td> <input type="file" name="image" /><br /> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> </tr> <tr> </td> <td>Upload Image2</td><td></td> <td> <input type="file" name="[color=#0000ff]image2[/color]" /><br /> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> </tr> <tr> </td> <td>Upload Image3</td><td></td> <td> <input type="file" name="[color=#0000ff]image3[/color]" /><br /> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <input type="submit" id="submit" value="Save" /> </td> </tr> I will appreciate your help. joseph
×
×
  • 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.