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)
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()));}
– rray
the value of $i starts at 1?
– durtto
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));
– Guilherme Nascimento
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.
– user54154
Are you creating a table for every day? You find this really necessary?
– Guilherme Nascimento
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.
– user54154
@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.
– Guilherme Nascimento
@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.– Guilherme Nascimento