Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/#findComment-21431
Share on other sites

Then you should have something like:

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

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

if (isset($ip) && isset($date))
{
$query3 = "...";
}
[/code]

Substitude $ip, $date, and $queryX for your variable names and you should be right.
Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/#findComment-21438
Share on other sites

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>';
}
?>
Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/#findComment-21450
Share on other sites

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>';
}
?>
Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/#findComment-21461
Share on other sites

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

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

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.


Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/#findComment-21467
Share on other sites

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>';
Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/#findComment-21470
Share on other sites

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 [b]bold[/b]. You should replace the [b],[/b] with [b].[/b] and add the [b]</tr>[/b]. These may not fix your problem but need to be fixed anyway.

if (!isset($Src) && !isset($Sdate)) should be [b]if (!isset($Src) && isset($Sdate)) (if SDate only is entered)[/b]

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.
Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/#findComment-21473
Share on other sites

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>';
}
}
?>
Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/#findComment-21642
Share on other sites

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

[code]
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>';
}
[/code]

And make sure you address the points that I have already stated three times about the display code.
Link to comment
https://forums.phpfreaks.com/topic/5979-else-if-else-help-mysql/#findComment-21763
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.