array_combine可能与不相等数量的元素? Foreach替代品?

时间:2016-10-27 09:00:11

标签: php mysql arrays email

我有一个PHP代码,可以获取名称和电子邮件,并在foreach lop中运行它们,因此它可以向respodent发送电子邮件,并将名称和电子邮件输入到MySQL数据库中。

我的问题是我只能选择所有这些而不是单次一次。如果我只选择了一个respodent,我会得到Warning: array_combine(): Both parameters should have an equal number of elements,如果我选择了所有这些,它的工作没有问题。我尝试了一些解决方案$res = array_combine(array_intersect_key($arr1, $arr2), array_intersect_key($arr2, $arr1));

但它只接受顶部的名称,而不是每封电子邮件的确切名称。如果我选择2个respodents,让我们说马克和弗雷迪克,亚历克斯和马克插入数据库而不是马克和弗雷德里克。它显示正确的电子邮件但不显示名称。

是否有针对此问题的解决方法/修复方法?

    <?php
if(isset($_POST['check']) == true)
{

    $subject = trim($_POST['subject']);
    $message = trim($_POST['message']);
    $from = 'testing@test.com';
    $i=1;

    $arrayOne = $_POST['check'];
    $arrayTwo = $_POST['fname'];


 $min = min(count($arrayOne), count($arrayTwo));
    $result = array_combine(array_slice($arrayOne, 0, $min), array_slice($arrayTwo, 0, $min));

    foreach($result as $key => $value)
    {

    echo $key. ' '. $value ;

ini_set('display_errors',1);
error_reporting(E_ALL);
require 'class.phpmailer.php';
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

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


$mail->isSMTP();                                        // Set mailer to use SMTP 
$mail->Host = 'smt';             // Specify main/backup SMTP servers 
$mail->SMTPAuth = true;                           // Enable SMTP authentication 
$mail->Username = 'com';    // SMTP username 
$mail->Password = '4';    // SMTP password 
$mail->SMTPSecure = 'tls';                        // Enable TLS/SSL encryption 
$mail->Port = 587;                                      // TCP port to connect to

$mail->From = $from;
$to = $key;
$fornamn = $value;


$confirm_code=md5(uniqid(rand()));
    $pidnew4 = $_POST['pidnew3'];
    $pnamnnew4 = $_POST['pnamnnew3'];

$mail->IsHTML(true);

$mail->CharSet='UTF-8';
$mail->confirm_code=md5(uniqid(rand()));
$mail->FromName = $from; 
$mail->addAddress($to);
$mail->Subject = $_POST['subject'];
$mail->Message = $_POST['message'];
$msg = " Test!!";
$mail->Body = $msg;


$i++;

$sql= "INSERT INTO temp_members_db(firstname, confirm_code, email, pid, rid, note) VALUES('$fornamn', '$confirm_code', '$to', '$pidnew4', '$i', '')";
$b = mysqli_query($mysqli,$sql);

if(!$mail->send()) {

    echo 'Message could not be sent.'; 
    echo 'Mailer Error: ' . $mail->ErrorInfo; 
} else { 

    echo "<h2>Sent</h2><br><br>


<br>";
}
    }
    }

?>

表单看起来像这样

  <form action='insertrespondent1.php' method='POST' enctype='multipart/form-data' name='insertrespondent' onsubmit='return validateForm()'>

    <?php

$pidnew2 = $_GET['pidnew'];
$pnamnnew2 = $_GET['pnamnnew'];


    // Retrieve Email from Database
    $getemail = mysql_query("SELECT * FROM Email_Users WHERE pid='".$pidnew2."'");

    if (!$getemail) die('MySQL Error: ' . mysql_error());

    echo '<table class="table table-bordered">';
    echo "<thead>
          <tr>
          <th><input type='checkbox' onchange='checkedbox(this)' name='chk'/></th>
          <th>Förnamn</th>
          <th>Email</th> 
          </tr>
          </thead>";

    if (mysql_num_rows($getemail) == 0) {    
    echo "<tbody><tr><td colspan='3'>No Data Avaialble</td></tr></tbody>";    
    } 

    while ($row = mysql_fetch_assoc($getemail)) {
        echo "<input type='hidden' value='".$row['fornamn']."' name='fname[]' id='fname[]'/";
        echo "<tbody><tr><td><input value='".$row['email']."' type='checkbox' name='check[]' id='check[]'/></td>";
        echo "<td >".$row['fornamn']."</td>";
        echo "<td >".$row['email']."</td></tr></tbody>";

    } 
    echo "</table>";



    ?>
    </center>
    <br>
</div>


    <br>
    <br>

<?php       
        if (!empty($error_msg)) {
            echo $error_msg;
        }


$quest = "SELECT coalesce(max(rid)+1,1) AS id FROM temp_members_db";
$result = $mysqli->query($quest);

if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {   

echo "
    <input type='hidden' name='ridsend' value='". $row["id"] ."'>
    <input type='hidden' name='pidnew3' value='$pidnew2'>
    <input type='hidden' name='pnamnnew3' value='$pnamnnew2'>

 <b>Ämne </b><br><textarea rows='1' cols='1' name='subject'></textarea><br> 
<b>Meddelande </b><br><textarea rows='10' cols='30' name='message'></textarea> <br>



    <p>Klicka på knappen nedan för att den nya kandidaten ska bli tillagd.</p>
    <p>En länk för testet skickas ut till samtliga kandidater.</p>


<br>";
    }}

mysqli_close($mysqli);

        ?>


        <input type='submit' class='btn2' name='submit' src='https://i.imgur.com/0xPEf.gif' color='#556270' style='background-color:#556270; value='Skicka test'>

        </form>

1 个答案:

答案 0 :(得分:1)

我建议您更方便地构建表单,以便更轻松地访问所需数据。 更改了表单的一部分:

<tbody>
<?php  while ($row = mysql_fetch_assoc($getemail)) { ?>
<input type="hidden" value="<?php echo $row['fornamn']; ?>" name="fname[<?php echo $row['email']; ?>]"/>
   <tr>
      <td><input value="<?php echo $row['email']; ?>" type='checkbox' name="check[]" /></td>
      <td >".$row['fornamn']."</td>
      <td >".$row['email']."</td>
   </tr>
<?php } ?>
</tbody>

并在 php邮件脚本中使用它,如下所示:

$arrayOne = $_POST['check'];
$arrayTwo = $_POST['fname'];

foreach($arrayOne as $email){

   $fname = $arrayTwo[$email];
   echo $email. ' '. $fname ;
}