2013-05-08 112 views
0

我創建了一個歌曲數據庫(mysql),用戶可以在其中輸入數據到文本框中,選擇「按藝術家」或「按標題」單選按鈕,然後單擊提交。這一切工作正常。 :)如何使用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>"; 

} 
?> 

回答

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 
} 
0

你要檢查NewSongs提交按鈕提交表單上,並使用在if語句做查詢或不是。

<?php 
    if (!isset($_POST['NewSongs'])){ 
    //do the search 
    } else { 
    //don't do the search 
    } 
?> 
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 
} 
?> 
相關問題