Let's see if I explain myself well. I am doing a class project in which I want to make each user have a unique alphanumeric autoincrementing ID done in code. For example:
ES0000001
And that this is increased every time a user registers, is this possible? I have tried doing $number = 0000001; and $letter = ES; and then join it in $ID = $letter + $number;
But I get the error Warning: A non-numeric value encountered in D:\XAMPP\xampp\htdocs\DawProject\register.php
Code
<?php session_start();
if(isset($_SESSION['usuario'])) {
header('location: index.php');
}
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$email = $_POST['email'];
$usuario = $_POST['usuario'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$letra = "ES";
$numero = 000001;
$id = ($letra + $numero);
$password = hash('sha512', $password);
$password2 = hash('sha512', $password2);
$error = '';
if (empty($email) or empty($usuario) or empty($password) or empty($password2)){
$error .= '<i>Favor de rellenar todos los campos</i>';
}else{
try{
$conexion = new PDO('mysql:host=localhost;dbname=service_ticketing', 'root', '');
}catch(PDOException $prueba_error){
echo "Error: " . $prueba_error->getMessage();
}
$statement = $conexion->prepare('SELECT * FROM login WHERE usuarios = :usuario LIMIT 1');
$statement->execute(array(':usuario' => $usuario));
$resultado = $statement->fetch();
if ($resultado != false){
$error .= '<i>Este usuario ya existe</i>';
}
if ($password != $password2){
$error .= '<i> Las contraseñas no coinciden</i>';
}
}
if ($error == ''){
$numero++;
$statement = $conexion->prepare('INSERT INTO login (id, email, usuario, password) VALUES (:id, :email, :usuario, :password)');
$statement->execute(array(
'id'=>$id,
':email' => $email,
':usuario' => $usuario,
':password' => $password
));
$error .= '<i style="color: green;">Usuario registrado exitosamente</i>';
}
}
require 'frontend/register-vista.php';
?>
In PHP the operator to concatenate strings is '.', not '+' By putting more you are trying to add a number. to a string, and that's why you get the error.
I recommend you use a stored procedure, for the autoincremented code, it would be the following:
To call the procedure from php: