0

I make a register code email, user, pass and when I click in "zr" insert this info in database, but I'm getting following error:

MySQL error: Column count doesn’t match value count at row 1

Code:

include ('config.php');
include('login_css.php');
$error = "";
if (isset($_POST['zr'])){
$date = date("m d Y");
$user_name = strip_tags($_POST['user']);
$user_pass = strip_tags($_POST['pass']);
$user_email = strip_tags($_POST['email']);
$empty = strip_tags($_POST['none']);
$empty.= strip_tags($_POST['none']);
$empty.= strip_tags($_POST['none']);
$day = strip_tags($_POST['day']);
$month = strip_tags($_POST['month']);
$year = strip_tags($_POST['year']);
$dob = "$day/$month/$year";

 if ($user_name == "") {
  $error = "Firstname cannot be left empty.";
    echo $error;
 }
 else if ($user_pass == "") {
  $error = "Lastname cannot be left empty.";
    echo $error;
 }
 else if ($user_email == "") {
  $error = "Email cannot be left empty.";
    echo $error;
 }

 //Check the username doesn't already exist
 $check_username = mysql_query("SELECT yser FROM users WHERE username='$user_name'");
 $numrows_username = mysql_num_rows($check_username);
 if ($numrows_username != 0) {
  $error = 'That username has already been registered.';
  echo $error;
 }
 else
 {
  $check_email = mysql_query("SELECT email FROM users WHERE email='$user_email'");
 $numrows_email = mysql_num_rows($check_email);
 if ($numrows_email != 0) {
  $error = 'That email has already been registered.';
  echo $error;
 }
 else
 {
 //Register the user
 $register = mysql_query("INSERT INTO users(user,pass,email) VALUES('','$user_name','$user_pass','$user_email','$date')") or die(mysql_error());
 die('Registered successfully!');
 }
 }
 }

?>

<!-- Form Mixin-->
<!-- Input Mixin-->
<!-- Button Mixin-->
<!-- Pen Title-->
<div class="pen-title">
<title>WebooHub - Join</title>
  <h1>WebooHub - Join</h1>
</div>
<!-- Form Module-->
<div class="module form-module">
  <div class="toggle"><i class="fa fa-times fa-pencil"></i>
  </div>
  <div class="form">
    <h2>Create Your Account</h2>
    <form action="u_register" method="POST">



      <button>Join Now</button>
    </form>
  </div>
  <div class="cta"><a href="login.php">Login?</a></div>
</div><strong></strong>
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
  • 5
    @FakhruddinUjjainwala I noticed lately you post an answer and a comment. The OP will see your answer, there's no need to comment. – Funk Forty Niner Mar 01 '16 at 15:23
  • same error : @FakhruddinUjjainwala Ujjainwala Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\Weboo\WeboHub\app\join\u_register.php on line 33 Column count doesn't match value count at row 1 – Meral Ahmed Mar 01 '16 at 15:25
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Mar 01 '16 at 15:26
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Mar 01 '16 at 15:26
  • Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). – Jay Blanchard Mar 01 '16 at 15:26
  • @Fred-ii- Sorry I wont repeat that. It was just out of excitement. – Fakhruddin Ujjainwala Mar 01 '16 at 15:26
  • There are quite a lot issuses ranging from plainly wrong to suboptimal. E.g. $dob<->$date, what is this $empty variable, using _num_rows instead of a COUNT() query, counting the records in the first place instead of UNIQUE contraints, what is "yser", missing error handling for the mysql queries, sql injections ....and probably more that I've missed. Don't give up, but .... it's a long, long road ;-) – VolkerK Mar 01 '16 at 15:33
  • This whole script is back to front :-( – Strawberry Mar 01 '16 at 16:02

2 Answers2

4

The error message is telling you exactly what's wrong. Look at your INSERT statement:

INSERT INTO users(user,pass,email) VALUES('','$user_name','$user_pass','$user_email','$date')

You specify 3 columns, but provide 5 values.

Either provide only the 3 values you want to insert:

INSERT INTO users(user,pass,email) VALUES('$user_name','$user_pass','$user_email')

Or specify the 5 columns for which you're inserting values:

INSERT INTO users(someColumn,user,pass,email,someOtherColumn) VALUES('','$user_name','$user_pass','$user_email','$date')

Also, and this is important, your code is wide open to SQL injection. What this means is that you're blindly executing any code that your users send you in your database queries. Please take a look at this, as well as this. Use query parameters in prepared statements so that you treat user input as values instead of as code.


Additionally, you are storing user passwords in plain text. This is grossly irresponsible password handling. Please hash user passwords correctly. User passwords should be obscured behind a 1-way hash and should never be retrievable, not even by you as the system owner.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
0

Check out columns vs values:

You have 3 columns and 5 values. Those needs to be equal.

$register = mysql_query("INSERT INTO users(user,pass,email) VALUES('','$user_name','$user_pass','$user_email','$date')") or die(mysql_error());
 die('Registered successfully!');

Also strongly recommend using mysqli

Fakhruddin Ujjainwala
  • 2,493
  • 17
  • 26
  • Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\Weboo\WeboHub\app\join\u_register.php on line 33 Column count doesn't match value count at row 1 – Meral Ahmed Mar 01 '16 at 15:25
  • @MeralAhmed Thats a `select query` error. `Column count doesn't match value count at row 1 ` the above answer will resolve this. – Fakhruddin Ujjainwala Mar 01 '16 at 15:28