PHPmailer通过复选框选择电子邮件从数据库发送电子邮件

问题描述:

我使用phpmailer从数据库发送电子邮件到电子邮件地址,该工作。 但我想通过复选框选择电子邮件地址,这不起作用。 我没有得到erros,但我使用var_dump,问题是代码没有看到数据库中的id。 var_dump是:1string(33)“DELETE FROM memberlist WHERE id = o” 有人可以帮我吗?PHPmailer通过复选框选择电子邮件从数据库发送电子邮件

在此先感谢。

我PHPMailer的代码是:list.php的

<?php 
session_start(); 
include 'connect.php'; 
echo ini_get('display_errors'); 

if (!ini_get('display_errors')) { 
    ini_set('display_errors', '1'); 
} 

echo ini_get('display_errors'); 


//phpmailer 
if(isset($_POST['submit'])) 
{ 


require "phpmailer/class.phpmailer.php"; //include phpmailer class 
//this is the content of the e-mail 
$message=' 
    text: '.$_POST['textfield'].'<br /> 
    '; 

    $mail = new PHPMailer; 

//$mail->SMTPDebug = 3;        // Enable verbose debug output 

$mail->isSMTP();          // Set mailer to use SMTP 
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers 
$mail->SMTPAuth = true;        // Enable SMTP authentication 
$mail->Username = '[email protected]';     // SMTP username 
$mail->Password = 'secret';       // SMTP password 
$mail->SMTPSecure = 'ssl';       // Enable TLS encryption, `ssl` also accepted 
$mail->Port = 465;         // TCP port to connect to 


$mail->isHTML(true);         // Set email format to HTML 

$mail->Subject = 'mailing'; 

//this is the code that select the emailaddress  
$checkbox = $_POST['checkbox']; 
$query = "SELECT id, name, e_mail FROM memberlist"; 

for($i=0;$i<count($checkbox);$i++) 
     { 
      $delete = "DELETE FROM memberlist WHERE id=$checkbox[$i]"; 
      $result = mysqli_query($link, $query) or die(mysqli_error()); 
     } 

     var_dump($delete); 

$result = mysqli_query($link, $query) or die(mysqli_error()); 
//this is the code that send e-mail to all address in the db, this works 
while ($record = mysqli_fetch_assoc($result)) { 
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 
    //$mail->MsgHTML($message); 
    $mail->Body = $message; 
    $mail->AddAddress($record["e_mail"], $record["name"]); 

} 


if(!$mail->send()) { 
    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 
    echo 'Message has been sent'; 
    } 
    }  
mysqli_close($link); 
?> 

我的HTML代码是:

<form name="form1" id="form1" action="list.php" method="post"> 
<?php 



$query = "SELECT * FROM memberlist"; 


$result = mysqli_query($link, $query) or die(mysqli_error()); 


echo "<table border='1' class='transactions'>"; 
echo "<tr>"; 
echo "<th>total</th>"; 
echo "<th>name</th>"; 
echo "<th>lastname</th>"; 
echo "<th>address</th>"; 
echo "<th>street</th>"; 
echo "<th>place</th>"; 
echo "<th>E-mail</th>"; 
echo "<th>Telephone</th>"; 
echo "<th>company</th>"; 
echo "<th>Select</th>"; 
echo "</tr>"; 

while ($record = mysqli_fetch_assoc($result)) { 
    $name = $record['name']; 
    $lastname = $record['lastname']; 
    $address = $record['address']; 
    $street = $record['street']; 
    $place = $record['place']; 
    $e_mail = $record['e_mail']; 
    $telephone = $record['telephone']; 
    $company = $record['company']; 

    echo "<tr>"; 
    echo "<td>$name </td> <td>$lastname</td> <td>$address</td> <td>$street</td> <td>$place</td> <td>$e_mail</td> <td>$telephone</td> <td>$company</td><td><input type='checkbox' name='checkbox' id='checkItem'> Item</td>"; 
    echo "</tr>"; 

} 
echo "<tr><input type='checkbox' id='checkAll' name='checkbox'> check all</tr>"; 
echo "</table>"; 

?> 

<br /> 
<textarea name="textfield" rows="4" cols="50"></textarea> 
<br /> 
<input type="submit" name="submit" value="send" /> 
<p><?php if(!empty($message)) echo $message; ?></p> 
<?php 
mysqli_close($link); 

?> 

</form> 
+0

DId您将您的复选框的名称设置为复选框[],并将接收到的数组的计数,然后循环,而不是根据数据库中的值进行修剪! –

+0

把你的html代码。 – rahul

DELETE在SQL删除数据,不给它回来。所以你应该改变它为SELECT指令

您的WHERE id = o似乎也是错误的,检查您的复选框的值也。如果你想要更多的帮助,请发布你的html代码(带复选框)。