Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pq: invalid input syntax for type integer #1157

Open
Ghada-Emad1 opened this issue Apr 7, 2024 · 2 comments
Open

pq: invalid input syntax for type integer #1157

Ghada-Emad1 opened this issue Apr 7, 2024 · 2 comments

Comments

@Ghada-Emad1
Copy link

func (m *SnippetModel) Insert(title string, content string, expires int) (int, error) {
	var pk int
	err := m.DB.QueryRow(`INSERT INTO snippets VALUES($1,$2,$3) RETURNING id`, title, content, expires).Scan(&pk)
	if err != nil {
		return 0, err
	}

	return pk, nil
}

i want to return last index has been inserted into db

func (app *Application) snippetCreate(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodPost {
		w.Header().Set("Allow", "Post")
		app.ClientError(w, http.StatusMethodNotAllowed)
		return
	}

	title := "O snail"
	content := "O snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n– Kobayashi Issa"
	expires := 7
	
	id, err := app.snippets.Insert(title, content, expires)
	if err != nil {
		app.ServeError(w, err)
		return
	}

	// Redirect the user to the relevant page for the snippet.
	http.Redirect(w, r, fmt.Sprintf("/snippet/view?id=%d", id), http.StatusSeeOther)
}

but it always gives me that error

@iwanofski
Copy link

iwanofski commented Apr 8, 2024

Your INSERT query needs columns. Try updating 2nd line in Insert func to:

err := m.DB.QueryRow(`INSERT INTO snippets (title, content, expires) VALUES ($1,$2,$3) RETURNING id`, title, content, expires).Scan(&pk)

@Ghada-Emad1
Copy link
Author

thanks for your response ,i finally solved that dummy error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants