mbrooking Posted December 4, 2012 Share Posted December 4, 2012 Hi i'm creating a generic registration page for a website. I keep getting this error from mysql: "Column count doesn't match value count at row 1" the table is: CREATE TABLE IF NOT EXISTS `login` ( `fullname` varchar(50) COLLATE latin1_general_ci NOT NULL, `email` varchar(50) COLLATE latin1_general_ci NOT NULL, `username` varchar(50) COLLATE latin1_general_ci NOT NULL DEFAULT '', `password` varchar(50) COLLATE latin1_general_ci NOT NULL DEFAULT '' ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci; my form: <form id='register' action='register.php' method="post"> <p>Fullname <input type="text" name="fullname" /> <br /> Email <input type="text" name="email" /> </p> <p>Username <input type="text" name="username" onblur="modregck(this);"/> Password <input type="password" name="password" /> <br /> </p> <p> <input name="submit" type="submit" value="submit" /> </p> </form> The php/mysql is <?php if ( isset( $_REQUEST['submit'] ) ) { $to_insert ="insert into login values ( '"; $to_insert .= $_REQUEST['fullname'] . "',' " . $_REQUEST['email'] . "',' "; $to_insert .= $_REQUEST['username'] . "',' " . $_REQUEST['password'] . "','' )"; echo "<p>" . $to_insert . "</p>" ; if ( mysql_query($to_insert,$handle) ) echo "<p>inserted</p>"; else echo "<p>not inserted because: " . mysql_error() ; } # if ( isset( $_REQUEST['submit'] ) ) else echo "first time"; //include('navigation.php'); # if ($dbhandle == false ) ?> I am aware of the commented out bits of code I'm a complete novice with all this so sorry if i'm slow to understand. And thank you in advance. Matt. Quote Link to comment https://forums.phpfreaks.com/topic/271612-mysql-column-count-doesnt-match-value-count-at-row-1/ Share on other sites More sharing options...
kicken Posted December 4, 2012 Share Posted December 4, 2012 If you look at the insert query you are generating, you will see something like this: insert into login values ( 'foo',' bar',' blah',' meh','' ) As you can see, you're attempting to insert 5 values into a table that only has 4 columns. Remove your extra empty value from the end. Another issue is that your pre-pending a space to the values you are inserting into the email, username, and password columns Quote Link to comment https://forums.phpfreaks.com/topic/271612-mysql-column-count-doesnt-match-value-count-at-row-1/#findComment-1397582 Share on other sites More sharing options...
mbrooking Posted December 4, 2012 Author Share Posted December 4, 2012 Yeah it worked, thanks a bundle, I would have been staring at the script for about 5 hours still completely confused. Quote Link to comment https://forums.phpfreaks.com/topic/271612-mysql-column-count-doesnt-match-value-count-at-row-1/#findComment-1397587 Share on other sites More sharing options...
Pikachu2000 Posted December 4, 2012 Share Posted December 4, 2012 Probably be less confusing without all that obnoxious concatenation. $to_insert ="INSERT INTO login VALUES ( '{$_REQUEST['fullname']}', '{$_REQUEST['email']}', '{$_REQUEST['username']}', '{$_REQUEST['password']}' )"; Just sayin' . . . Quote Link to comment https://forums.phpfreaks.com/topic/271612-mysql-column-count-doesnt-match-value-count-at-row-1/#findComment-1397606 Share on other sites More sharing options...
Christian F. Posted December 5, 2012 Share Posted December 5, 2012 As another note, you really should be validating the input and escaping the output. The former is done with the ctype_* () functions, and possibly some custom RegExps to cover the more specialized cases. While output escaping is done via real_escape_string (), but only when adding the data to the SQL query. Last, but not least, you should be looking at learning how to use MySQLI or PDO instead. Seeing as the MySQL (without the I) extension is old, missing features, and soon to be removed as it's no longer being developed. Quote Link to comment https://forums.phpfreaks.com/topic/271612-mysql-column-count-doesnt-match-value-count-at-row-1/#findComment-1397646 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.