Php 在foreach循环中存储变量
您好,我目前有一些PHP代码,可以将以前从上一页的复选框中选择的时间段放入我的数据库中,但我不确定在执行此操作之前如何将每个时间段存储在变量中,以便我可以使用它们在我的电子邮件函数中列出约会时间段 当我尝试将“$key”插入电子邮件功能时,它只保留最后选择的值 我在第一页的代码是:Php 在foreach循环中存储变量,php,mysql,foreach,Php,Mysql,Foreach,您好,我目前有一些PHP代码,可以将以前从上一页的复选框中选择的时间段放入我的数据库中,但我不确定在执行此操作之前如何将每个时间段存储在变量中,以便我可以使用它们在我的电子邮件函数中列出约会时间段 当我尝试将“$key”插入电子邮件功能时,它只保留最后选择的值 我在第一页的代码是: <?php for ($i=0;$i<=31;$i++){ while ($myrow = mysql_fetch_row($result)) { printf("<tr><td>
<?php
for ($i=0;$i<=31;$i++){ while ($myrow = mysql_fetch_row($result)) {
printf("<tr><td>%s</td><td>%s</td></tr>\n",
$myrow[0]);
}
echo "<td><input type='checkbox' name='$displayTime2' onclick='KeepCount()'></td>";
echo "<td>$pagetime</td>"; ?>
虽然上面的评论是对的,但让我来帮你 首先,您需要确定选中了哪些复选框,所以在foreach循环中的每个复选框上执行一些IF语句来确定 试着这样做:
<?php
$savedData = array();
foreach ($_POST as $key=>$value){
$key = mysql_real_escape_string($key);
echo($key. "<br>"); // show times selected on previous page
mysql_query("INSERT INTO appointment(Patient_ID, Appointment_Date, Appointment_Time
, Practice_ID, Appointment_ID)
VALUES('$patid','$insertdate','$key','$pracid','$apptype')");
//To save the variables for later:
$savedData[] = $key;
}
?>
因此,这将为$savedData中的每一部分发送一封电子邮件-如果这真的是目的的话。这段代码中有很多错误级别…请发布您的HTML代码。您能写更多详细信息吗?你说的是电子邮件,但我只是在循环中插入mysql语句…这是什么?我甚至没有在for循环中放入mysql查询-它会锁定你的表。不要使用mysql_查询-使用PDO或MySQLi。在没有先清理变量的情况下,不要发布数据在本例中为$\u post数组。这些是复选框,因此该值通常为开或关。复选框的名称可能以某种方式表示日期。@mellamokbtheWise good call,没有看到复选框注释。让我编辑。@Shackrock非常感谢,我对其进行了轻微修改,使其仅发送一封包含所有选定时间的电子邮件,感谢您的所有回复-非常感谢:-
$to = "$pemail";
$subject = "Talking Teeth Check Up Booking Confrmation";
$message = "Hello! This e-mail is to confirm your ".$appname." appointment has been
booked for ".$insertdate. " at ".$key." at the ".$pracname." practice";
$from = "talkingteethdentists@talkingteeth.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
<?php
$savedData = array();
foreach ($_POST as $key=>$value){
$key = mysql_real_escape_string($key);
echo($key. "<br>"); // show times selected on previous page
mysql_query("INSERT INTO appointment(Patient_ID, Appointment_Date, Appointment_Time
, Practice_ID, Appointment_ID)
VALUES('$patid','$insertdate','$key','$pracid','$apptype')");
//To save the variables for later:
$savedData[] = $key;
}
?>
foreach($savedData as $time){
$to = "$pemail";
$subject = "Talking Teeth Check Up Booking Confirmation"; //mis-spelled confirmation =)
$message = "Hello! This e-mail is to confirm your ".$appname." appointment has been
booked for ".$insertdate. " at ".$time." at the ".$pracname." practice";
$from = "talkingteethdentists@talkingteeth.co.uk";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
}