首页 > 解决方案 > 我在使用 java 在 mysql 中插入值时遇到问题

问题描述

我有一个表单,用户从中选择一个答案,然后我取所选答案的值并在数据库中插入 3 个值,但有两个问题 1)我插入 3 行但我在 mysql 中只找到一个 2) selected_answer 值在 mysql 中始终为空,当我输入一个数字而不是 selected_answer 时,它可以工作!

<form  action ="Thanks.jsp" method ="get">


    <p><input type="radio" name="ans" value= <%=answers_id[0]%>/><%=answers[0]%></p>
    <p><input type="radio" name="ans" value=<%=answers_id[1]%>/><%=answers[1]%></p>
    <p><input type="radio" name="ans" value=<%=answers_id[2]%>/><%=answers[2]%></p>

    <p><input type="submit" value="submit"/>
</form>



<%

        String chosen_answer = request.getParameter("ans");

        //in this loop i'm removing the chosen_answer from the array
        for (int i = 0; i < answers_id.length; i++) {
            if (answers_id[i] == chosen_answer) {
                // shifting elements
                for (int j = i; j < answers_id.length - 1; j++) {
                    answers_id[j] = answers_id[j + 1];
                }
                break;
            }
        }

        // i will make the first aid is the answer that is chosen to know if he answer it right or wronge
        String queryString3 = "INSERT INTO stud_ans VALUES("
                + "'" + ID + "',"
                + "'" + 1 + "',"
                + "'" + 7 + "') , (" + "'" + ID + "',"
                + "'" + 1 + "',"
                + "'" + 7 + "'),(" + "'" + ID + "',"
                + "'" + 1 + "',"
                + "'" + 7 + "')";
        Stmt.executeUpdate(queryString3);
        Con.close();
        RS.close();

    } catch (Exception cnfe) {
        System.err.println("Exception: " + cnfe);
    }


%>

标签: javamysqljsp

解决方案


推荐阅读