N
N
Neowaring Stalker2020-11-06 15:39:26
opencart
Neowaring Stalker, 2020-11-06 15:39:26

How to check a database table for the existence of a record?

In the module I enter the name of the skin in the input field and after that I load the database file for import

I write the name of the new skin to the database

move_uploaded_file($this->request->files['import']['tmp_name'], $filename);
            $skin_name = $this->request->post['skin_name'];
            $this->db->query("INSERT INTO  `oc_testtheme_skin` (`skin_name`) VALUES ('$skin_name');");


If you enter the same name 2 times, it will write exactly the same name.

As possible before
move_uploaded_file($this->request->files['import']['tmp_name'], $filename);


check if such a skin name exists in the table, and if so, display a warning that this name already exists ?

Here is a check before move_uploaded_file gives alert "error"
$query = $this->db->query("SELECT oc_testtheme_skin FROM skin_name");
            $existedSkinname = $query->rows;
            if (in_array($this->request->post['skin_name'], $existedSkinname)) {
                echo "exists";
            }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nokimaro, 2020-11-06
@nokimaro

spoiler
SELECT что FROM таблица

SELECT skin_name FROM oc_testtheme_skin

A
Antonio Solo, 2020-11-06
@solotony

as an option, make the field UNIQUE and do the insert
INSERT IGNORE
or
INSERT ..... ON DUPLICATE KEY UPDATE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question