Page 1 of 1

"Column count doesn't match value count at row 1"

Posted: Fri Jan 12, 2007 12:28 pm
by oskare100
Hello,
I'm trying to fix this login script, but I always get the error Column count doesn't match value count at row 1 . Can you see any errors in the code/database structure or do you know another way of doing it?

The script:

Code: Select all

<?php
session_start();

include 'db_info.php'; 
// Connect to server and select databse. 
mysql_connect("$sqlhost", "$sqlusername", "$sqlpassword")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// username and password sent from signup form 
$vusername=$_POST['vusername']; 
$vpassword=$_POST['vpassword']; 

$sql="SELECT * FROM $user_tbl WHERE username='$vusername' and password='$vpassword'"; 
if ($result=mysql_query($sql) or die( mysql_error() )) {

    if(mysql_num_rows($result) == 1) {
        // If result matched $vusername and $vpassword, table row must be 1 row 
        $row = mysql_fetch_assoc($result);
        $vuserid = $row['user_id'];
    
        //Register session variables
        $_SESSION["vusername"] = $vusername;
        $_SESSION["vpassword"] = $vpassword;
        
        // Log the login in the ip log table
        $sql="INSERT INTO $login_logs_tbl (user_id, ip, logged) VALUES('$vuserid', '".$_SERVER['REMOTE_ADDR']."', CURRENT_DATE, NOW())"; 
        mysql_query($sql) or die( mysql_error() );
    
        // Set the latest login in the user table
        $sql="UPDATE $user_tbl SET (latest_login, num_logins) VALUES (CURRENT_DATE, NOW(),num_logins+1) where user_id = '$vuserid'"; 
        mysql_query($sql) or die( mysql_error() );
    
        // Check if the IP is already logged in the database 
        $sql="update $ip_logs_tbl set (latest) values (CURRENT_DATE, NOW()) where ip = '".$_SERVER['REMOTE_ADDR']."' AND user_id='$vuserid'"; 
        mysql_query($sql) or die( mysql_error() );
        if (mysql_affected_rows() == 0) {
             // It's a new IP for that user - log it 
            $sql="INSERT INTO $ip_logs_tbl (ip, user_id, latest) VALUES('".$_SERVER['REMOTE_ADDR']."', '$vuserid', CURRENT_DATE, NOW())"; 
            mysql_query($sql) or die( mysql_error() ); 
            
             // And add 1 to the number of different IPs in the user table 
            $sql="UPDATE $user_tbl SET num_ips = num_ips+1 where user_id = '$vuserid'"; 
            mysql_query($sql) or die( mysql_error() );
        }
        echo "logged in";
    } else {
        echo "Wrong Username or Password";
    }
}
?>
The database (I'm trying with):

Code: Select all

-- 
-- Table structure for table `users`
-- 

CREATE TABLE `users` (
  `user_id` int(11) NOT NULL auto_increment,
  `active` varchar(10) NOT NULL default '',
  `username` varchar(50) NOT NULL default '',
  `password` varchar(40) NOT NULL default '',
  `ebay_username` varchar(50) NOT NULL default '',
  `ebay_email` varchar(100) NOT NULL default '',
  `ebay_status` varchar(20) NOT NULL default '',
  `paypal_email` varchar(100) NOT NULL default '',
  `paypal_status` varchar(15) NOT NULL default '',
  `num_downloads` int(5) NOT NULL default '0',
  `num_logins` int(5) NOT NULL default '0',
  `num_ips` int(5) NOT NULL default '0',
  `num_purchases` int(5) NOT NULL default '0',
  `first_name` varchar(30) NOT NULL default '',
  `last_name` varchar(50) NOT NULL default '',
  `address_street` varchar(50) NOT NULL default '',
  `address_city` varchar(30) NOT NULL default '',
  `address_state` varchar(30) NOT NULL default '',
  `address_zip` varchar(20) NOT NULL default '',
  `address_country` varchar(30) NOT NULL default '',
  `address_status` varchar(11) NOT NULL default '',
  `latest_login` datetime NOT NULL default '0000-00-00 00:00:00',
  `latest_updated` datetime NOT NULL default '0000-00-00 00:00:00',
  `created` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

-- 
-- Dumping data for table `users`
-- 

INSERT INTO `users` (`user_id`, `active`, `username`, `password`, `ebay_username`, `ebay_email`, `ebay_status`, `paypal_email`, `paypal_status`, `num_downloads`, `num_logins`, `num_ips`, `num_purchases`, `first_name`, `last_name`, `address_street`, `address_city`, `address_state`, `address_zip`, `address_country`, `address_status`, `latest_login`, `latest_updated`, `created`) VALUES 
(1, '', 'test', 'test2', '', '', '', '', '', 0, 1, 1, 0, '', '', '', '', '', '', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '0000-00-00 00:00:00');
Thanks in advance,
/Oskar

Posted: Fri Jan 12, 2007 12:34 pm
by volka
There are six calls to mysql_query in that script. Which one caused the error?

Posted: Fri Jan 12, 2007 12:38 pm
by Begby
Every query with CURRENT_DATE, NOW() has one extra field in the values part. Thats your problem.

Posted: Fri Jan 12, 2007 12:54 pm
by oskare100
Hello,
OK, thanks, that solved the problem..
Now I've another one;
"(CURRENT_DATE,'num_logins+1') where user_id = '$vuserid'"; "

gives me this error;
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 '(latest_login, num_logins) VALUES (CURRENT_DATE,'num_logins+1') where user_id = ' at line 1

What I want to do is add 1 to the current number of logins from the database.

Thanks,
/Oskar

Posted: Fri Jan 12, 2007 4:51 pm
by volka

Code: Select all

INSERT tablename (fieldA,fieldB) VALUES (123,456)
but

Code: Select all

UPDATE tablename SET fieldA=123, fieldB=456
see http://dev.mysql.com/doc/refman/5.0/en/select.html
and http://dev.mysql.com/doc/refman/5.0/en/update.html