user10479159
user10479159

Reputation:

HTML form input fields add null values to MySQL database

I have the below html form where a user reserves a room. I am trying to get the data entered by the user in the jsp code as shown but all the values in the database are added as null except for the "user_email" field which is taken as an attribute dispatched from a servlet. Is there any problem with my code? If so, how can I get the values from the input fields of this form and submit them to the jsp code below.

<%@ 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>

//Insert function

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();
                    }
                }
            }

        }

Upvotes: 0

Views: 788

Answers (2)

Saket Yadav
Saket Yadav

Reputation: 1017

# 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.#

Upvotes: 1

Ravinder Reddy
Ravinder Reddy

Reputation: 24002

It seems you are reading parameters on the same jsp page where input form is defined.
It is not wrong but, before reading parameters, you are not checking if the form is submitted or not.

Simple solution is:

check for existence of specific parameter in the request,

  • if it is present then read all other parameters.

    • then perform insert operation.
  • if it is not present then don't read any other parameter

    • and don't perform insert operation.

Example:

<%  
    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
    }
;
%>

Upvotes: 0

Related Questions