【问题标题】:Using URL parameters with mySQL and JSP ERROR将 URL 参数与 mySQL 和 JSP 一起使用 ERROR
【发布时间】:2015-02-28 14:38:53
【问题描述】:

代码连接到 mySQL 数据库并创建一个用数据填充它的表。每行都有一个特定于该行的购买按钮。所以具体的商品代码发送到jsporder.jsp页面。

<sql:query var="result">
select * from inventory;
</sql:query>

<table border="1" width="100%">
<tr>
<th>Code</th>
<th>Author</th>
<th>Description</th>
<th>Other field(Probs price)</th>
<th>Item Stock</th>
<th>Order Stock</th>
<th>Buy option </th>
</tr>

<c:forEach var = "row" items = "${result.rows}">
<tr>
<td><c:out value = "${row.item_code}"/></td>
<td><c:out value = "${row.item_author}"/></td>
<td><c:out value = "${row.item_description}"/></td>
<td><c:out value = "${row.item_price}"/></td>
<td><c:out value = "${row.item__stock_count}"/></td>
<td><c:out value = "${row.item_order_count}"/></td>
<td><a href="jsporder.jsp?item_code=${row.item_code}">Buy!</a></td>
<td><a
</tr>
</c:forEach>
</table>
</div>  

现在我的 Jsp 订单页面我想更改数据库中的数据。

首先使用:

String itemCode = request.getParameter("item_code");

我认为它是从上一页获取 item_code 的?

现在我有需要更改的 mySQL 查询:

<sql:update var="count">
      <%-- update inventory - take one item from stock --%>
      update inventory set item__stock_count = item__stock_count - 1
        where item_code="${itemCode}"; //This is set to the itemCode variable
    </sql:update>

item_code 与 itemCode 不匹配。它似乎没有选择 itemCode 作为任何东西,因此它不会更改数据库中的值/在数据库中添加任何条目。

itemCode 变量似乎对 jsp/mysql 不存在。

新的网络开发人员。谢谢(额外)

        <sql:update var="count">
      <%-- add new customer_order --%>
      insert into customer_order values
        ( '${itemCode}' "2015-02-24 11:42:30", 1, 
          "2015-02-25 11:42:30", 5789 );
    </sql:update>

页面上显示的内容:

insert into customer_order values ( '' "2015-02-24 11:42:30", 1, "2015-02-25 11:42:30", 5789 ); : Column count doesn't match value count at row 1"

【问题讨论】:

  • This code does not seem to work 。你到底得到了什么。正确解释?
  • @singhakash 抱歉,已编辑。
  • @singhakash 100% 做,多次检查数据库。错误可能是它在字符串中提取 item_code(因为它有引号)例如,如果我在 item_code 上单击购买:A001-1111 将 SQL/JSP 将其拾取为“A001-1111”所以它不会匹配数据库上的值?
  • 是的,您必须将'${itemCode}' 放在单引号中
  • @singhakash 仍有错误,在编辑中添加了更多代码

标签: html mysql jsp


【解决方案1】:

将购买链接更改为

<a href="jsporder.jsp?itemCode=${row.item_code}">Buy!</a>

并从 jsporder.jsp 中删除以下内容,因为那时不需要。

String itemCode = request.getParameter("item_code");

此外,您可能需要在此处的 update 查询中使用逗号。

  insert into customer_order values
    ( '${param.itemCode}', "2015-02-24 11:42:30", 1, 
                         ^
      "2015-02-25 11:42:30", 5789 );

验证您的 customer_order 表是否有五列并且具有匹配的数据类型。

【讨论】:

  • 感谢您的回复。该解决方案仍然不起作用,这很烦人,因为我已经尝试修复这个问题 2 小时,而不是打印它只显示“,”的项目代码。我检查了 mysql 脚本,一切都匹配
  • 使用${param.itemCode} 引用该值。如果查询仍然失败,请发布表结构:列名和类型。
  • 确实做到了。最后。非常感谢。
猜你喜欢
  • 2016-08-20
  • 2019-01-25
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 2012-03-28
  • 1970-01-01
  • 2018-06-23
  • 2023-03-24
相关资源
最近更新 更多