我正在尝试为客户生成账单。当我在表单中添加项目并单击添加按钮时,我希望它创建表格并显示账单。但它不能正常工作。我试图使用sql查询,但它只显示2项,有时空白。当有3个项目插入数据库时。请任何人都能告诉我哪里出错了。
<tr>
<td height="517"><form name="form1" method="post" action="">
<table width="400" border="0" align="center" cellpadding="10" cellspacing="0">
<tr>
<td colspan="2"><div align="center">Add item Details </div></td>
</tr>
<tr>
<td width="117">Item Name</td>
<td width="243"><input name="itemName" type="text" id="itemName">
</td>
</tr>
<tr>
<td>Quantity</td>
<td><input name="qty" type="text" id="qty"></td>
</tr>
<tr>
<td>Price </td>
<td><input name="price" type="text" id="price"></td>
</tr>
<tr>
<td>Vat </td>
<td><input name="vat" type="text" id="vat"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Add"></td>
</tr>
</table>
</form>
<p align="center">Bill Record</p>
<p align="center"> </p>
<table width="70%" border="0" align="center" cellpadding="10" cellspacing="0">
<tr>
<th>Item Name</th>
<th>Quantity</th>
<th>Price</div></th>
<th>Vat</th>
<th>total</th>
</tr>
<% double grandTotal=0;
try
{
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from bill where cname='"+session.getAttribute("cname")+"' order by datetime asc");
while(rs.next())
{
%>
<tr>
<td><%=rs.getString("itemName")%></td>
<td><%=rs.getString("qty")%></td>
<td><%=rs.getString("price")%></td>
<td><%=rs.getString("vat")%></td>
<td><%= rs.getString("total") %></td>
<% grandTotal+=Double.parseDouble( rs.getString("total"));%>
</tr>
<%
} %>
<tr>
<td></td>
<td></td>
<td></td>
<td>grandTotal</td>
<td><%= grandTotal %> </td>
</tr>
<% con.close();
}
catch(Exception e)
{
out.println("Error: "+e);
}
%>
</table>
</tr>
以下是我在数据库中添加值的代码。
<%
if(request.getMethod().equals("POST"))
{
String itemName="";
int qty;
double price,vat,total;
grandTotal=0;
boolean exists=false;
itemName=request.getParameter("itemName");
qty=Integer.parseInt(request.getParameter("qty"));
price=Double.parseDouble(request.getParameter("price"));
vat=Double.parseDouble(request.getParameter("vat"));
total=price+vat;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
try
{
Statement st=con.createStatement();
st.executeUpdate("INSERT into bill values('"+session.getAttribute("cname")+"','" + dateFormat.format(date) + "','"+itemName+"','"+qty+"','"+price+"','"+vat+"','"+total+"','"+grandTotal+"')");
out.println("<script> alert('Item Added Successfully')</script>");
}