Page 1 of 1

Query Problem

Posted: Wed Jan 26, 2011 10:08 am
by batowiise
Hi All,

Have a problem with this piece of script.

Step 1.

Currently, the script first displays a form that asks the user to click on button to perform a search.

Step 2.
It then displays the search criteria to use.

Step 3.
When the criteria are defined, it then queries the database and displays the return results.

How can I skip the two steps and display the return results.

Your help is welcome.

Code: Select all

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
	  <table width="779" border="0" align="left">
        <tr>
          <td width="846" colspan="4">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="4"><?php
if (isset($_GET['dosearch'])): // If the user wants to perform a search
?>
              <table width="767" border="0" cellpadding="2" cellspacing="2">
                <tr>
                  <td valign="top"><strong>Criteria:</strong></td>
                  <td colspan="2" valign="top"><select name="criteria" id="search">
                      <option>Mails received from front-desk</option>
                    </select>                  </td>
                </tr>
                <tr>
                  <td width="97" valign="top">&nbsp;</td>
                  <td colspan="2" valign="top">&nbsp;</td>
                </tr>
                <tr>
                  <td valign="top"><strong>Date: <br />
                  </strong></td>
                  <td valign="top">Start Date:&nbsp;&nbsp;&nbsp; </td>
                  <td valign="top"><input name="date1" />
                      <script language="JavaScript" type="text/javascript" id="js1">
var cal1 = new CalendarPopup();
                              </script>
                    <a href="#" onclick="cal1.select(document.forms[0].date1,'anchor1','yyyy/MM/dd'); return false;" title="cal1.select(document.forms[0].date1,'anchor1','MM/dd/yyyy'); return false;" name="anchor1" id="anchor1"><img src="../images/images/calendar.png" alt="Calendar" border="0" title="Click to use calendar" /></a></td>
                </tr>
                <tr>
                  <td width="97" valign="top">&nbsp;</td>
                  <td width="76" valign="top"><p>End Date: </p></td>
                  <td width="574" valign="top"><input name="date2" />
                      <script language="JavaScript" type="text/javascript" id="js1">
var cal1 = new CalendarPopup();
                              </script>
                    <a href="#" onclick="cal1.select(document.forms[0].date2,'anchor1','yyyy/MM/dd'); return false;" title="cal1.select(document.forms[0].date2,'anchor1','MM/dd/yyyy'); return false;" name="anchor1" id="anchor1"><img src="../images/images/calendar.png" alt="Calendar" border="0" title="Click to use calendar" /></a></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <tr>
                  <td><strong>Details:</strong></td>
                  <td colspan="2"><input name="searchstring" id="searchstring" /></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td colspan="2"><input name="finddata" type="submit" class="plainbtn" value="Search" /></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td colspan="2">&nbsp;</td>
                </tr>
              </table>
            <p>&nbsp;</p>
            <?php
else: // Default page display
// Connect to the database server
require_once("../admin/db.php");


// add it to the database.
 
