I'm trying to write a registration page for clients but this error appears in every moment.
Failed to run query: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
I've followed this tutorial: http://untame.net/2013/06/how-to-build-a-functional-login-form-with-php-twitter-bootstrap/ The DB is this: DB
The code is this:
<?php
require("config.php");
if(empty($_SESSION['user']))
{
header("Location: index.php");
die("Redirecting to index.php");
}
require("config.php");
if(!empty($_POST))
{
// Ensure that the user fills out fields
if(empty($_POST['username']))
{ die("Please enter a username."); }
if(empty($_POST['password']))
{ die("Please enter a password."); }
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{ die("Invalid E-Mail Address"); }
// Check if the username is already taken
$query = "
SELECT
1
FROM users
WHERE
username = :username
";
$query_params = array( ':username' => $_POST['username'] );
$query_params = array( ':nombre' => $_POST['nombre'] );
$query_params = array( ':empresa' => $_POST['empresa'] );
$query_params = array( ':apellidos' => $_POST['apellidos'] );
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); }
$row = $stmt->fetch();
if($row){ die("This username is already in use"); }
$query = "
SELECT
1
FROM users
WHERE
email = :email
";
$query_params = array(
':email' => $_POST['email']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage());}
$row = $stmt->fetch();
if($row){ die("This email address is already registered"); }
// Add row to database
$query = "
INSERT INTO users (
username,
password,
salt,
email,
empresa,
nombre,
apellidos
) VALUES (
:username,
:password,
:salt,
:email,
:empresa,
:nombre,
:apellidos
)";
/*
*/
// Security measures
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
$password = hash('sha256', $_POST['password'] . $salt);
for($round = 0; $round < 65536; $round++){ $password = hash('sha256', $password . $salt); }
$query_params = array(
':username' => $_POST['username'],
':password' => $password,
':salt' => $salt,
':email' => $_POST['email'],
':empresa' => $_POST['empresa'],
':nombre' => $_POST['nombre'],
':apellidos' => $_POST['apellidos']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); }
header("Location: index.php");
die("Redirecting to index.php");
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootstrap Tutorial</title>
<meta name="description" content="Bootstrap Tab + Fixed Sidebar Tutorial with HTML5 / CSS3 / JavaScript">
<meta name="author" content="Untame.net">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="assets/bootstrap.min.js"></script>
<link href="assets/bootstrap.min.css" rel="stylesheet" media="screen">
<style type="text/css">
body { background: url(assets/bglight.png); }
.hero-unit { background-color: #fff; }
.center { display: block; margin: 0 auto; }
</style>
</head>
<body>
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand">Registro en 4tickets</a>
<div class="nav-collapse">
<ul class="nav pull-right">
<li><a href="index.php">Volver a casa</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container hero-unit">
<h1>Register</h1> <br /><br />
<form action="register.php" method="post">
<label>Username:</label>
<input type="text" name="username" value="" />
<label>Email: <strong style="color:darkred;">*</strong></label>
<input type="text" name="email" value="" />
<label>Password:</label>
<input type="password" name="password" value="" /> <br />
<label>Nombre:</label>
<input type="text" name="nombre" value="" /> <br />
<label>apellidos:</label>
<input type="text" name="apellidos" value="" /> <br />
<label>Empresa:</label>
<input type="text" name="empresa" value="" /> <br />
<p style="color:darkred;">* Poner bien las direcciones de mail</p><br />
<input type="submit" class="btn btn-info" value="Register" />
</form>
</div>
</body>
</html>
I've seen that all the variables are ok and I don't know where is the error.
My config.php
<?php
// These variables define the connection information for your MySQL database
$username = "root";
$password = "root";
$host = "localhost";
$dbname = "4tickets";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try { $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); }
catch(PDOException $ex){ die("Failed to connect to the database: " . $ex->getMessage());}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
header('Content-Type: text/html; charset=utf-8');
session_start();
?>
I've only added 3 things: empresa, nombre and apellidos that weren't in the tutorial and I needed.
Thanks in advance, John.