Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[42000]'

Asked

Viewed 1,510 times

0

I need to generate a schedule for a certain period, but it’s making this mistake.

Fatal error: Uncaught Exception 'Pdoexception' with message 'SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near '(day),data_agenda(Month),data_agenda(year)) VALUES('19:00:00','31','12','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);
  • 1

    I don’t understand PHP, but putting the name of the variables in quotes will not end up sending the literal to the bank? Ex.: '$inicio' will send the literal $inicio instead of the variable value.

  • There are some things out of place, day(), month() and year() are functions to extract pieces of a date, which is the reason to save 3 times some value in the same column(data_agenda) ?

  • You need to explain what you want to do, to make the syntax work just remove day, month, year.

1 answer

1

day(), month() and year() are functions to extract specific pieces of a date and your call on the Insert is 'reversed' instead of data_agenda(day) sure would be day(data_agenda), but you save 3 different values 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 its intention is (to 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 simplify the string a little by removing the simple quotes in the values.

Recommended reading:

Using PDO is the safest way to connect to a PHP BD?

  • It worked that way, but only recorded the last record in the database. Echo the query to see how the Insert is right.

  • @Eduehi the exec() what wheel the Insert is after the two whiles maybe should be inside.

Browser other questions tagged

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