if (isset($_POST['finddata'])) {
	$criteria = $_POST['criteria'];
	$date1 = $_POST['date1'];
	$date2 = $_POST['date2'];
	$searchstring = $_POST['searchstring'];
	
	if($criteria=="Mails received from front-desk")
	{	 
		$result = @mysql_query("SELECT * FROM incomingmails WHERE status = 'Front-Desk' ORDER BY arrivaldate DESC, entryid ASC");
		$report_title="Mails received from front-desk";

	}		

if (!$result) {
die('<p>Error performing query: ' .
mysql_error() . '</p>');
}
else{

//echo('<p align=center><a href="' . $_SERVER['PHP_SELF'] .
//'?dosearch=1">Please click here to perform a search</a> | <a href="#" onclick="window.print();return false;">Print page </a></p>');

echo"<p><b>$report_title</b></p>";

echo('<table class="data-table"><thead><tr>' );
					echo('<td scope="col">Date</td><td>Sender</td><td>Country/City of Origin</td><td>Recipient</td><td>Unit</td><td>Mail Registration ID</td><td>Status</td><td>Remarks</td><td>Action</td>' );
					echo('</tr></thead>' );	
			echo('</tr></thead>' );				

		$rowcount=1;
		while (($row = mysql_fetch_array($result) and ($rowcount<=mysql_affected_rows()))) {
		
			if (($rowcount%2)==0){
				
				echo('<tr class="even">' );
				
				
				echo('<td valign=top>'. $row['arrivaldate'] . '</td>');
				echo('<td valign=top>'. $row['sender'] . '</td>');
				echo('<td valign=top>'. $row['senderlocation'] . '</td>');
				echo('<td valign=top>'. $row['recipient'] . '</td>');
				echo('<td valign=top>'. $row['unit'] . '</td>');				
				echo('<td valign=top>'. $row['registrationid'] . '</td>');				
				
				
				if($row['status']=="PENDING")
				{
					echo('<td valign=top><b><blink>'. $row['status'] . '</blink></b></td>');
				}
				else{
					echo('<td valign=top><b>'. $row['status'] . '</b></td>');
				}
				
				echo('<td valign=top>'. $row['remarks'] . '</td>');
						
								
				echo("<td valign=top><a href=\"" . $_SERVER['PHP_SELF'] . "?updaterecord=" .$row['entryid'] . "&Submit=Edit\"><img title=\"Edit\" alt=\"Edit\" src=\"../images/edit.png\"></a></td></tr>\n\n");

	
				echo('</tr>' );	
				}
				else{
				echo('<tr class="odd">' );
					
					echo('<td valign=top>'. $row['arrivaldate'] . '</td>');
					echo('<td valign=top>'. $row['sender'] . '</td>');
					echo('<td valign=top>'. $row['senderlocation'] . '</td>');
					echo('<td valign=top>'. $row['recipient'] . '</td>');
					echo('<td valign=top>'. $row['unit'] . '</td>');				
					echo('<td valign=top>'. $row['registrationid'] . '</td>');									
					
					if($row['status']=="PENDING")
					{
						echo('<td valign=top><b><blink>'. $row['status'] . '</blink></b></td>');
					}
					else{
						echo('<td valign=top><b>'. $row['status'] . '</b></td>');
					}
					
					echo('<td valign=top>'. $row['remarks'] . '</td>');
								
				echo("<td valign=top><a href=\"" . $_SERVER['PHP_SELF'] . "?updaterecord=" .$row['entryid'] . "&Submit=Edit\"><img title=\"Edit\" alt=\"Edit\" src=\"../images/edit.png\"></a></td></tr>\n\n");
				
				echo('</tr>' );	
				
				}
				
				$rowcount++;
				
				
		} //end of while loop
		
		
		
		echo('</tr></table>' );	
		
		echo('<p><b> Your request returned '. mysql_affected_rows(). ' record(s).</b></p>');
		}

}


if (isset($_GET['deleterecord'])) {  
			   
					   
		echo"<p>Do you want to delete record?</p>";	   
		
		echo"<p>&nbsp;</p>";
		
		echo"<p><input type=\"submit\" name=\"btnDelete\" id=\"btnDelete\" class=\"plainbtn\" value=\"Yes\">  <input type=\"submit\" name=\"btnCancel\" id=\"btnCancel\" class=\"plainbtn\" value=\"No\"></p>";
		$entryid = $_GET['deleterecord'];
		echo"<input type=hidden name=entryid value=$entryid>";
	}
	
	//DELETE BUTTON - YES
	if (@$_REQUEST['btnDelete'] == "Yes") 
	{ 
		$entryid = $_REQUEST['entryid'];
		$sql = "DELETE FROM incomingmails WHERE entryid=$entryid";      
		if (@mysql_query($sql)) {
	
			echo('<p><b>Record has been successfully deleted.</p> 
			<p><a href="' . $_SERVER['PHP_SELF'] .
'?dosearch=1">Please click here to return to search page.</a></b></p>');  
		} 
		else 
		{        
		
			echo('<p><font color=\"red\"><b>Error deleting record: ' .  mysql_error() . '</b></font></p>'); 			
		
		}		  

	}
	
	//DELETE BUTTON - CANCEL
	if (@$_REQUEST['btnCancel'] == "No") 
	{ 

			echo('<p><b>Delete operation was cancelled.</p> 
			<p><a href="' . $_SERVER['PHP_SELF'] .
'?dosearch=1">Please click here to return to search page.</a></b></p>');  
		
	} 
	
	
