Mysql从html中获取数据选择复选框

时间:2016-06-23 04:57:33

标签: php html mysql

我想为复选框获取多个返回值。但问题是以下代码复选框只能回显单个值。即使我选择了多个复选框,我只能获得单个值。如何解决

$q2 = mysqli_query($conn,"SELECT product_img_url FROM temp_img");

while ($row = mysqli_fetch_array($q2)) {
    $product_img_url = $row['product_img_url'];
    $target_dir = "../assets/img/temp_img/";
    $img_link = $target_dir.$product_img_url;

    echo '<br>Add it<br><input type="checkbox" name="pic_tobe_add" value="'.$img_link.'"><br><img src="'.$img_link.'" class="img-rounded" alt="Uploaded image" width="152" height="118"><br><br>';
}
if(isset($_POST['submit_p'])) {
   if (empty($_POST['pic_tobe_add'])) {
      echo "Error: select a pic";
  }else{echo $pic_tobe_add = $_POST['pic_tobe_add'];}
}
echo '<br><input type="submit" name="submit_p" value="Add this product"><form>';

2 个答案:

答案 0 :(得分:1)

使用org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'configbean' available as request attribute org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:472) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:790) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) root cause java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'configbean' available as request attribute org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188) org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130) org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120) org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90) org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84) org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80) org.apache.jsp.Firstpage1_jsp._jspx_meth_form_005flabel_005f0(Firstpage1_jsp.java:216) org.apache.jsp.Firstpage1_jsp._jspx_meth_form_005fform_005f0(Firstpage1_jsp.java:152) org.apache.jsp.Firstpage1_jsp._jspService(Firstpage1_jsp.java:105) org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) javax.servlet.http.HttpServlet.service(HttpServlet.java:790) org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439) org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395) org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339) javax.servlet.http.HttpServlet.service(HttpServlet.java:790) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

<input type="checkbox" name="pic_tobe_add[]" value="'.$img_link.'">

答案 1 :(得分:1)

实际上每个name的{​​{1}}都是相同的,这就是为什么你只得到一个最后一个值的原因。

check-box更改为name="pic_tobe_add"(将其设为数组类型,以便获得所有值)

在下一页上,使用name="pic_tobe_add[]"

打印出POST值来确认