Update Row If IP = IP in database

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

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

echo $id and $ip

i'll bet you one of those isn't set
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

They both are.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

naa if they're not set they shouldnt do an error, just put in a blank value AFAIK.

Post your current code again please, you've probably changed a lot in it.
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

The code as of now is

Code: Select all

<font color=white>
<? 
$username= "muot_report";  
$password= "report";  
$database= "muot_report";  
$connection = mysql_connect('localhost',$username,$password);  
mysql_select_db($database); 

$id = $_POST['id']; 
$ip = $_POST['ip']; 

$result = mysql_query("SELECT * FROM `comments` WHERE `ip` = '$ip'") or die(mysql_error()); 
while($row = mysql_fetch_array($result)) 
{ 
        if($ip == $row['ip']) 
        { 
                mysql_query("UPDATE `comments` set `show` = 0 WHERE `id` = $id") or die(mysql_error()); 
        } 
} 
mysql_close($connection); 
?>
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

sorry my comment about == wont solve your error but is still needed for what you want


sorry i have no ideas for the error

thanks reece
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

Ok, I'm trying to see what you're doing here

Code: Select all

<font color=white>
<?
$username= "muot_report"; 
$password= "report"; 
$database= "muot_report"; 
$connection = mysql_connect('localhost',$username,$password); 
mysql_select_db($database);

$id = $_POST['id'];
$ip = $_POST['ip']; 

mysql_query("UPDATE `comments` SET `show`=0 WHERE `ip`='$ip' AND `id`=$id") or die(mysql_error());
mysql_close($connection);
?>
I think this should do the same thing, try it :{]
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Same error with that code too..
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

Ok, check yoru fields in the database, are they called like that? Thats the only thing I can think of...
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

Also, show is an integer type right?
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

CHAR
Last edited by nickman013 on Sun Oct 22, 2006 3:24 pm, edited 1 time in total.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

hahaha
then do `show`='0'
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

Now Its INT and it doenst work still.
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post by Flamie »

Code: Select all

<font color=white>
<?
$username= "muot_report";
$password= "report";
$database= "muot_report";
$connection = mysql_connect('localhost',$username,$password);
mysql_select_db($database);

$id = $_POST['id'];
$ip = $_POST['ip'];
echo "id: ".$id."<br>ip: ".$ip."<br>";
mysql_query("UPDATE `comments` SET `show`=0 WHERE `ip`='$ip' AND `id`=$id") or die(mysql_error());
mysql_close($connection);
?>
whast the full ouput of this script?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Flamie beat me to it..

couple of remarks though: validate your user input... Don't use it 'as is' in your query...
And an ip-address is simply a number, unsigned int(10) seems a more appropriate type for the column than a varchar.. (lookup the inet_aton and inet_ntoa functions in your mysql manual).
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post by nickman013 »

The code now is

Code: Select all

<font color=white>
<? 
$id = $_POST['id']; 
$ip = $_POST['ip']; 

$username= "muot_report"; 
$password= "report"; 
$database= "muot_report"; 
$connection = mysql_connect('localhost',$username,$password); 
mysql_select_db($database); 


mysql_query("UPDATE `comments` SET `show`=0 WHERE `ip`='$ip' AND `id`=$id") or die(mysql_error()); 
mysql_close($connection); 
?>
The error is

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1



My database structure is

Code: Select all

CREATE TABLE `comments` (
  `id` int(99) NOT NULL auto_increment,
  `name` varchar(99) NOT NULL default '',
  `comment` text NOT NULL,
  `who` int(99) NOT NULL default '0',
  `ip` varchar(50) NOT NULL default '0',
  `show` int(2) NOT NULL default '1',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1040 DEFAULT CHARSET=latin1 AUTO_INCREMENT=1040 ;
Post Reply