My query that used to work, now does not go to bd at all

Asked

Viewed 42 times

0

Out of the blue my querys are no longer accepted by my BD. That’s both by the site in php and direct with an INSERT in phpmyadmin. See if you can shed a light:

CREATE TABLE `despesas` (
  `id` int(11) NOT NULL,
  `desc` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `venc` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `valor` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `recibo` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

And this is my query in php:

$query = "INSERT INTO despesas (desc, venc, valor, recibo) values('$desc', '$venc', '$val', '" . $rec . "')";
        $salva = mysqli_query(BDConexao(), $query);

If I try to insert direct into phpmyadmin also will not:

INSERT INTO despesas (desc, venc, valor, recibo) values('AWS Amazon', '05/02/2017', '25,00', 'Chrysanthemum.jpg')

So I ask you, would something be wrong with my database?

  • The desc is a reserved word from Mysql, already tried using`desc`, between ``.

  • Check this out: http://answall.com/questions/109125/mysql-palavras-chaves-reservadas

  • Thank you very much, it worked.

  • 3

    Possible duplicate of Mysql reserved keywords

2 answers

0


Well come on,

there are two problems in your query:

  1. The id field: Your table is set to prevent records with this null field: id int(11) NOT NULL, and inside the Insert you are not informing this field, if you wanted this field to be generated automatically you have to configure it with AUTO INCREMENT.
  2. The word DESC: As our friend @Inkeliz commented, it is a reserved Mysql word, for your query to work you will need to put this word between "`", or, change the field name to Description.

I hope I’ve helped.

  • Thank you very much Roberto.

0

Friend if you set that field id cannot be null must inform a value for it as below:

$query = "INSERT INTO despesas ('id',descricao, vencimento, valor, recibo) values(1,'$desc', '$venc', '$val', '" . $rec . "')";
        $salva = mysqli_query(BDConexao(), $query);

If you want it to be incremented sequentially you must create the table and the query in this way:

CREATE TABLE `despesas` (
  `id` int(11) NOT NULL AUTO_INCREMENT,//<-- Veja o AUTO_INCREMENT aqui
  `descricao` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `vencimento` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `valor` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `recibo` varchar(200) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

$query = "INSERT INTO despesas (descricao, vencimento, valor, recibo) values('$desc', '$venc', '$val', '" . $rec . "')";
            $salva = mysqli_query(BDConexao(), $query);

See that I also changed the word desc which is a reserved word from Mysql. With this I believe everything will work well.

  • Thank you Rogers.

Browser other questions tagged

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