mark103 Posted January 1, 2011 Share Posted January 1, 2011 Hi guys, I need your help. I am trying to insert the rows in the mysql database as I input the values in the url bar which it would be like this: www.mysite.com/testupdate.php?user=tester&pass=test&user1=tester&[email protected]&ip=myisp However i have got a error which i don't know how to fix it. Error: Column count doesn't match value count at row 1 <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $username = clean($_GET['user']); $password = clean($_GET['pass']); $adduser = clean($_GET['user1']); $email = clean($_GET['email']); $IP = clean($_GET['ip']); if($username == '') { $errmsg_arr[] = 'username is missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'PASSWORD is missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { $sql = "INSERT INTO `members` (`username`,`email`,`IP`) VALUES ('$adduser','$email','$IP')"; if (!mysql_query($sql,$link)) { die('Error: ' . mysql_error()); } echo "The information have been updated."; } ?> Here's the name of the columns i have got in my database: username email IP I have input the correct columns names, so I can't correct the problem I am getting. Please can you help? Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/ Share on other sites More sharing options...
rbrown Posted January 1, 2011 Share Posted January 1, 2011 try this: $sql = "INSERT INTO members (username, email, IP) VALUES ('".$adduser."', '".$email."','".$IP."')"; Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153655 Share on other sites More sharing options...
sooner Posted January 1, 2011 Share Posted January 1, 2011 one thing i notice is you don't need quotes for members...so it can be like this INSERT INTO members..But i don't think this one will rectify the error..this is what i can see.. Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153656 Share on other sites More sharing options...
Pikachu2000 Posted January 1, 2011 Share Posted January 1, 2011 Echo the query string with the error . . . die('<br>Error: ' . mysql_error() . "<br>Query: $sql" ); Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153658 Share on other sites More sharing options...
Pikachu2000 Posted January 1, 2011 Share Posted January 1, 2011 one thing i notice is you don't need quotes for members...so it can be like this INSERT INTO members..But i don't think this one will rectify the error..this is what i can see.. Those aren't quotes, they're backticks, which are fine to use, and in some cases required, around table and field names in MySQL. Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153659 Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2011 Share Posted January 1, 2011 Edit: LOL again. I recommend that you echo $sql as part of your die() error handling so that you can see exactly what the query is. I don't think that is the code and/or the query that is failing. @sooner, those aren't quotes. They are back-ticks (yes it matters what they are.) Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153661 Share on other sites More sharing options...
mark103 Posted January 1, 2011 Author Share Posted January 1, 2011 Still the same, do you have any idea? Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153663 Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 What is the new error? Paste it here with the query string echoed also. Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153685 Share on other sites More sharing options...
mark103 Posted January 2, 2011 Author Share Posted January 2, 2011 What is the new error? Paste it here with the query string echoed also. The same one I has Error: Column count doesn't match value count at row 1 Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153688 Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 Paste the echoed query string also. Echo the query string with the error . . . die('<br>Error: ' . mysql_error() . "<br>Query: $sql" ); Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153693 Share on other sites More sharing options...
mark103 Posted January 2, 2011 Author Share Posted January 2, 2011 Here it's: Error: Column count doesn't match value count at row 1 Query: INSERT INTO member (username, email, IP) VALUES('','','','') And here's the update code: <?php session_start(); define('DB_HOST', 'localhost'); define('DB_USER', 'mydbusername'); define('DB_PASSWORD', 'mydbpassword'); define('DB_DATABASE', 'mydbname'); $errmsg_arr = array(); $errflag = false; $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } function clean($var){ return mysql_real_escape_string(strip_tags($var)); } $username = clean($_GET['user']); $password = clean($_GET['pass']); $adduser = clean($_GET['adduser']); $email = clean($_GET['email']); $IP = clean($_GET['ip']); if($username == '') { $errmsg_arr[] = 'username is missing'; $errflag = true; } if($password == '') { $errmsg_arr[] = 'PASSWORD is missing'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; echo implode('<br />',$errmsg_arr); } else { if(isset($_GET['adduser'])) { $insert[] = 'username = \'' . clean($_GET['adduser']) .'\''; } if(isset($_GET['email'])) { $insert[] = 'email = \'' . clean($_GET['email']) . '\''; } if(isset($_GET['ip'])) { $insert[] = 'ip = \'' . clean($_GET['ip']) . '\''; } $names = implode(',',$insert); $sql="INSERT INTO members (username, email, ip) VALUES('','$_POST[adduser]','$_POST[email]','$_POST[ip]')"; if (!mysql_query($sql,$link)) { die('<br>Error: ' . mysql_error() . "<br>Query: $sql" ); } echo "The information have been updated"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153701 Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 You're trying to insert 4 values into 3 fields. You have an extra '', at the beginning of the VALUES( Also, none of your variables have values, probably because you're trying to use $_POST where it should be $_GET. Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153705 Share on other sites More sharing options...
mark103 Posted January 2, 2011 Author Share Posted January 2, 2011 Thanks for your help. This time I get a different error. Error: Column count doesn't match value count at row 1 Query: INSERT INTO user_banned_list (username, email, IP_domain) VALUES('','') Here's the update code: $sql = "INSERT INTO members (username, email, ip) VALUES('','$_GET[names]')"; Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153709 Share on other sites More sharing options...
revraz Posted January 2, 2011 Share Posted January 2, 2011 You are still trying to enter only 2 values when you list 3 fields. Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153723 Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 Thanks for your help. This time I get a different error. Error: Column count doesn't match value count at row 1 Query: INSERT INTO user_banned_list (username, email, IP_domain) VALUES('','') Here's the update code: $sql = "INSERT INTO members (username, email, ip) VALUES('','$_GET[names]')"; That isn't the code causing that error message. Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153726 Share on other sites More sharing options...
PFMaBiSmAd Posted January 2, 2011 Share Posted January 2, 2011 LOL, none of the code/errors posted together match. mark103, you cannot (successfully) write code by randomly changing things. You must know what you are changing and why you are changing it (you don't even have a names parameter in the URL, so the $_GET[names] you used in the last code you posted doesn't even exist) and you must read what the error messages are telling you. Quote Link to comment https://forums.phpfreaks.com/topic/223159-mysql-error-column-count-doesnt-match-vakue-count-at-row/#findComment-1153864 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.