2014-11-20 81 views
0

我創建了一個連接到mysql的日曆。日曆搜索mysql並顯示在休息時間的員工數量。有幾個經理爲各種各樣的僱員。我創建了一個searchterm框,管理員可以在其中鍵入名稱,代碼將查詢特定於管理員名稱的數據庫(實質上只顯示經理員工而不是整個公司)。僱員休假時間的數量在日曆內顯示爲鏈接和該特定日期的總數。一旦點擊它,然後顯示與當天關聯的員工姓名。我們遇到的問題是,經理點擊鏈接後,它會自動默認爲所有員工,而不是特定於經理的員工。經理人的搜索字詞正在被刪除,代碼默認回覆,就像沒有輸入任何內容。我的問題是,我如何可以重複使用該searchterm,直到其他明智的指示。PHP,Mysql日曆

$searchTerm = trim($_GET['keyname']); 


if($searchTerm != 'All Drivers' && $searchTerm != '') 

{ 
$sqlEvent2 = mysql_query("select * FROM timeoff_365_days where (DM = '$searchTerm' or FM = '$searchTerm' or region ='$searchTerm' or location ='$searchTerm') and TimeOffDate = '".$year."-".$month."-".$i."'"); 
$num_rows = mysql_num_rows($sqlEvent2); 

echo '<div id="button">'; 
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$i."&year=".$year. "&v=false ' >".$num_rows."</a></td>"; 
echo '</div>'; 


} 
else{ 






$sqlEvent = mysql_query("select * FROM timeoff_365_days where TimeOffDate = '".$year."-".$month."-".$i."'" ); 
if (!$sqlEvent) { 
    echo 'Could not run query: ' . mysql_error(); 
    exit; 
} 



$num_rows = mysql_num_rows($sqlEvent); 
echo '<div id="button">'; 
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$i."&year=".$year."&v=true' >".$num_rows."</a></td>"; 
echo '</div>'; 

$sqlEvent = mysql_query("select * FROM timeoff_365_days where TimeOffDate = '".$year."-".$month."-".$i."'" ); 
if (!$sqlEvent) { 
    echo 'Could not run query: ' . mysql_error(); 
    exit; 
} 



$num_rows = mysql_num_rows($sqlEvent); 
echo '<div id="button">'; 
echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$i."&year=".$year."&v=true' >".$num_rows."</a></td>"; 
echo '</div>'; 

} 

} 

echo "<tr>"; 
echo"</table>"; 
?> 
<div class="accordion vertical"> 
<ul> 
<li> 
<input type="radio" id="radio-3" name="radio-accordion" /> 
      <label for="radio-3">Time Off by Driver Code</label> 
      <div class="content"> 




<?php 
if(($_GET['v']==false)) { 
$sqlEvent2 = "select * FROM timeoff_365_days where (DM = '$searchTerm' or FM = '$searchTerm' or region ='$searchTerm'or location ='$searchTerm') and TimeOffDate ='".$year."/".$month."/".$day."'"; 
$resultEvents2 = mysql_query($sqlEvent2); 

while ($events2 = mysql_fetch_array($resultEvents2)){ 
echo $events2['DriverCode']."-"; 
echo $events2['Unit']."</br>"; 
} 
} 
else { 
echo ""; 

} 
?> 

<?php 
echo "<tr >"; 
var_dump($searchTerm); 


if(isset($_GET['v'])) { 
$sqlEvent = "select * FROM timeoff_365_days where TimeOffDate ='".$year."/".$month."/".$day."'"; 
$resultEvents = mysql_query($sqlEvent); 

while ($events = mysql_fetch_array($resultEvents)){ 
echo $events['DriverCode']."-"; 
echo $events['Unit']."</br>"; 
} 
} 
else { 
echo ""; 
} 
echo "<tr>"; 

var_dump($searchTerm); 

?> 
+0

請,或不使用'mysql_ *'功能(HTTP: //stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php),他們不再維護和[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。學習[準備的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://us1.php.net/pdo)或[MySQLi](http:// us1.php.net/mysqli)。你也會想[防止SQL注入!](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – 2014-11-20 19:19:38

回答

1

與$ _GET得到它,所以用domain.com/index.php?search=asddf做url中

+1

我的搜索欄與日曆。我最終會將其格式化爲一個菜單位,現在只是試圖讓它通過文本搜索。每次我在搜索中輸入名稱,頁面都會重新加載並丟失searchterm。同樣的事情發生在我點擊鏈接時。無論如何要讓代碼在每次重新加載時都引用searchterm? – josh 2014-11-20 19:16:17

+1

如果您提交表單,您已經獲得了searchquery,因此在鏈接中使用$ _POST – jmattheis 2014-11-20 21:17:06