表中每行的复选框以获取值并插入数据库中

时间:2015-11-13 08:34:17

标签: php checkbox pdo

我正在使用我的页面的发送邮件功能。为了能够获取表中的值,我想在每行中添加一个可以勾选超过1行的复选框。然后,一旦按钮提交,它将被插入数据库中。我不知道我的代码有什么问题。请帮忙。

这是复选框:

 echo "<td><input type='checkbox' name='checkb[".$row['ID']."]'></td>";

以下是代码:

 function InsertEmailMessage() {

 foreach ($_POST['checkb'] as $id => $val) {

 if($val=='checked'){
 $sql6 = "SELECT * FROM invalid_invoice WHERE ID='".$id."'";
 $conn = dbConnect();
 $stmt6 = $conn->prepare($sql6); 
 $stmt6->execute();
 $data = $stmt6->fetchAll(PDO::FETCH_ASSOC);     
 $conn=null;
 }

 foreach ($data as $row6) {

 $invnumb=$row6['Invoice_Number'];
 $partnumb=$row6['Part_Number'];
 $issue=$row6['Issues'];
 $pic=$row6['PIC_Comments'];
 $emailadd= $row6['PersoninCharge'];
 $issuetype=$row6['Issue_Type'];
 $createdate=$row6['Creation_Date'];
 $site=$row6['Site'];
 $vendor=$row6['Vendor_Name'];
 $invdate=$row6['Invoice_Date'];
 $po=$row6['PO'];
 $rr=$row6['RR']; 
 $currency=$row6['Currency'];
 $invamount=$row6['Invoice_Amount'];
 $stat=$row6['Status'];


 if($row6['Status']=="Open") {

 $message = "<html><b>Issue Type: {$issuetype} </b><br><br>";       
 $message .= "<b>Creation Date: {$createdate} </b><br><br>";
 $message .= "<b>Site: {$site} </b><br><br>";
 $message .= "<b>Vendor Name: {$vendor} </b><br><br>";
 $message .= "<b>Invoice Date: {$invdate} </b><br><br>";
 $message .= "<b>Invoice Number: {$invnumb} </b><br><br>";
 $message .= "<b>Part Number:</b><br>{$partnumb}<br><br>";
 $message .= "<b>PO: {$po} </b><br><br>";
 $message .= "<b>RR: {$rr} </b><br><br>";
 $message .= "<b>Currency: {$currency} </b><br><br>";
 $message .= "<b>Invoice Amount: {$invamount} </b><br><br>";
 $message .= "<b>Issues:</b><br>{$issue}<br>";
 $message .= "<b>Status: {$stat} </b><br><br>";  
 $message .= "<b>{$pic}<b><br>";  
 $message .= "</html>";


 if(!empty($emailadd)) {
 dbInsertEmailMessage($emailadd, "Invoice Number: {$invnumb} - {$issue}.", $message);
 echo "<script language='javascript'>alert('Email sent to {$emailadd}')</script>";
 }
 }
 }

 }
 }
 }

 function dbInsertEmailMessage($send_to, $subject, $message) {

  $sql7 = "INSERT INTO email_queue (send_to, subject, message) VALUES (:send_to, :subject, :message)";  
  $conn = dbConnect();
  $stmt7 = $conn->prepare($sql7); 
  $stmt7->bindParam(':send_to', $send_to);
  $stmt7->bindParam(':subject', $subject);
  $stmt7->bindParam(':message', $message);
  $stmt7->execute();
  $conn=null; 
  }  

2 个答案:

答案 0 :(得分:0)

这一行似乎错了:

<td><input type='checkbox' name='checkb[".$row['ID']."]'></td>

如果您使用的是PHP,请回显示例:

echo '<td><input type="checkbox" name="checkb[\'' . $row['ID'] . '\']"></td>';

如果你是HTML:

<td><input type='checkbox' name='checkb["<?php echo $row['ID'] ?>"]'></td>

然后在你的发送功能中你可以尝试一些调试:

var_dump( $_POST );

答案 1 :(得分:0)

这种宣告方式是错误的。

echo "<td><input type='checkbox' name='checkb[".$row['ID']."]'></td>";

将此更改为

echo "<td><input type='checkbox' name='checkb[]' value='".$row['ID']."'></td>";

获取选中复选框的方式是

$checkb=$_POST['checkb'];

$TotalCheckbox=$sizeof($checkb);
for($i=0;$i<$TotalCheckbox;$i++)
{
    echo $Selcheckb=$checkb[$i];
}

现在,用$Selcheckb ..

做任何你想做的事
.
.
.
// Your code.

应该是这样的。

<?
echo "<td><input type='checkbox' name='checkb[]' value='".$row['ID']."'></td>";

function InsertEmailMessage() 
{
        $checkb=$_POST['checkb'];
        $TotalCheckbox=$sizeof($checkb);
        for($i=0;$i<$TotalCheckbox;$i++)
        {
             $Selcheckb=$checkb[$i];
             $sql6 = "SELECT * FROM invalid_invoice WHERE ID=$Selcheckb";
             $conn = dbConnect();
             $stmt6 = $conn->prepare($sql6); 
             $stmt6->execute();
             $data = $stmt6->fetchAll(PDO::FETCH_ASSOC);     
             $conn=null;
        }
.
.// Your rest of the code..