Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [42000]'

0

I need to generate a schedule of a certain period, but it is giving this error.

  

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; ('19: 00: 00 ',' 31 ',' 12 '), and you can use the following syntax: '2016') 'at line 1

$dataInicio = '2016-01-01';
                $intervalo = 60;

                do {
                    list( $ano, $mes, $dia ) = explode('-', $dataInicio);
                    $inicio = '07:00:00';
                    $final = '19:00:00';
                    do {
                        list($hora, $minuto, $segundo ) = explode(':', $inicio);
                        $sql = "INSERT INTO agenda (hora_agenda,data_agenda(day),data_agenda(month),data_agenda(year)) VALUES('$inicio','$dia','$mes','$ano')";
                        $inicio = date("H:i:s", mktime($hora, $minuto + $intervalo, $segundo, $mes, $dia, $ano));
                    } while ($inicio <= $final);
                    $dataInicio = date('Y-m-d', mktime(0, 0, 0, $mes, $dia + 1, $ano));
                } while (date('Y') == date('Y', strtotime($dataInicio)));

                $dados = connection::exec($sql);
    
asked by anonymous 04.03.2016 / 13:08

1 answer

1

day() , month() and year() are functions to extract specific pieces of a date and your call in the insert is 'inverted' instead of data_agenda(day) the right would be day(data_agenda) , but you record 3 values different in the same field

Minimum enough to work:

INSERT INTO agenda (hora_agenda,data_agenda, data_agenda,data_agenda) 
            VALUES('$inicio','$dia','$mes','$ano')

I believe your intention is (write 3 values in 3 different fields):

INSERT INTO agenda (hora_agenda, dia_agenda, mes_agenda, ano_agenda) 
            VALUES('$inicio','$dia','$mes','$ano')

It is important to use prepared statements to avoid sql injections and to simplify the string somewhat by removing the single quotes in the values.

Recommended reading:

Using PDO is the safest way to connect to a DB with PHP?

    
04.03.2016 / 13:22