如何使用MYSQL和两个不同的提交按钮?

时间:2013-05-08 02:49:54

标签: php html mysql forms

我创建了一个歌曲数据库(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>";

}  
?>

3 个答案:

答案 0 :(得分: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'" />

如果值是&#34; list_all&#34;,请处理,否则执行当前处理。

if ($_POST['act'] == 'list_all') {
   // process second submit button code
} else {
   // all your current code
}

答案 1 :(得分:0)

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

<?php
  if (!isset($_POST['NewSongs'])){
     //do the search
  } else {
     //don't do the search
  }
?>

答案 2 :(得分: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
}
?>