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

0

Of course my queries are no longer accepted by my DB. This is for both the site in php and direct with an INSERT in phpmyadmin. See if you can give me 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 it will not either:

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

Then I ask you, is something wrong with my database?

    
asked by anonymous 19.02.2017 / 06:21

2 answers

0

Well come on,

There are two problems in your query:

  • 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 want this field to be generated automatically you have to configure it with AUTO INCREMENT.
  • The word DESC : As our friend @Inkeliz commented, it is a reserved MySQL word, so that your query works you will need to put this word between "'", or change the field name to description.
  • I hope I have helped.

        
    19.02.2017 / 06:54
    0

    Friend if you set that the id field can not be null you should enter 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 to be incremented sequentially, you should create the table and query as follows:

    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);
    

    I've also changed the word desc which is a reserved MySQL word. With that I believe everything will work fine.

        
    19.02.2017 / 07:16