Jump to content

Mysql: Column Count Doesn't Match Value Count At Row 1


mbrooking

Recommended Posts

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.