dongzha0149 2016-10-27 09:00
浏览 53
已采纳

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

I have a php code that fetch name and email and runs them in a foreach lop so it can send email to a respodent and inputs name and email into a MySQL databas.

My problem is that i can only select all of them but not single once. If I chose only one respodent i get Warning: array_combine(): Both parameters should have an equal number of elements and if i chose all of them it works without a problem. I have tried some solutions as $res = array_combine(array_intersect_key($arr1, $arr2), array_intersect_key($arr2, $arr1));

But it then takes only the names from the top and not the exact name of each email. If I select 2 respodents, lets say Mark and Fredik , Alex and Mark gets inserted inte the database instead of Mark and Frederick. It shows up right email but not the names.

Is there a workaround/fix for this problem?

    <?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>";
}
    }
    }

?>

The form look like this

  <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条回答 默认 最新

  • douzi0609 2016-10-27 16:00
    关注

    I suggest a more convenient way to build your form so you can access the desired data more easy. Changed part of your form:

    <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>
    

    And use it in your php mailing script like this:

    $arrayOne = $_POST['check'];
    $arrayTwo = $_POST['fname'];
    
    foreach($arrayOne as $email){
    
       $fname = $arrayTwo[$email];
       echo $email. ' '. $fname ;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?