Creating tables with PHP

Asked

Viewed 621 times

3

Expensive;

I have a problem and need a little help. I have an html page that contains some radio Buttons. When clicking on a radio button and then on the send button, it redirects to a PHP page that captures the radio value and with this value, enters a for, and inside the for has the mktime function, and with the radio button value adds the months. Below put the code for better understanding:

<?php

  // Dados da conexão com o banco de dados

define('SERVER', 'localhost');
define('DBNAME', 'contas');
define('USER', 'root');
define('PASSWORD', '');


$valor = $_POST['radios'];
$i = 1;

for ($i; $i <= $valor; $i++) {
$result = date('Y-m-d',mktime(0,0,0,date('m')+$i));

  $opcoes = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8');
  $conexao = new PDO("mysql:host=".SERVER."; dbname=".DBNAME, USER, PASSWORD,                `$opcoes);`

  $sql = "CREATE TABLE ".$result." (id int not null   auto_increment,nome_boleto          VARCHAR(70) not null,data_inclusao date not null,vencimento date not    null,valor_boleto VARCHAR(20) not null,mensal VARCHAR(4) not null,descricao    VARCHAR(50) not null,pago VARCHAR(4) not null,data_pagamento date not    null,forma_pagamento VARCHAR(13) not null)";
  $stm = $conexao->prepare($sql);
  $stm->execute();




}

?>

However it does not execute the SQL code, it does not create the tables in the accounts database. If I put an "echo" at the end returning the variable ($result), the return occurs.

Ps: If I put an incorrect command in the $sql variable to force an error, it does not return anything, I believe this block is not running, it is being ignored.


With the help of rray, asking to print the return of stm, I arrived at a syntax error:

Array ( [0] => 42000 [1] => 1064 [2] => You have an error in your SQL syntax;    check the manual that corresponds to your MariaDB server version for the right syntax to use near '2016-10-14 (id int not null auto_increment,nome_boleto VARCHAR(70) not null,data' at line 1 ) Dados inseridos2016-10-14

Mariadb is not accepting a table with this format 2016-10-14. So it does not accept table name with date or numeral format.

Putting the variable with "crase" escaping it, as the orientation of rray , I solved my problem.

Grateful for the personal help!

(living and learning)

  • 1

    What’s the point of creating so many tables? the connection to the database should be before the for since there is no need to create a new connection at each iteration. Maybe do not create tables are already created. Change the execute() for: if(!$stm->execute()){print_r($stm->errorInfo()));}

  • the value of $i starts at 1?

  • Usually in production the user is not allowed to create tables directly (sometimes), I would choose to create a table only and create a column with the value of $result = date('Y-m-d',mktime(0,0,0,date('m')+$i));

  • Then, in html contains an option to create tables according to the months of your choice. If the user clicks on the radio button with a value of 12, it will create 12 tables of the months from the date they requested. If it clicks 12 today, it will create 12 tables from October. The problem that does not generate any error. If I put an incomplete code in the $SQL variable, it should generate a forced error, no more. Just ignore. Finally, yes the $i value starts with one, because the for ends until the $i arrive at the value the user selected on the radio button.

  • Are you creating a table for every day? You find this really necessary?

  • No, it is per month. Each month will have different costs. In the mktime function I have just set the month field. It is a financial "personal" system, where each month I will allocate my costs and with it I plot charts and have a control of everything.

  • 1

    @user54154 being month or day yet it doesn’t make much sense to separate into different tables, it is possible to distinguish with a simple query in the query.

  • @user54154 would just use something like SELECT * FROM MINHA_TABELA WHERE data_inclusao BETWEEN '2016-11-01' AND '2016-11-30';, see the answer I added.

Show 3 more comments

1 answer

4


According to the response of @rray explains the problem of syntax error, however sometimes it is not possible to create tables in the database dynamically, only manually (I said the times), it would make its application susceptible to failures.

Another situation is that I noticed that you already want to create a table for each month of the year, do you think this really necessary? I mean you don’t need to create anything beforehand, the best would be to really pull the only things that exist, I’ll explain further below.

If the idea is to create tables to organize something by date then I recommend using the column data_inclusao table creating a single table something like:

CREATE TABLE MINHA_TABELA
(
    id              INT NOT NULL auto_increment,
    nome_boleto     VARCHAR(70) NOT NULL,
    data_inclusao   DATE NOT NULL,
    vencimento      DATE NOT NULL,
    valor_boleto    VARCHAR(20) NOT NULL,
    mensal          VARCHAR(4) NOT NULL,
    descricao       VARCHAR(50) NOT NULL,
    pago            VARCHAR(4) NOT NULL,
    data_pagamento  DATE NOT NULL,
    forma_pagamento VARCHAR(13) NOT NULL
)

So if you’re going to filter by date I would just use this:

SELECT * FROM MINHA_TABELA WHERE data_inclusao = 'YYYY-MM-DD';

exchange YYYY-MM-DD by the numbers as 1999-11-22

And if it’s today, wear it CURDATE:

SELECT * FROM MINHA_TABELA WHERE data_inclusao = CURDATE();

So you can create simpler query queries, for example:

SELECT * FROM table WHERE data_inclusao BETWEEN '2015-MM-DD' AND CURDATE()

Creating a table for each day seems a bit of an exaggeration, if you are trying to achieve something like performance I recommend at most creating a table per year (maybe, there is a lot of variation).

Consultation per month:

# Consulta mês 11 de 2016
SELECT * FROM MINHA_TABELA WHERE data_inclusao BETWEEN '2016-11-01' AND '2016-11-30';

# Consulta mês 12 de 2016
SELECT * FROM MINHA_TABELA WHERE data_inclusao BETWEEN '2016-12-01' AND '2016-12-31';
  • 2

    Hello William, in fact it makes sense and I will use your explanation to improve this project. Grateful for the time you have disposed to help me.

Browser other questions tagged

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