Button to add numeric value, write to Postgresql database and display on screen with PHP

Asked

Viewed 94 times

-2

Next, I am working on a design of a system that has as one of the parameters "series".. I need from the "Save" button it takes the value of the series, save it in postgresql and show the next number on the screen..

Example: Series:'0002', click on the 'Save' button, add in the database, show on the screen the following number:'0003' so that a new registration can be made..

I know it’s simple, but I’ve never worked with php and am doing in the race rs

I appreciate the help!

part of the series:

<div class="col-sm-2">
 <label for="exampleInputEmail1">Série</label>
 <input name="serie" id="idserie" class="form-control" type="text" 
 value="00001" disabled>
</div>

part of saving:

</div>
 <button id="btnslv" type="button" class="btn btn-success btn-block" 
 name="button">SALVAR</button>
</div>

php part:

<?php
 //inicia uma conexão com o banco de dados orcamentos.
 require "orcamento.php";
 require "conexao.php";

if ($conn) {
 function slvserie (){
 $sre_id = $_GET['serie'];
 $SQL = "INSERT INTO dds_cli(sre_id) VALUES ({$sre_id})";
 pg_query($conn, $sql);
 echo "++Registro inserido com sucesso!!";
 pg_close($conn);
}
}
else
{
echo "++ Falha na conexão com o PostgreSQL!!";
}
?>

I’m not using any framework.

  • I recommend editing and putting the code you have in php and HTML (basically the form part), is using some framework ?

2 answers

1


So if I understand your problem you want to indicate an increment for it you can just create another table in your comic book, being the value indicated in this new table simply created and inserted in this when loading this page if the button you want is primed can do this with the function isset() in php

if (isset($_POST['valordoteubotao')
{
    o teu codigo ;
}

You can then recover the value of the serial number of this table and modify it by increment if any condition is met. to keep an order on increment one way and always check the previous value and declare the next entry as the "previous++";

0

Here’s what I’d do.

First, save a session in php:

session_start();
$_SESSION['serie'] = 1;

On the form screen, you can print the Session using sprintf, thus maintaining the desired format (documentation):

echo sprintf('%04s', $_SESSION['serie']);

After saving the data in the database, just increment the Session:

$_SESSION['serie']++;

Another alternative would be to take the total records of the table using "SELECT COUNT(sre_id) total FROM dds_cli" and increment this value into a variable.

I hope it helps.

Browser other questions tagged

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