Display next table id in a form input

Asked

Viewed 78 times

0

I need to display the next id number in an input, in case the form would be for a new sale, then I need it to appear in an input where it says the current sale number q will be made, in case the next id number of table q is auto incriment

next id search script: .

include_once "configbanco.php";

$sql = "select (max(Id) + 1) from venda";
$resultado = mysqli_query($con,$sql);
$result = mysqli_fetch_row ($resultado);
$id = $result;   

form that received next id:

        <div class="form-row">
        <div class="form-group col-md-7 mr-3">
        </div>
        <label class=" col-form-label mr-1" ></label>
        <div class="col-sm-1 mr-3">
        </div>
        <label for="idvenda" class=" col-form-label mr-3" >Número da Venda</label>
        <div class="col-sm-1">
        <input class="form-control" id="idvenda" name="idvenda" value="<?php echo $id ?>" required  readonly >
        </div>
        </div>
        <hr>

currently displaying "array" as value in the input field

  • this is not a reliable way to be next and then there may be a new data entry in the database and then change and then no longer valid the number displayed. I don’t know if there is any way to inform the next ID to be incremented unless only the user makes use of your system or with table lock.

  • Poise mas n has to be inserted before this form in this table... it’s like in a service order, you have to show the number of the order you are opening, in case the next id of the table to be generated... in case my command select the result in the same database, it shows... I just need it to show the result of this query ...

  • You can create a TOKEN for this, instead of using the TABLE record ID, which is not safe to use the ID as well as can not have control with multiple sessions making use of the system.

2 answers

0

You can create a TOKEN for service order instead of using the ID for this, which is not safe to use the ID as well as can not have control with multiple sessions making use of the system.

I suggest a function for that:

if(!function_exists('numero_unico')){
    function numero_unico( $quantidade=NULL, $prefixo=NULL ){
        $aleatorio = (date('s') + date('i') * 10000);
        $numerounico = time();
        $numerounico1 = ($numerounico.mt_rand(111111,999999));
        $numerounico2 = ($numerounico.mt_rand(111111,999999));
        $numerounico = ($aleatorio.$numerounico.$numerounico1.$numerounico2);
        $numerounico = str_shuffle($numerounico);
        if($quantidade === 0){

        } else{
            $numerounico = substr($numerounico, 0, $quantidade);
        }
        $conta_prefixo = strlen($prefixo);
        if($conta_prefixo >= 1){
            $numerounico = ($prefixo . $numerounico);
        }
        return $numerounico;
    }
}

In the above function there are two possibilities, to generate a unique number with only numerical values as well as to create a value with a prefix:

$token = numero_unico(12); // Guarda na variável valor numérico de 12 dígitos, ex: 102030405060

$token = numero_unico(12,'TKN'); // Guarda na variável valor numérico de 12 dígitos com prefixo TKN, ex: TKN102030405060

Then you make use of it in your input

<input class="form-control" id="idvenda" name="idvenda" value="<?php echo $token ?>" required readonly />
  • Okay, thanks I’ll check

-2

Checks the $id variable with a var_dump($id).

I believe the $result variable is passing an array, so you can use $id = $result[0];

  • I will check this, Poise, and my query is working, because in the database this showing result if I query that way, I would only need to show the result in the input... complicated and seems so simple kkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.