0

I`m making a php file to generate users and inserting them in a database. The first thing I made is to obtain the latest one and save it in a variable, then, I use a function to obtain the integer part of the string and increment that value by one, but that is the part that I think is not working.

This is the code:

<?php

$con = new mysqli('localhost', 'root', '', 'prueba');

function create_user($var){
    $prefix = "U";
    for($i = 0; $i < 5- strlen((String)$var); $i++) {
         $prefix .= '0';
    }
    $var = mb_substr($var, 1);

    if(is_numeric($var)) {
        $int = $var++;
        $var = $prefix . $int;
    }
    return $var;
}

$execute = mysqli_query($con, "select id_user from usuarios
    WHERE id_user=(SELECT MAX(id_user) FROM usuarios)");

$row = mysqli_fetch_array($execute);
print_r($row['id_user']);
$var1 = $row['id_user'];

$userid=create_user($var1);

mysqli_query($con, "insert into usuarios (password, descripcion, id_user) 
    values ('A12345a', 'hgfhdgfh', ' $userid' )");

echo "hecho";
?>

Any help, please?

6
  • Why are you mix and matching mb_* and non-mb_* functions...? Commented Jul 9, 2013 at 9:22
  • Is the output of your create_user() function correct? Commented Jul 9, 2013 at 9:25
  • Please give an example of the string before it is passed to create_user() and after. Commented Jul 9, 2013 at 9:30
  • This is the string returned by the select U0001 Commented Jul 9, 2013 at 9:41
  • Ok but what is passed INTO the function? An actual value In and out would be useful. Commented Jul 9, 2013 at 10:38

1 Answer 1

1

Change this line:

$int = $var++;

to:

$int = ++$var;

It's assigning the value of $var before incrementing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.