skip to Main Content

So I have a table where when executed the table loops through the data and will insert the row of values into the DB. At the moment I am using a basic insert statement (I know about SQL injection is very temp). What I am having trouble with is at the moment I am hard coding the position of the index in the array. As you can imagine this only works for the first row. If I have two+ rows the next row values won’t be the same index. So what I am trying to though is instead of hard coding a million insert statements the table when it loops and logs the values on the UI it should insert it back into the Table at the same exact place no matter how many rows there are in the table. So instead of getInput[3].value it should be getInput[index position of whatever input it came from].value

 var getInput = document.querySelectorAll(".vcheck");
for (var i = 0; i < getInput.length; i++)
  console.log(getInput[i].id, " => Input", i, " = ", getInput[i].value);


DB(INSERT INTO TABLE(GENERIC_NAME, GENERIC_NAME2, etc) VALUES ('" + getInput[1].value + "', '" + getInput[2].value + "', etc))

Goal is it to be something like this

  var getInput = document.querySelectorAll(".vcheck");
for (var i = 0; i < getInput.length; i++)
  console.log(getInput[i].id, " => Input", i, " = ", getInput[i].value);


DB(INSERT INTO TABLE(GENERIC_NAME, GENERIC_NAME2, etc) VALUES ('" + getInput[(position in the table)].value + "', '" + getInput[position in the table].value + "', etc))

enter image description here

2

Answers


  1. DECLARE @i int = 0
    WHILE @i < 300 
        BEGIN
            SET @i = @i + 1
            /* your code goes here */
        END
    
    Login or Signup to reply.
  2. use Array.from to convert the list returned by querySelectorAll and use map method to extract values from each input.

    var getInput = document.querySelectorAll(".vcheck");
    // create an array of the input values
    var values = Array.from(getInput).map(input => input.value);
    // generate the SQL statement using template literals and the index of the input
    var sql = `INSERT INTO TABLE(GENERIC_NAME, GENERIC_NAME2, etc) 
               VALUES ('${values[0]}', '${values[1]}', etc)`;
    // execute
    DB(sql);
    

    Hope this will solve your issue

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search