html表单输入字段向mysql数据库添加空值

hwazgwia  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(448)

我有下面的html表单,用户可以在其中预订房间。我正试图获取用户在jsp代码中输入的数据,如图所示,但是数据库中的所有值都被添加为null,除了“user\u email”字段,该字段作为servlet发送的属性。我的代码有问题吗?如果是这样,我如何从这个表单的输入字段中获取值并将它们提交给下面的jsp代码。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" import = "com.user.*"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Reservation</title>
    </head>
    <body>
    <form  action="#">
    <center>

    <h3>Reservation</h3>
    </center>
    <table align="center" border="1">
        <tr>
            <td colspan="4" align="center">Please complete your reservation</td>
        </tr>   

        <tr>
            <td>Arrival Date</td>
            <td><select name="day" required>
                <option value="">--Day--</option>
                <option value="01">1</option>
                <option value="02">2</option>
                <option value="03">3</option>
                <option value="04">4</option>
                <option value="05">5</option>
                <option value="06">6</option>
                <option value="07">7</option>
                <option value="08">8</option>
                <option value="09">9</option>
                <option value="10">10</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
                <option value="14">14</option>
                <option value="15">15</option>
                <option value="16">16</option>
                <option value="17">17</option>
                <option value="18">18</option>
                <option value="19">19</option>
                <option value="20">20</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
                <option value="24">24</option>
                <option value="25">25</option>
                <option value="26">26</option>
                <option value="27">27</option>
                <option value="28">28</option>
                <option value="29">29</option>
                <option value="30">30</option>
                <option value="31">31</option>
            </select>
            <select name="month" required>
                <option value="">--Month--</option>
                <option value="01">Jan</option>
                <option value="02">Feb</option>
                <option value="03">Mar</option>
                <option value="04">Apr</option>
                <option value="05">May</option>
                <option value="06">Jun</option>
                <option value="07">Jul</option>
                <option value="08">Aug</option>
                <option value="09">Sep</option>
                <option value="10">Oct</option>
                <option value="11">Nov</option>
                <option value="12">Dec</option>
            </select>
            <select name="year" required>
                <option value="">--Year--</option>
                <option value="2018">2018</option>
                <option value="2019">2019</option>
                <option value="2020">2020</option>
                <option value="2021">2021</option>
            </select></td>
        </tr>
        <tr>
            <td>Number of rooms</td>
            <td colspan="2"><input type="text" name="roomNo" id="roomNos"  required>
                    </td>

        </tr>
        <tr>
            <td>Number of nights</td>
            <td colspan="2"><input type="text" name="nights" id="nightsNos" required>

    </td>

        </tr>

        <tr>
        <td>Room type:</td>
        <td><select name="rType" required>
          <option value="Single"> Single</option>
          <option value="Double"> Double</option>
          <option value="Triple"> Triple</option>
          <option value="Suite"> Suite</option>
        </select></td>
        </tr>

    </table><br>
    <center><input type="submit" value="Add reservation" name="button" ></center>
    </form>
    <%  

        String d = request.getParameter("day");
        String m = request.getParameter("month");
        String y = request.getParameter("year");
        String r = request.getParameter("roomNo");
        String n = request.getParameter("nights");
        String rt = request.getParameter("rType");
        String em = String.valueOf(request.getAttribute("email"));

        ReservationDb rd = new ReservationDb();
        rd.insert(d, m, y, r, n, rt, em);
    %>

    </body>
    </html>

//插入函数

public void insert(String d, String m, String y, String r, String n, String rt, String em) {
            Connection connection = null;
            PreparedStatement ppStm = null;

            try {
                connection = ConnectionConfiguration.getConnection();
                ppStm = connection.prepareStatement("INSERT INTO reservations (res_day,res_month,res_year,no_rooms,"
                        + "no_nights,room_type,user_email)"
                        + "VALUES (?,?,?,?,?,?,?)");

                int count = 1;
                ppStm.setString(count++, d);
                ppStm.setString(count++, m);
                ppStm.setString(count++, y);
                ppStm.setString(count++, r);
                ppStm.setString(count++, n);
                ppStm.setString(count++, rt);
                ppStm.setString(count++, em);
                ppStm.executeUpdate();

            } catch(Exception e) {
                e.printStackTrace();
            } finally {

                if (ppStm != null){
                    try {
                        ppStm.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                        }
                if (connection != null) {
                        try {
                            connection.close();
                        } catch (SQLException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                    }
                }
            }

        }
68bkxrlz

68bkxrlz1#


# Add this line in jsp page #

<tr><td>Email</td><td><input name="email" required></td></tr>

# Modify this line #

String em = String.valueOf(request.getAttribute("email"));

# Change with this code #

String em = request.getParameter("email");

# Kindly modify this and add condition like above answer.#
ryevplcw

ryevplcw2#

似乎您正在同一个定义输入表单的jsp页面上读取参数。
这没有错,但是在读取参数之前,您没有检查表单是否已提交。
简单的解决方案是:
检查请求中是否存在特定参数,
如果存在,则读取所有其他参数。
然后执行插入操作。
如果不存在,则不读取任何其他参数
不执行插入操作。
例子:

<%  
    String operation = request.getParameter("operation");
    if ( operation != null and operation.equals( "addreservation" ) )
    {
        String d = request.getParameter("day");
        String m = request.getParameter("month");
        String y = request.getParameter("year");
        String r = request.getParameter("roomNo");
        String n = request.getParameter("nights");
        String rt = request.getParameter("rType");
        String em = String.valueOf(request.getAttribute("email"));

        ReservationDb rd = new ReservationDb();
        rd.insert(d, m, y, r, n, rt, em);
    }
    else
    {
        // ignore reading parameters
    }
;
%>

相关问题