php新人求助
如图,现在这个表中商品名是从数据库中导出来的。现在,我想实现将每一行的值对应起来,比如第一个复选框 购买 和 苹果 和 后面顾客填入的 订单备注联系起来,以便我将数据导入到数据库中。现在问题是我看了半天书,也不知道该如何联立起来每行数据。希望可以得到解答,万分感谢!
写的代码如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">PHP 表单
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>查询商品</title>
</head>
<form action="createorder.php" method="post" >
<tr valign="top" bgcolor="#FFFFFF">
<td height="81">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="90" align="center" valign="top"> <br>
<table width="650" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#625D59">
<tr align="center" bgcolor="#CC99FF">
<td width="40">购买</td>
<td width="40">商品名</td>
<td width="40">订单备注</td>
</tr>
<?php
include('conn.php');//连接数据库。
mysql_query("use sale_system");
$sql=mysql_query("select commodity from stock");
$info=mysql_fetch_array($sql);
do{
?>
<tr align="left" bgcolor="#FFFFFF">
<td height="20" align="center"><input name=fruit[] type="checkbox" value="$info">购买</td>
<td height="20" align="center"><?php echo $info['commodity']; ?></td>
<td width="80" height="25" align="center" ><input name=remark[] type="textarea" size="50" rows="2" cols="50"></td>
</tr>
<?php
}
while($info=mysql_fetch_array($sql));
?>
<tr align="left" bgcolor="#FFFFFF">
<td width="35">送单时段</td>
<td height="25" align="center"><select name="select">
<option value="中午">中午</option>
<option value="下午"selected>下午</option>
<option value="晚上">晚上</option>
</select></td>
</table></td>
</tr>
</table>
<br></td>
</tr>
</table>
</form>
</body>
</body>
</html>
[解决办法]
每个商品都给予唯一的id值,加上用户的id,作为每行关联的依据。
[解决办法]
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$fruits = array();
foreach ($_POST['fruit'] as $fruit) {
$fruits[$fruit] = $_POST['remark_' . $fruit];
}
var_dump($fruits);
}
?>
<form action="" method="post">
<input type="checkbox" name="fruit[]" value="1"> Apple
<input type="text" name="remark_1"><br>
<input type="checkbox" name="fruit[]" value="2"> Melon
<input type="text" name="remark_2"><br>
<input type="submit" value="Submit">
</form>
[解决办法]
没看懂啥意思?
你是说: 点击checkbox,能够和后面的内容关联起来。 一起保存到数据库中。
首先你的设计就有问题:产品 苹果,西瓜,香蕉 这些都只有中文名字,没有对应的编码吗?
你保存到数据库的只有产品中文名称? 这是不行的。
其次:<td height="20" align="center"><?php echo $info['commodity']; ?></td>
这样写,你不方便获取到 “苹果,西瓜,香蕉”这些中文名称。
应该加上<label>标签
<td height="20" align="center"><label id="<?php echo $info['commodity_id'];?>"><?php echo $info['commodity']; ?></label></td>
这样你就可以通过document.getElementById('commodity_id');来得到标签里面的内容。
[解决办法]
订单表里面存 用户ID 商品ID 备注
这样就知道是谁买了什么商品备注什么了