Insert Query: Column count doesn't match value count at row 1 [nextjs]

213 views Asked by At

I've a form on my page where I want to be able to submit data into my database/table, but get the error. The amount of given values do match the given columns in the code, so now I don't know exactly where to look for the error.

  • My table has 4 columns (the 'id' should auto-increment).
  • The input gets logged in the console
  • I run it on localhost
  • I'm using nextjs

My code:

import { query } from "../../lib/db";

export default async function handler(req, res) {

    // Get data submitted in request's body.
    const body = req.body;
    const streamer = req.body.streamer;
    const clip = req.body.clip; 
    const category = req.body.category;

    console.log('- - - - - - -');
    console.log('body: ', body);
    console.log('- - - - - - -');
    console.log('streamer: ', streamer);
    console.log('clip: ', clip);
    console.log('category: ', category);
    console.log('- - - - - - -');

    if (req.method === "POST") {

      try { 
      const insertSQL = await query({
        query: "INSERT INTO submissions2023 (streamer, clip, category) VALUES (?)",
        values: [streamer, clip, category]
      });

      res.status(200).json({ data: 'Success :' + streamer });
    } catch (error) {
      res.status(500).json({ data: error.message });
  }

    }

}

Console: Values get logged in console

DB-Table: Table structure in my DB with 4 columns

I changed the way of coding the query and values but this resulted in other errors.

1

There are 1 answers

0
dazznlos On

I found my mistake and made following changes:

try { 
  const insertSQL = await query({
   query: 'INSERT INTO submissions2023 (streamer, clip, category) VALUES (?, ?, ?)',
   values: [streamer, clip, category],
});