Jump to content


Photo

Else if Else help mysql


  • Please log in to reply
11 replies to this topic

#1 mcdrr

mcdrr
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 March 2006 - 01:47 AM

Here is what I changed the code to. I am unable to get any results. Can anyone point me in the right direction as to where/what is wrong with this code? Am I supposed to print the results after every if statement? If so can someone give me an example.

if {
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."'";
}
else if {
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%')";
}
else {
"select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%') and (inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."')";
}
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
//this is a line break before the spit output
while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';
echo '<td align="center">',$line[3],'</td>';
echo '<td align="center">',$line[4],'</td>';
echo '<td align="center">',$line[5],'</td>';
}
?>

THANKS AGAIN FOR THE HELP

#2 khendar

khendar
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 28 March 2006 - 02:51 AM

What are the if statements for ? There are no conditions to be satisfied for the if statements.

An if statement should look like:

if($something==true)
{
// do something
}



#3 mcdrr

mcdrr
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 March 2006 - 02:56 AM

what would be the best way to do this if statement. Like I mentioned I am really new to all of this. I am trying to do if statements so that if a users wants to search the DB by just entering and IP address or just entering a date or entering both date and IP address the query will print out results.

So for example how would I do

if user enters just IP info on (but should this would be a query or something else)
then print out the results

if user enters just DATE info
then print out the results

if user enters both IP and DATE info
then print out the results

thanks for the HELP I really appreciate it.


#4 khendar

khendar
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 28 March 2006 - 03:51 AM

Then you should have something like:

if (isset($ip) && !isset($date))
{
$query1 = "...";
}

if (!isset($ip) && !isset($date))
{
$query2 = "...";
}

if (isset($ip) && isset($date))
{
$query3 = "...";
}

Substitude $ip, $date, and $queryX for your variable names and you should be right.

#5 mcdrr

mcdrr
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 March 2006 - 04:36 AM

Thanks again for the reply.

How would I print the output for each if statement. Do I print it like I had it before?? Or do I have to print it out in a different way.

thanks for you patience and help once again!

$result = mysql_query($query) or die('Query failed: ' . mysql_error());
//this is a line break before the spit output
while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';
echo '<td align="center">',$line[3],'</td>';
echo '<td align="center">',$line[4],'</td>';
echo '<td align="center">',$line[5],'</td>';
}
?>

#6 khendar

khendar
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 28 March 2006 - 04:42 AM

If you use the same variable name for the $query then you only need one mysql_query statement.



#7 mcdrr

mcdrr
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 March 2006 - 05:18 AM

Here is the entire code that I have. I have made the changes per your advice. I am still having problems print any results. I replaced if ($IP) with $Sdate. not sure if that is right?? THANKS FOR ALL YOUR HELP.

if (isset($Src) && !isset($Sdate))
<th align="center">Source_Port</th>
<th align="center">Destination_Port</th>
<th align="center">Date_Time</th>
<th align="center">Signature</th>
<th align="center">BASE_Link </th>

<?
$host = "localhost";
$user = "xxxxx";
$pass = "xxxx";
$dbname = "xxxxx";
$Src = $_POST['Src'];
$Sdate = $_POST['Sdate'];
$Edate = $_POST['Edate'];

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
//Query that is being run
if (isset($Src) && !isset($Sdate))
{
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."'";
}

if (!isset($Src) && !isset($Sdate))
{
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%')";
}

if (isset($Src) && isset($Sdate))
{
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%') and (inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."')";
}

//this will print the query results
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
//this is a line break before the spit output
while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';
echo '<td align="center">',$line[3],'</td>';
echo '<td align="center">',$line[4],'</td>';
echo '<td align="center">',$line[5],'</td>';
}
?>

#8 khendar

khendar
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 28 March 2006 - 05:31 AM

Well you have some issues in here (fixes in bold)

while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">'.$line[0].'</td>';
echo '<td align="center">'.$line[1].'</td>';
echo '<td align="center">'.$line[2].'</td>';
echo '<td align="center">'.$line[3].'</td>';
echo '<td align="center">'.$line[4].'</td>';
echo '<td align="center">'.$line[5].'</td>';
echo '</tr>';
}

If that still doesn't work:

Try echoing the $query out before executing it to see what its looking for.

If you have phpmyadmin installed try running the query from there to see if the problem is in the data retrieval or in the display.

Try doing print_r($line) inside the loop to see if the data is being returned.




#9 mcdrr

mcdrr
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 March 2006 - 06:04 AM

I am complely lost here. I am not sure what I am supposed to change the line below to. Before I had to add the if statements all my code was working before. I used the echo's below to print out each one of the columns which work just fine. Is there a better way to print out each query in the individual IF statements?

echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';

Here is what I have tried but not sure if I have added the print_r($query) in this code but get either no error or data results.

Also what am I supposed to be replacing the ($)in the following if statement.
if (isset($Src) && isset($Sdate)) is this any variable I want of am I supposed to define it somewhere?

If anyone have any ideas it would be really helpful AS I AM COMPLETELY STUCK AND LOST.

thanks for ALL the HELP.

<table border="1" width="82%" cellpadding="1" cellspacing="2">
<th align="center">Source_IP</th>
<th align="center">Destination_IP</th>
<th align="center">Source_Port</th>
<th align="center">Destination_Port</th>
<th align="center">Date_Time</th>
<th align="center">Signature</th>
<th align="center">BASE_Link </th>

<?
$host = "xxxx";
$user = "xxxxx";
$pass = "xxxxx";
$dbname = "xxxxx";
$Src = $_POST['Src'];
$Sdate = $_POST['Sdate'];
$Edate = $_POST['Edate'];

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
//Query that is being run
if (isset($Src) && !isset($Sdate))
{
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."'";
print_r($query)
}

if (!isset($Src) && !isset($Sdate))
{
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%')";
print_r($query)
}

if (isset($Src) && isset($Sdate))
{
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%') and (inet_ntoa(ip_src) like '" .$Src."' or inet_ntoa(ip_dst) like '" .$Src."')";
print_r($query)
}

//this will print the query results
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
//this is a line break before the spit output
while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';
echo '<td align="center">',$line[3],'</td>';
echo '<td align="center">',$line[4],'</td>';
echo '<td align="center">',$line[5],'</td>';


#10 khendar

khendar
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 28 March 2006 - 06:23 AM

if (isset($Src) && isset($Sdate))

These should be the variables that you said a user was able to enter which determined which query is to be run ie the IP and the DATE (i'm guessing $Src and $Sdate). You need to define your logic a little better - which date is the one they must enter ? - The Sdate of the Edate ?

Please look at the parts I highlighted in bold. You should replace the , with . and add the </tr>. These may not fix your problem but need to be fixed anyway.

if (!isset($Src) && !isset($Sdate)) should be if (!isset($Src) && isset($Sdate)) (if SDate only is entered)

When you ran the code you posted last - did you get a print out of the $query ? It should have displayed what query was being executed.


#11 mcdrr

mcdrr
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 28 March 2006 - 04:37 PM

Ok here is what I got so far. I have the following code below sorta working. The problem is that when I do a !isset statement it stops working.
For example
If I do this if (isset($_POST['Sdate']) && isset($_POST['Edate'])) it works FINE
If I change it to this if (isset($_POST['Sdate']) && !isset($_POST['Edate'])) then I get a blank page for results.

The other problem that I have is that if I add another if statement just like the one below (which is currenlty working, but with IP ADDRESS variables) neither the working one or the new one work. Again I just get a blank page.


Here is the code that currently works for searching just for the dates. Like I said ONCE a new if isset statement gets added neither if statement works. So I guess my question is how would I add another if isset statement that works when the $_POST['Src']; variable is set instead of $_POST['Sdate'] like I have below.

THANKS AGAIN FOR ANY HELP OR IDEAS!!!


<table border="1" width="82%" cellpadding="1" cellspacing="2">
<th align="center">Source_IP</th>
<th align="center">Destination_IP</th>
<th align="center">Source_Port</th>
<th align="center">Destination_Port</th>
<th align="center">Date_Time</th>
<th align="center">Signature</th>
<th align="center">BASE_Link </th>

<?
$host = "";
$user = "";
$pass = "";
$dbname = "";
$Src = $_POST['Src'];
$Sdate = $_POST['Sdate'];
$Edate = $_POST['Edate'];

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
//Query that is being run
if (isset($_POST['Sdate']) && isset($_POST['Edate']))
{
$query = "select inet_ntoa(ip_src), inet_ntoa(ip_dst), layer4_sport, layer4_dport, timestamp, sig_name from acid_event where (timestamp between '" .$Sdate."%' and '" .$Edate."%')";
print_r($query);
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';
echo '<td align="center">',$line[3],'</td>';
echo '<td align="center">',$line[4],'</td>';
echo '<td align="center">',$line[5],'</td>';
}
}
?>

#12 khendar

khendar
  • Members
  • PipPipPip
  • Advanced Member
  • 78 posts

Posted 28 March 2006 - 11:34 PM

You are not reading my replies.

You need an if statement for each test you will make, and only ONE mysql_query call and results display: ie

if(isset($variable1) && isset($variable2))
{
$query = "...";
}

if(!isset($variable1) && isset($variable2))
{
$query = "...";
}


if(isset($variable1) && !isset($variable2))
{
$query = "...";
}


$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($line = mysql_fetch_row($result) )
{
echo '<tr>';
echo '<td align="center">',$line[0],'</td>';
echo '<td align="center">',$line[1],'</td>';
echo '<td align="center">',$line[2],'</td>';
echo '<td align="center">',$line[3],'</td>';
echo '<td align="center">',$line[4],'</td>';
echo '<td align="center">',$line[5],'</td>';
}

And make sure you address the points that I have already stated three times about the display code.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users