基本上,我要做的是在复选框选择中从mysql数据库中检索整个行。此代码适用于单个选择。但是,对于多个选择,它仍然检索单个行。我该怎么循环?
$sql = "Select * from $DB_TBLName WHERE Delivery_no = '{$_REQUEST['check'][0]}'" ;
echo $sql;
$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
if(isset($_POST['submit'])){
if(!empty($_POST['check'])) {
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check'] as $selected) {
echo $selected ;
}
}
}
$sep = "\t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
while($row = mysql_fetch_row($result)){
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++) {
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
$file_ending = "xls";
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");HTML代码
<td class="border-right" align="center"><input type="checkbox" name="check[]" id="check[]" value= "<?php echo $row['Delivery_no']; ?>" /></td>
<td><?php echo $row['Delivery_no'];?></td>
<td><?php echo $row['Invoice_no'];?></td>
<td><?php echo $row['Bill_date'];?></td>
<td><?php echo $row['Bill_to_party'];?></td>
<td><?php echo $row['Quantity'];?></td>
<td><?php echo $row['Brand']; } ?></td>
</tr>
</table>
<br>
<input type= "submit" name="submit" id="submit" value="submit" class="btn btn-primary button-loading">发布于 2017-03-31 09:39:18
您似乎在一开始就构建了SQL,但只使用了检查输入数组的第一个元素。
你需要使用所有的元素
很简单,就像这样:-
if (is_array($_REQUEST['check']) and count($_REQUEST['check']) > 0)
{
$checks = implode(',', array_map('intval', $_REQUEST['check']));
$sql = "Select * from $DB_TBLName WHERE Delivery_no IN ($checks)" ;
echo $sql;
$result = @mysql_query($sql,$Connect) or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());
if(isset($_POST['submit'])){
if(!empty($_POST['check'])) {
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check'] as $selected) {
echo $selected ;
}
}
}
$sep = "\t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
while($row = mysql_fetch_row($result)){
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++) {
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
$file_ending = "xls";
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
}注意-您使用的是旧的过时的mysql_*函数,而不是当前的mysqli_*函数(或PDO等效函数)。我建议你换个地方。这还允许您使用更安全的参数化查询。您当前的查询非常容易受到SQL注入攻击。
https://stackoverflow.com/questions/43136832
复制相似问题