Chamarejc
Chamarejc

Reputation: 25

MYSQL insert from select and variables

I am trying to insert values coming from a select and variable :

INSERT INTO routeur (`codeAdherent`, `quantiteArticle`, `dateFin`) VALUES 
(SELECT `codeAdherent` FROM adherents WHERE categorie = 'G', `quantiteArticle` = $a, `dateFin`= $b);

Write it with and without VALUES, with and without IN, with and without brackets but I always get an synthax error.

Where is my mistake?

Upvotes: 1

Views: 2128

Answers (3)

Jignesh M. Khatri
Jignesh M. Khatri

Reputation: 1606

You can try this out :

INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin) VALUES 
(SELECT codeAdherent FROM adherents WHERE categorie = 'G', $a, $b);

Upvotes: 0

kiks73
kiks73

Reputation: 3758

You have to read carefully the INSERT syntax because you have got many errors. This is the right syntax:

INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin) 
SELECT codeAdherent, '$a', '$b'
FROM adherents 
WHERE categorie = 'G' 

PS: To avoid the SQL Injection you should use Prepared Statements

Upvotes: 0

Fahmi
Fahmi

Reputation: 37473

Try below:

INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin) 
SELECT codeAdherent, @a, @b FROM adherents WHERE categorie = 'G'

Upvotes: 1

Related Questions