Generate sequential numbers for draw

Asked

Viewed 440 times

-2

I am creating an online draw system in PHP and Mysql. First I create the draw where I inform the prize and its details, among them the amount of coupons, beginning at a specific number. To put it simply, I’m going to draw a car and sell 800 numbers, which should start at 101 and go up to 900 (101, 102, 103, ...899, 900). In another table, I need to generate these numbers automatically, and in each of them I will have the name of the buyer of this number, phone and email, which will be completed later. How do I generate this sequence and write to the database? The table looks like this:

CREATE TABLE `numeros` (
  `id` int(11) NOT NULL,
  `rifa` varchar(50) NOT NULL,
  `numero` decimal(8,0) NOT NULL,
  `status` varchar(50) NOT NULL,
  `nome` varchar(250) NOT NULL,
  `celular` varchar(50) NOT NULL,
  `email` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Only the number field should be generated at this stage, besides id, of course. I have tried several ways, I could not. I know your help.

  • you want to generate the numbers that won the awards?

  • I think the problem is that you declare all fields as not null.. if you just want to fill out the number, the rest will stay null, no?! if yes, can’t be not null..

  • I don’t think I’ve been understood. I need a script that automatically fills the number and raffle field (which will be informed through an array) only, massively in sequence (101, 102, 03, until the end 899, 900). After that, the person will select the number and fill in her data and start to compete with that number.

  • But then, in question you say that only the field number should be generated and in comment the field number and raffle. Number is the sequence from 101 to 900 and raffle as it would be?

1 answer

0

Procedures are routines defined in the database, identified by a name by which they can be invoked. Such a procedure can execute a series of instructions, receive parameters and return values.

CREATE TABLE `numeros` (
  `id` int(11) NOT NULL auto_increment primary key,
  `rifa` varchar(50) NOT NULL,
  `numero` decimal(8,0) NOT NULL,
  `status` varchar(50) NOT NULL,
  `nome` varchar(250) NOT NULL,
  `celular` varchar(50) NOT NULL,
  `email` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

delimiter //
create procedure populate (in num int)
begin
declare i int default 101;
while i < num do
insert into numeros (numero) values (i);
set i = i + 1;
end while;
end //
delimiter ;

call populate (901);
  • DELIMITER - Serves to mark where begins and ends the trial, in this case I chose //
  • CREATE PROCEDURE - will create the precedent, note that there are parameters for these procedures. The parameters can be IN, input only, OUT, output only and INOUT, input and output only. The data types of the parameters are the same data types as Dbms ( INT, VARCHAR( ), TEXT, BLOB...)
  • In our case we have the variable num, which will be passed to
  • Between the commands BEGIN and END is that the commands executed by the system will be placed.
  • While... End While statement - executes the commands inside the loop, while the boolean expression is true`.

The formal syntax of the While... End while control structure is:

While(Expressão_Booleana)
   instrução(ções)
End while

In our case while i (which starts at 101) is less than the variable num passed to Procedure (901) makes the following instruction

insert into numeros (numero) values (i);

Browser other questions tagged

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