How to Save Colors in Database

Asked

Viewed 1,641 times

6

I have a simple form, and a TYPE=color. These colors are passed in hexadecimal, but I cannot save in the bank. It gives the following error:

Warning: pg_query(): Query failed: ERROR: Invalid input syntax for integer: "#000000"

I tried to change in the bank, but it does not accept another entry. Someone has some solution?

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • 1

    Why don’t you make the column VARCHAR?

  • I have tried and I can’t. From error saying that the entry and invalidates.

  • 1

    How did you try to change the type of column in the bank? Yes. Anyway, if you want it is also possible to convert the color to number and write as int even. What language you use on the server?

  • Hello use Postgres. I would like to know yes how I do. Why I try to change the type in the same bank and not the

  • To using php, html and a bit of js

1 answer

7

The structure of its database foresees a relationship between two tables: the colors are recorded in a table (if I understand correctly, the value of the color in hexadecimal), while the clothes data is in another. The clothes table makes a reference to the color table by saving the color code in a column that is a foreign key.

In this case, when inserting a new garment in the bank, what enters the table of garments is not the value of the color, but the code of the color as it appears in the other table (in the column cd_cor). To get the clothing data and its color in a single query, you use a JOIN, as demonstrated in some other questions here on the site (for example, here and here).

What I’m finding a little strange is that it doesn’t make much sense to use a separate color table if your site allows you to choose any color rather than offer a limited number of options. If you had options like "navy blue" or "snow white", it makes sense to use the color table. But if you want to record the haxadecimal code of color, and accept any color among the 16+ million possible in that notation, it would be more worthwhile to have a text column directly in the table of clothing, and eliminate the relationship between the tables.


Original response, written before the questioner posts the structure of the database

I would recommend changing the data type in your table, and saving as text. To do this in Postgres, you use the ALTER TABLE. For example:

ALTER TABLE minhatabela
ALTER COLUMN nomedacoluna TYPE char(7);

However, if you really want to save the data as a number, PHP has the function hexdec that makes the conversion easily:

$preto = hexdec("#000000");    // 0
$branco = hexdec("#FFFFFF");   // 16777215
$azul = hexdec("#0000FF");     // 255
$verde = hexdec("#00FF00");    // 65280
$vermelho = hexdec("#FF0000"); // 16711680

You will probably need to convert back to hexadecimal. It has the function dechex for this, but you will need to put the # and zeros left manually. For example, only the dechex:

$azul = dechex(255); // "FF" e não "#0000FF"

One of the ways to fix it:

"#" . substr("000000" . dechex(255), -6); // "#0000FF"
  • The bank accepts base 10 only as number?

  • Good question, I don’t know.

  • @Indaiararibeiro I passed the information here of the comments for the answer, trying to explain better what I think of the problem (if I understand the situation well).

Browser other questions tagged

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