1

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 !

2
  • 1
    Try placeholders: stackoverflow.com/questions/35781555/…. And don't store a raw password into a database, hash and salt it first. Commented Jun 24, 2020 at 9:56
  • Note that the language has no support for implicit string interpolation of variables accessible in the current scope, as you seem to expect. You need to pass the variables to the function explicitly. i.e. Query("... VALUES (?, ?)", name, pass). Commented Jun 24, 2020 at 10:12

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

This is wrong, not only have you switched the order of columns and values but you are also using the wrong parameter references for mysql.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.