在数据库中插入带有文本框值的多个复选框值 [英] Insert multiple checkbox value with textbox value in database

查看:90
本文介绍了在数据库中插入带有文本框值的多个复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   <b>
   <li>
    <img height="150" width="200"src="<?php echo $images_dir."/tb_".$row[2]; ?>"/>
     <input type='checkbox' name='chk1[]' value ='<?php echo basename($images_dir."/".$row[2]);?>'/><input type='text' name='textbox[]'id='textbox'/>    </br>

此代码显示一些带有复选框和文本框的图像.我想选择一个或多个图像,并给它们提供文本值并存储在数据库中.这是用于插入的代码

This code shows some images with checkbox and textbox. I want to select one or more image and give them text value and store in database. Here is the code is for inserting

  $checkbox1=$_POST['chk1'];

   $txt=$_POST['textbox'];

   if($_POST["submit"]=="submit") {
   for($i=0;$i<sizeof($checkbox1);$i++) {
   if(!empty($txt)) {
  echo""
  $query="INSERT INTO message(item,quantity)       VALUES('".$checkbox1[$i]."','".$txt[$i]."')";
    mysql_query($query) or die(mysql_error());
  }
  }
 echo "message is send";
 }
 ?>

当我运行此代码时,它将仅插入复选框值,而文本框值未插入数据库中

When i run this code then it inserts only checkbox value but textbox value is not inserted in database

推荐答案

textbox []是一个数组.因此,使用$txt[$i]

textbox[ ] is a array. So Use $txt[$i]

$query="INSERT INTO message(item,quantity)  
 VALUES('".$checkbox1[$i]."','".$txt[$i]."')";
                                      ^

仅包含一个文本框

<b>
   <li>
    <img height="150" width="200"src="<?php echo $images_dir."/tb_".$row[2]; ?>"/>
     <input type='checkbox' name='chk1[]' value ='<?php echo basename($images_dir."/".$row[2]);?>'/><input type='text' name='textbox'id='textbox'/>    </br>


 $checkbox1=$_POST['chk1'];

   $txt=$_POST['textbox'];

   if($_POST["submit"]=="submit") {
   for($i=0;$i<sizeof($checkbox1);$i++) {
   if(!empty($txt)) {
  echo""
  $query="INSERT INTO message(item,quantity)       VALUES('".$checkbox1[$i]."','".$txt."')";
    mysql_query($query) or die(mysql_error());
  }
  }
 echo "message is send";
 }

这篇关于在数据库中插入带有文本框值的多个复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