Mister Adrian
Mister Adrian

Reputation: 49

Inserting a variable into MySQL with Go

I have these 2 variables here

name := request.FormValue("username")
pass := request.FormValue("password")

I want to insert those 2 variables into my database

db.Query("INSERT INTO `godb` (`Username`, `Password`) VALUES (   )")

I tried (name,pass) ('name','pass') ($name, $pass) , none of them work.

Hope the question is not stupid, but I've been looking for solutions online but I did't understand them. Thanks !

Upvotes: 1

Views: 2028

Answers (2)

Kazbek Borash
Kazbek Borash

Reputation: 1

query should be in this format db.Query("INSERT INTO table ($1, $2) VALUES (column1, column2)", value1, value2) in your case something like that db.Query("INSERT INTO godb ($1, $2) VALUES (username, password)", name, pass)

Upvotes: -1

Álvaro González
Álvaro González

Reputation: 146460

From Using Prepared Statements

Parameter Placeholder Syntax

The syntax for placeholder parameters in prepared statements is database-specific. For example, comparing MySQL, PostgreSQL, and Oracle:

MySQL               PostgreSQL            Oracle
=====               ==========            ======
WHERE col = ?       WHERE col = $1        WHERE col = :col
VALUES(?, ?, ?)     VALUES($1, $2, $3)    VALUES(:val1, :val2, :val3)

You tried PostgreSQL syntax but you use MySQL.

Upvotes: 4

Related Questions