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>