//update details
if (isset($_GET['updaterecord'])) {  
		
		
		
		$entryid=$_GET['updaterecord'];
		$sql =  "SELECT * FROM incomingmails WHERE entryid = '$entryid'"; 
		
		/* Passes query to database */ 
		
		$result = mysql_query($sql); 
		if (!$result) { 
		  echo("<p>Error performing query: " . mysql_error() . "</p>"); 
		  exit(); 
		}  
		
		/* creates our row array with an if statement to report errors */ 
		
		if ($row = @mysql_fetch_array($result, MYSQL_ASSOC)) {  
		
		/* prints out the artist and title */ 
		
		
		

//		echo('<p align=center><a href="' . $_SERVER['PHP_SELF'] .
//'?dosearch=1">Please click here to perform a search</a> | <a href="#" onclick="window.print();return false;">Print page </a></p>');
		
		
		print"<p><table class=\"data-table\"><thead>
		<tr>
			<td width=\"102\"><b>Details of Mails Received By $row[recipient] On $row[arrivaldate]</td>
		</tr></thead></table></b></p>"; 
		
		
		print "<form name=\"issue\" method=\"post\" action=\"$_SERVER[PHP_SELF]\">"; 
					
  		echo"<p><table width=\"630\" border=\"0\" align=\"left\">
          <tr>
            <td colspan=\"4\"><div class=\"content_title\"></div></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td width=\"128\">&nbsp;</td>
            <td width=\"183\">&nbsp;</td>
          </tr>
          <tr>
            <td>Date:</td>
            <td><input name=\"arrivaldate\" id=\"arrivaldate\" value=\"$row[arrivaldate]\"/>
              <script language=\"JavaScript\" type=\"text/javascript\" id=\"js1\">
var cal1 = new CalendarPopup();
                              </script>
              <a href=\"#\" onclick=\"cal1.select(document.forms[0].arrivaldate,'anchor1','yyyy/MM/dd'); return false;\" title=\"cal1.select(document.forms[0].arrivaldate,'anchor1','MM/dd/yyyy'); return false;\" name=\"anchor1\" id=\"anchor1\"><img src=\"../images/images/Calendar.png\" alt=\"Calendar\" border=\"0\" title=\"Click to use calendar\" /></a></td>
            <td>Time:</td>
            <td><input name=\"arrivaltime\" id=\"arrivaltime\" value=\"$row[arrivaltime]\"/></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td colspan=\"2\">&nbsp;</td>
          </tr>
          <tr>
            <td colspan=\"4\"><p align=\"center\">
              <input name=\"Submit\" type=\"Submit\" class=\"plainbtn\" id=\"submit\" value=\"Update\" />              
            </p></td>
          </tr>
        </table></form></p></td></tr></table>";
  
}

}

if (@$_REQUEST['Submit'] == "Update") { 

		$entryid = $_REQUEST['entryid'];
		$arrivaldate = $_REQUEST['arrivaldate'];
		$arrivaltime = $_REQUEST['arrivaltime'];
		
		$sql_update = "UPDATE incomingsnailmails SET arrivaldate = '$arrivaldate',
		arrivaltime = '$arrivaltime',		
		arrivaldate = '$arrivaldate'
		 WHERE entryid = '$entryid'"; 
									
		/* Passes query to database */ 
		
		$result = mysql_query($sql_update); 
		if (!$result) { 
		  echo("<b><p><font color=\"red\">Error performing query: " . mysql_error() . "</font></p><p><a href=\"" . $_SERVER['PHP_SELF'] . "?updaterecord=" .$entryid. "&Submit=Edit\">Click here</a> to return to details of mail with Tracking Number $trackingno.</p></b>"); 
		  exit(); 
		} 
		
		else{
			echo"<b>Record updated successfully.<p><a href=\"" . $_SERVER['PHP_SELF'] . "?updaterecord=" .$entryid. "&Submit=Edit\">Click here</a> to return to details of mail with Tracking Number $trackingno</p></b>";
		}

}


 
//function for updating record
function update_records() { 

/* Prints succes message */ 

print "<p> Successfully Updated</p>"; 

/* closes connection */ 

mysql_close ($conn); 

/* Calls get_data() function */ 

//get_data(); 

} 

// When clicked, this link will load this same page
echo('<p></p>');
//echo('<p align=center name=bottom><a href=#top>Top of Page</a></p>');

echo('<p align=center><a href="' . $_SERVER['PHP_SELF'] .
'?dosearch=1"><input type="Submit" name="Submit" id="Submit" value="Click here to perform a search" /> | <a href="#" onclick="window.print();return false;"><input type="Submit" name="Submit" id="Submit" value="Print Page" /></a></b></p>');
endif;
?>
            </p></td>
        </tr>
      </table>
	</form>
Regards.