0

当谈到 php 和 mysql 时,我有点像周末战士。我大部分时间使用 Dreamweaver 8 来构建我的代码,但必要时在 TextWrangler 中进行一些手动编码。

我有一个有 28 列的表,目前有 1224 行(并且正在增长)。我使用 DW8 构建高级搜索页面的代码。它工作得很好,除了如果一行在一个或多个列中有 NULL (其中很多列),它将不会检索该记录。这是个问题。我需要它忽略 NULL 值并无论如何检索记录。

我发现如果我将表中的所有记录修改为 NULL 以外的内容,例如空格或破折号,查询将找到记录。输入新记录时,表中将添加新的 NULL,并且这些记录将不可搜索。

这些列必须允许 NULL,因为“添加记录”页面会插入到该表和另一个表中,并且它们必须按顺序排列。我发现如果我将列设置为不接受 NULL,则一个表接受数据,而另一个表拒绝数据,那么主键就会不同步。

<?php require_once('Connections/mediadb.php'); ?>
<?php $currentPage = $_SERVER["PHP_SELF"]; ?>

if (isset($_GET['pageNum_rsResults'])) {
$pageNum_rsResults = $_GET['pageNum_rsResults'];
}
$startRow_rsResults = $pageNum_rsResults * $maxRows_rsResults;

$varkind_rsResults = "-1";
if (isset($_GET['searchkind'])) {
  $varkind_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchkind'] : addslashes($_GET['searchkind']);
}
$varcomments_rsResults = "-1";
if (isset($_GET['searchnotes'])) {
  $varcomments_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchnotes'] : addslashes($_GET['searchnotes']);
}
$varrating_rsResults = "-1";
if (isset($_GET['searchrating'])) {
  $varrating_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchrating'] : addslashes($_GET['searchrating']);
}
$varloc_rsResults = "-1";
if (isset($_GET['searchlocation'])) {
  $varloc_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchlocation'] : addslashes($_GET['searchlocation']);
}
$vardiscnum_rsResults = "-1";
if (isset($_GET['searchdisc_number'])) {
  $vardiscnum_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchdisc_number'] : addslashes($_GET['searchdisc_number']);
}
$vardisccnt_rsResults = "-1";
if (isset($_GET['searchdisc_count'])) {
  $vardisccnt_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchdisc_count'] : addslashes($_GET['searchdisc_count']);
}
$vartrackcnt_rsResults = "-1";
if (isset($_GET['searchtrack_count'])) {
  $vartrackcnt_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchtrack_count'] : addslashes($_GET['searchtrack_count']);
}
$vartracknum_rsResults = "-1";
if (isset($_GET['searchtrack_number'])) {
  $vartracknum_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchtrack_number'] : addslashes($_GET['searchtrack_number']);
}
$varyear_rsResults = "-1";
if (isset($_GET['searchyear'])) {
  $varyear_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchyear'] : addslashes($_GET['searchyear']);
}
$varmod_rsResults = "-1";
if (isset($_GET['searchmodified'])) {
  $varmod_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchmodified'] : addslashes($_GET['searchmodified']);
}
$varadd_rsResults = "-1";
if (isset($_GET['searchdate_added'])) {
  $varadd_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchdate_added'] : addslashes($_GET['searchdate_added']);
}
$varrecnum_rsResults = "-1";
if (isset($_GET['searchrecnum'])) {
  $varrecnum_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchrecnum'] : addslashes($_GET['searchrecnum']);
}
$vartime_rsResults = "-1";
if (isset($_GET['searchtime'])) {
  $vartime_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchtime'] : addslashes($_GET['searchtime']);
}
$vargenre_rsResults = "-1";
if (isset($_GET['searchgenre'])) {
  $vargenre_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchgenre'] : addslashes($_GET['searchgenre']);
}
$vargroup_rsResults = "-1";
if (isset($_GET['searchgrouping'])) {
  $vargroup_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchgrouping'] : addslashes($_GET['searchgrouping']);
}
$varalbum_rsResults = "-1";
if (isset($_GET['searchalbum'])) {
  $varalbum_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchalbum'] : addslashes($_GET['searchalbum']);
}
$varcomp_rsResults = "-1";
if (isset($_GET['searchcomp'])) {
  $varcomp_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchcomp'] : addslashes($_GET['searchcomp']);
}
$varartist_rsResults = "-1";
if (isset($_GET['searchartist'])) {
  $varartist_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchartist'] : addslashes($_GET['searchartist']);
}
$varname_rsResults = "-1";
if (isset($_GET['searchname'])) {
  $varname_rsResults = (get_magic_quotes_gpc()) ? $_GET['searchname'] : addslashes($_GET['searchname']);
}
mysql_select_db($database_mediadb, $mediadb);
$query_rsResults = sprintf("SELECT recnum, name, artist, composer, album, `grouping`, genre, `time`,  year, date_modified, date_added, kind, comments, my_rating, location, track_number, track_count, disc_number, disc_count 
FROM media_lib
WHERE name LIKE '%%%s%%' AND artist LIKE '%%%s%%' AND composer LIKE '%%%s%%' AND album LIKE '%%%s%%' AND `grouping` LIKE '%%%s%%' AND genre LIKE '%%%s%%' AND `time` LIKE '%%%s%%' AND recnum LIKE '%%%s%%' AND date_added LIKE '%%%s%%' AND date_modified LIKE '%%%s%%' AND year LIKE '%%%s%%' AND track_number LIKE '%%%s%%' AND track_count LIKE '%%%s%%' AND disc_number LIKE '%%%s%%' AND disc_count LIKE '%%%s%%' AND kind LIKE '%%%s%%' AND comments LIKE '%%%s%%' AND my_rating LIKE '%%%s%%' AND location LIKE '%%%s%%'", $varname_rsResults,$varartist_rsResults,$varcomp_rsResults,$varalbum_rsResults,$vargroup_rsResults,$vargenre_rsResults,$vartime_rsResults,$varrecnum_rsResults,$varadd_rsResults,$varmod_rsResults,$varyear_rsResults,$vartracknum_rsResults,$vartrackcnt_rsResults,$vardiscnum_rsResults,$vardisccnt_rsResults,$varkind_rsResults,$varcomments_rsResults,$varrating_rsResults,$varloc_rsResults);
$query_limit_rsResults = sprintf("%s LIMIT %d, %d", $query_rsResults, $startRow_rsResults, $maxRows_rsResults);
$rsResults = mysql_query($query_limit_rsResults, $mediadb) or die(mysql_error());
$row_rsResults = mysql_fetch_assoc($rsResults);

if (isset($_GET['totalRows_rsResults'])) {
  $totalRows_rsResults = $_GET['totalRows_rsResults'];
} else {
  $all_rsResults = mysql_query($query_rsResults);
  $totalRows_rsResults = mysql_num_rows($all_rsResults);
}
$totalPages_rsResults = ceil($totalRows_rsResults/$maxRows_rsResults)-1;

$queryString_rsResults = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_rsResults") == false && 
        stristr($param, "totalRows_rsResults") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rsResults = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rsResults = sprintf("&totalRows_rsResults=%d%s", $totalRows_rsResults, $queryString_rsResults);
?>
4

1 回答 1

0

您应该能够在您的子句中使用ifnullNULLs 转换为(例如)空字符串。WHERE例如,name LIKE '%%%s%%'将成为ifnull(name, '') LIKE '%%%s%%'.

就像您当前的代码一样,它假定您设置了各种$_GET参数 --1如果未设置参数,它将与您正在使用的参数不匹配。

于 2013-01-01T14:24:27.897 回答