Php noob needs help please check my code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
theoretical_dreamer
Forum Newbie
Posts: 13
Joined: Mon May 22, 2006 12:53 am

Php noob needs help please check my code

Post by theoretical_dreamer »

Guys need help in using forms, I have 4 fields in my database table named "ipadd". I have a search form that handle the queries inputted by the user. The user will input values in this format "192.168.2.1". In the database table, the values in each octet of the IP address is distributed in the four fields. For example using the value "192.168.2.1". The value of the 1st Octet "192" is in the 1st field of the database named "IP1". "168" will be in the second field and so on. However the value of the 1st and the second Octet is constant: "10" and "0". The problem is how can I get the value of the third and 4th Octet in php (can I use the trim command?). Please check my code:


case "ip":
$ip1 = 10;
$ip2 = 0; $ip3 = // What should I put in here?
$ip4 = // What should I put in here?
$rs = mysql_query("SELECT * FROM ipadd WHERE IP1 LIKE'%".$_GET['ip1']."%' AND IP2 LIKE'%".$_GET['ip3']."%' AND IP3 LIKE'%".$_GET['ip3']."%'" AND IP4 LIKE'%".$_GET['ip4']."%'");
break;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Php noob needs help please check my code

Post by RobertGonzalez »

theoretical_dreamer wrote:The problem is how can I get the value of the third and 4th Octet in php (can I use the trim command?). Please check my code:

Code: Select all

<?php
case "ip":
$ip1 = 10;
$ip2 = 0; $ip3 = // What should I put in here? 
$ip4 = // What should I put in here?
$rs = mysql_query("SELECT * FROM ipadd WHERE IP1 LIKE'%".$_GET['ip1']."%' AND IP2 LIKE'%".$_GET['ip3']."%' AND IP3 LIKE'%".$_GET['ip3']."%'" AND IP4 LIKE'%".$_GET['ip4']."%'"); 
break;
?>

Code: Select all

<?php
$user_ip = $_SERVER['REMOTE_ADDR']; // Or whatever you are using for IP
$ip_parts = explode('.', $user_ip);
$ip1 = $ip_parts[0];
$ip2 = $ip_parts[1];
$ip3 = $ip_parts[2];
$ip4 = $ip_parts[3];

// Now do what you will with the octets
>?
theoretical_dreamer
Forum Newbie
Posts: 13
Joined: Mon May 22, 2006 12:53 am

Post by theoretical_dreamer »

Thanks a lot bro, it really helps :D
Post Reply