0

我创建了一个歌曲数据库(mysql),用户可以在其中将数据输入文本字段,选择“按艺术家”或“按标题”单选按钮,然后单击提交。这一切都很好。:)

但是,我想添加另一个提交按钮,绕过上述查询并简单地列出所有已添加到数据库中的歌曲以及一年的时间。

我相信我需要的查询是:

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)

但我无法弄清楚如何编写 PHP 代码来运行过去一年内的歌曲查询。

这是我有的两种形式:

<p>
<form name="form" method="get" action="">
    Search for: <input type="text" name="q" />
    <input type="radio" name="field" value="title" <?=$checkTitle?> checked> Title
    <input type="radio" name="field" value="artist" <?=$checkArtist?> > Artist
  <input type="submit" name="Submit" value="Search" />
</form></p>
<form name="form" method="post" action="">
    <br />
    <input type="submit" name="Newsongs" value="New Releases" />
</form></p>

这是第一个查询的当前工作 php:

<?php
    $delimiter = "\t";
    $newline = "\n";

  $var = @$_GET['q'] ;
  $field = @$_GET['field'];
  $var = htmlentities($var);
  $trimmed = trim($var); //trim whitespace from the stored variable
  $search_words = explode(' ', $var);


if ($var != "") {

// rows to return
$limit=100; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database 
mysql_connect("DBLocation","DatabaseName","PASSWORD"); //(host, username, password)

//specify database 
mysql_select_db("DatabaseName") or die("Unable to select database"); //select which database we're using

// Build SQL Query  


    $query = "SELECT * FROM Songs where $field like \"%$trimmed%\" order by $field "; 

 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";

// begin to show results set
echo "<p style=\"color: #00ff00; font-size: 1.2em;\">Results</p>";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["title"];
  $artist = $row["artist"];

  echo "$title";
  echo " - ";
  echo "$artist";
  echo "<br />";
  echo $newline;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";


$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";

}  
?>
4

3 回答 3

0

您可以添加一个隐藏字段,然后在提交时检查它:

<input type="hidden" name="act" id="act" value="">

然后在您的第二个提交按钮上,使用 JavaScript 更改值:

<input type="submit" name="Newsongs" value="New Releases" onclick="document.getElementById('act').value = 'list_all'" />

如果值为“list_all”,则处理该值,否则执行当前处理。

if ($_POST['act'] == 'list_all') {
   // process second submit button code
} else {
   // all your current code
}
于 2013-05-08T02:58:32.840 回答
0

您需要检查 NewSongs 提交按钮是否在提交的表单上,并在 if 语句中使用它来执行查询。

<?php
  if (!isset($_POST['NewSongs'])){
     //do the search
  } else {
     //don't do the search
  }
?>
于 2013-05-08T02:58:57.593 回答
0

您可以在表单中使用 2 个提交按钮:

    <p><form name="form" method="get" action="">
 Search for: <input type="text" name="q" />
    <input type="radio" name="field" value="title" <?=$checkTitle?> checked> Title
    <input type="radio" name="field" value="artist" <?=$checkArtist?> > Artist
  <input type="submit" name="submit" value="Search" />
<input type="submit" name="submit" value="New" />
</form>
</p>

<?php
if(isset($_GET['submit']) && $_GET['submit'] == 'Search'){
    // do search
}
if(isset($_GET['submit']) && $_GET['submit'] == 'New'){
    // do second search
}
?>
于 2013-05-08T03:04:48.487 回答