PHP If statement problem

Hello,

I wonder if someone could help me and am still in the process of learning PHP.

I am trying to edit a script to email the form results - But it needs to check the database to see if nhs_mail is Yes if not continue, however i keep getting Server Error so something slightly wrong.

Could someone cast there eyes over it for me please, I have marked the additional section of the PHP in *** so you can find it.

Many Thanks

[php] $rsDef = mysql_query("select n.email as nhs_email,d.email as def_email,n.mail as nhs_mail,d.username,d.site_name,d.region from defibrillator d join nhs_department n on d.region=n.region where defibrillator_id = ".fsSafeNum(“login_id”));
$rowDef = mysql_fetch_array($rsDef);


   if($rowDef("nhs_mail")==Yes)

{
$message = “Dear Admin,

”;
$message .= ‘A Defibrillator user with username "’.$rowDef[‘username’].’" and site name “’.$rowDef[‘site_name’].’” has submitted ‘.$data_submission_type.’ data as follows:
’;
$message .= “Submission Date: “.date(“d/m/Y”).”
”;
$message .= $form_data."

";
$message .= “Your next $data_submission_type data submission date is $next_date_1

”;

    $footer = str_replace($admin_email_address_var,$admin_email_address,$footer_email_template);
    $footer = str_replace($current_year_var,date("Y"),$footer);                
    
    sendMail($admin_email_address,"$data_submission_type Data submitted by $rowDef[site_name]",$message,$footer,$rowDef['def_email']);        
    ob_clean();
    header("location:".$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
    exit;                                   

}
$message = “Dear Admin,

”;
$message .= ‘A Defibrillator user with username "’.$rowDef[‘username’].’" and site name “’.$rowDef[‘site_name’].’” has submitted ‘.$data_submission_type.’ data as follows:
’;
$message .= “Submission Date: “.date(“d/m/Y”).”
”;
$message .= $form_data."

";
$message .= “Your next $data_submission_type data submission date is $next_date_1

”;

    $footer = str_replace($admin_email_address_var,$admin_email_address,$footer_email_template);
    $footer = str_replace($current_year_var,date("Y"),$footer);                
    
    sendMail($admin_email_address,"$data_submission_type Data submitted by $rowDef[site_name]",$message,$footer,$rowDef['nhs_email']);        
    ob_clean();
    header("location:".$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
    exit;

}
mysql_free_result($rsRecord);

}else{
$data_submission_type = frSafeNum(‘data_submission_type’);
$rsDataType = mysql_query("select data_submission_type from data_submission_type where data_submission_type_id = ".$data_submission_type);
if(mysql_num_rows($rsDataType) > 0)
{
$rowDataType = mysql_fetch_array($rsDataType);
$data_submission_type_text = $rowDataType[‘data_submission_type’];
}
mysql_free_result($rsDataType);
}
[/php]

You need quotes around the yes on this line
[php]if($rowDef(“nhs_mail”)==Yes)[/php]

Thanks fastsol for your reply, I have changed it to

[php]if($rowDef(“nhs_mail”)==‘Yes’)[/php]

Unfortunately still have the same problem.

[php]if ($rowDef[‘nhs_mail’] == ‘Yes’)[/php]

Thank for the suggestion M@tt,

Unfortunately that did not work either :frowning:

What exactly doesn’t work?

[php]******************************************************[/php]

Are these actually in your code?

Your curly brackets are also not correct. Is this the entire code?

Hi M@tt,

No the **** are not the in code, They were to show the section i added.

Whats wrong with the brackets? maybe that’s the problem?

Without seeing the full code it’s hard to say if the brackets are correct.

The closing bracket under your second **** line doesn’t have any opening bracket.

Also, why are you duplicating that entire section of code?

If ‘nhs_mail’ == ‘Yes’ it would send 2 emails, otherwise it’s still going to send 1. I don’t see the point in having all this code duplicated

Not sure if this will help. It looks like the only reason you duplicated the code was to switch between ‘nhs_email’ and ‘def_email’

[php]
$message = “Dear Admin,

”;
$message .= ‘A Defibrillator user with username "’.$rowDef[‘username’].’" and site name “’.$rowDef[‘site_name’].’” has submitted ‘.$data_submission_type.’ data as follows:
’;
$message .= “Submission Date: “.date(“d/m/Y”).”
”;
$message .= $form_data."

";
$message .= “Your next $data_submission_type data submission date is $next_date_1

”;

$footer = str_replace($admin_email_address_var,$admin_email_address,$footer_email_template);
$footer = str_replace($current_year_var,date(“Y”),$footer);

$email = $rowDef[‘nhs_email’]; // default to nhs_email
if($rowDef[‘nhs_mail’] == ‘Yes’) {
$email = $rowDef[‘def_email’]; // switch to def_email when nhs_mail = Yes
}

sendMail($admin_email_address,"$data_submission_type Data submitted by $rowDef[site_name]",$message,$footer,$email);
ob_clean();
header(“location:”.$_SERVER[‘PHP_SELF’].’?’.$_SERVER[‘QUERY_STRING’]);
exit;
[/php]

Hi M@tt,

if nhs_mail = yes it will send 2 email one to nhs address and 1 to admin, If no it will just send to admin.

I will enter all the code over 2 posts (too big for one) any help gratefully received.

[php]<?php
require_once(“config.php”);
$data_submission_type_id = 0;
$menu_option = “submit_data”;

$defibrillator_id = fsSafeNum(“login_id”);

if($_REQUEST[‘action’] == “allow_instant_check” && $defibrillator_id > 0 && $_REQUEST[‘type’] > 0){
mysql_query("delete from data_submission_date where data_id = “.$_REQUEST[‘type’].” and user_id = ".$defibrillator_id);
$_SESSION[‘msg’] = “Instant check added successfully”;
header(“location:”.$_SERVER[‘PHP_SELF’]);
exit;
}

if(frSafeNum(‘data_submission_type’) > 0)
{
$data_submission_type_id = frSafeNum(‘data_submission_type’);
}

$rsDataType = mysql_query(“select data_submission_type from data_submission_type where data_submission_type_id = “.$data_submission_type_id);
if(mysql_num_rows($rsDataType) == 0 && $data_submission_type_id > 0)
{
$_SESSION[‘wmsg’] = “Wrong data submission type selected”;
ob_clean();
header(“location:”.$_SERVER[‘PHP_SELF’]);
exit;
}
if($_REQUEST[‘cmdSubmit’] != “”){
$data_submission_type_id = frSafeNum(‘data_submission_type_id’);
$rsDataType = mysql_query(“select data_submission_type from data_submission_type where data_submission_type_id = “.$data_submission_type_id);
if(mysql_num_rows($rsDataType) > 0)
{
$rowDataType = mysql_fetch_array($rsDataType);
$data_submission_type = $rowDataType[‘data_submission_type’];
}
mysql_free_result($rsDataType);
$rsRecord = mysql_query(“SELECT ff.form_field_id,data_submission_type_id,label,extra_data,fft.field_id, fft.field_label, fft.field_type
FROM form_fields ff
JOIN form_fields_type fft ON fft.field_id = ff.field_id
WHERE data_submission_type_id = $data_submission_type_id
order by form_field_id“);
if(mysql_num_rows($rsRecord) > 0)
{
$data_id = ‘’;
$form_data = “”;
$cm_gp = rand(1111111111,9999999999);
while($rowRecord = mysql_fetch_array($rsRecord))
{
if($rowRecord[‘field_type’] == “checkbox”)
{
$value_arr = array();
if(is_array($REQUEST["name”.lowercase_un($rowRecord[‘label’])]))
{
$value_arr = implode(”, “,$REQUEST["name”.lowercase_un($rowRecord[‘label’])]);
$sql = “insert into user_form_data (user_id,form_field_id,data_submission_type_id,label,value,cm_gp) values
($defibrillator_id,”.$rowRecord[‘form_field_id’].”,$data_submission_type_id,
'”.$rowRecord[‘label’].”’,’”.$value_arr."’,’$cm_gp’)";
//echo $sql;
mysql_query($sql) or die(“Error2: “.mysql_error());
$form_data .= “”.$rowRecord[‘label’].”: “.$value_arr.”
”;
}
}
else
{
$sql = “insert into user_form_data (user_id,form_field_id,data_submission_type_id,label,value,cm_gp) values
($defibrillator_id,”.$rowRecord[‘form_field_id’].",$data_submission_type_id,
‘".$rowRecord[‘label’]."’,’".$REQUEST["name".lowercase_un($rowRecord[‘label’])]."’,’$cm_gp’)";
mysql_query($sql) or die(“Error2: “.mysql_error());
$form_data .= “”.$rowRecord[‘label’].”: “.$REQUEST["name”.lowercase_un($rowRecord[‘label’])].”
";
}

        $data_id = mysql_insert_id();
    }
    $_SESSION['msg'] = "User data updated successfully";
    
    //mysql_query("delete from data_submission_date where data_id = $data_submission_type_id and user_id = $defibrillator_id");
    if($data_submission_type_id == 1) $days = "week";
    if($data_submission_type_id == 2) $days = "month";
    if($data_submission_type_id == 3) $days = "year";
    if($data_submission_type_id == 4) $days = "day";

    $next_date = date("Y-m-d",strtotime("+1 $days"));

    // Insert entries for all 4 types
    // ============================================================================================================
    // 1) week
    $next_date_w = date("Y-m-d",strtotime("+1 week"));
    mysql_query("insert into data_submission_date (data_id,user_id,submission_type_id,next_date) values ($data_id, $defibrillator_id, 1,'$next_date_w')");

    // 2) month
    $next_date_m = date("Y-m-d",strtotime("+1 month"));
    mysql_query("insert into data_submission_date (data_id,user_id,submission_type_id,next_date) values ($data_id, $defibrillator_id, 2,'$next_date_m')");

    // 3) year
    $next_date_y = date("Y-m-d",strtotime("+1 year"));
    mysql_query("insert into data_submission_date (data_id,user_id,submission_type_id,next_date) values ($data_id, $defibrillator_id, 3,'$next_date_y')");

    // 4) day
    $next_date_d = date("Y-m-d",strtotime("+1 day"));
    mysql_query("insert into data_submission_date (data_id,user_id,submission_type_id,next_date) values ($data_id, $defibrillator_id, 4,'$next_date_d')");
    // ============================================================================================================

    $next_date_1 = date("d/m/Y",strtotime($next_date));
    
    $rsDef = mysql_query("select n.email as nhs_email,d.email as def_email,n.mail as nhs_mail,d.username,d.site_name,d.region from defibrillator d join nhs_department n on d.region=n.region where defibrillator_id = ".fsSafeNum("login_id"));
    $rowDef = mysql_fetch_array($rsDef);

if($rowDef(“nhs_mail”) == ‘Yes’)
{
$message = “Dear Admin,

”;
$message .= ‘A Defibrillator user with username "’.$rowDef[‘username’].’" and site name “’.$rowDef[‘site_name’].’” has submitted ‘.$data_submission_type.’ data as follows:
’;
$message .= “Submission Date: “.date(“d/m/Y”).”
”;
$message .= $form_data."

";
$message .= “Your next $data_submission_type data submission date is $next_date_1

”;

    $footer = str_replace($admin_email_address_var,$admin_email_address,$footer_email_template);
    $footer = str_replace($current_year_var,date("Y"),$footer);                
    
    sendMail($admin_email_address,"$data_submission_type Data submitted by $rowDef[site_name]",$message,$footer,$rowDef['def_email']);        
    ob_clean();
    header("location:".$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
    exit;                                   

}
$message = “Dear Admin,

”;
$message .= ‘A Defibrillator user with username "’.$rowDef[‘username’].’" and site name “’.$rowDef[‘site_name’].’” has submitted ‘.$data_submission_type.’ data as follows:
’;
$message .= “Submission Date: “.date(“d/m/Y”).”
”;
$message .= $form_data."

";
$message .= “Your next $data_submission_type data submission date is $next_date_1

”;

    $footer = str_replace($admin_email_address_var,$admin_email_address,$footer_email_template);
    $footer = str_replace($current_year_var,date("Y"),$footer);                
    
    sendMail($admin_email_address,"$data_submission_type Data submitted by $rowDef[site_name]",$message,$footer,$rowDef['nhs_email']);        
    ob_clean();
    header("location:".$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
    exit;

}
mysql_free_result($rsRecord);
}else{
$data_submission_type = frSafeNum(‘data_submission_type’);
$rsDataType = mysql_query("select data_submission_type from data_submission_type where data_submission_type_id = ".$data_submission_type);
if(mysql_num_rows($rsDataType) > 0)
{
$rowDataType = mysql_fetch_array($rsDataType);
$data_submission_type_text = $rowDataType[‘data_submission_type’];
}
mysql_free_result($rsDataType);
}
[/php]

Second half of code

[php]?>

User form data <?php include('inc/topbar.php');?> jQuery(document).ready(function(){ jQuery(".detail_row:odd").addClass('formrow1'); jQuery(".detail_row:even").addClass("formrow2");
        jQuery("#detail_form").validationEngine({
             ajaxSubmit: true,
             ajaxSubmitFile: "<?php echo $_SERVER['SCRIPT_NAME']?>",
             ajaxSubmitMessage: "",
             success :  function() {
                window.location.href = "<?php echo $list_filename?>"
             },
             failure : function() {

             }
        });  
        $('.wysiwyg').wysiwyg();                                  
    });
    
</script>
<?php include('inc/header.php');?>

User Form

					<div class="content-box-content">
                          <?php
                             if($_SESSION['msg']!=""){
                                 echo '<div class="notification success">'.$_SESSION['msg'].'</div>';
                                 $_SESSION['msg']="";
                             }   
                          ?>  
                          <?php
                          if($_SESSION['wmsg']!=""){
                             echo '<div class="notification warning">'.$_SESSION['wmsg'].'</div>';
                             $_SESSION['wmsg']="";
                          }
                          $bWarning = false;
                          if($data_submission_type_id > 0 && $defibrillator_id > 0){
                                $rsNextDate = mysql_query("select next_date from data_submission_date where data_id = $data_submission_type_id and user_id = $defibrillator_id");
                                if(mysql_num_rows($rsNextDate) > 0){
                                    $rowNextDate = mysql_fetch_array($rsNextDate);
                                    if($rowNextDate['next_date'] != date("Y-m-d") && $rowNextDate['next_date'] > date("Y-m-d")){
                                        $bWarning = true;
                                        echo "<div class='notification warning'>Your next <b>$data_submission_type_text</b> data submission date is <i><b>".date("d/m/Y",strtotime($rowNextDate['next_date']))."</b></i>";              
                                        echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                              <a class='button_no' href='".$_SERVER['PHP_SELF']."?action=allow_instant_check&type=$data_submission_type_id'>Do Instant Check</a>";
                                        echo "</div>";
                                    }
                                    mysql_free_result($rsNextDate);
                                }else{
                                    $bWarning = false;
                                }
                          }
                          $data_submission_type_text;
                          ?>
                          <form name="form_field" method="get" style="margin-left: 10px;">
                               <select name="data_submission_type" onchange="this.form.submit();">
                                    <option value="0">Select Data Type</option>
                                    <?php echo dataSubmissionTypeOption($data_submission_type_id);?>
                               </select>
                          </form>
                          <?php
                          if(!$bWarning){
                                ?>                                
                                <div class="form_data_wrapper">
                                    <?php
                                    $rsRecord = mysql_query("SELECT `data_submission_type_id`,label,extra_data,fft.field_id, fft.field_label, fft.field_type
                                                             FROM form_fields ff
                                                             JOIN form_fields_type fft ON fft.field_id = ff.field_id                                                                 
                                                             WHERE data_submission_type_id = $data_submission_type_id
                                                             order by `form_field_id`");
                                    if(mysql_num_rows($rsRecord) > 0)
                                    {
                                        ?>
                                        <form method="post" name="form_data" class="form_field_data" id="detail_form">
                                            <?php
                                            while($rowRecord = mysql_fetch_array($rsRecord))
                                            {
                                                ?>                                                
                                                     <div class="detail_row user">
                                                        <div class="detail_column"><?php echo $rowRecord['label'] ?></div>
                                                        <div class="detail_field">
                                                        <?php 
                                                        if($rowRecord['field_type'] == "checkbox" || $rowRecord['field_type'] == "select" || $rowRecord['field_type'] == "radio")
                                                        {
                                                            if($rowRecord['extra_data'] != "")
                                                            {
                                                                $extra_data_arr = explode("~~~",$rowRecord['extra_data']);
                                                                $value_arr_new = explode("|||",$extra_data_arr[0]);
                                                                $text_arr_new = explode("|||",$extra_data_arr[1]);
                                                                if($rowRecord['field_type'] == "checkbox")
                                                                {
                                                                    for($i=0;$i<=9;$i++){
                                                                         if($value_arr_new[$i] != ""){
                                                                             echo "<input id='check_$i_".lowercase_un($rowRecord['label'])."' value='".$value_arr_new[$i]."' class='validate[minCheckbox[1]] checkbox' type='checkbox' name='name_".lowercase_un($rowRecord['label'])."[]' /> ".$text_arr_new[$i]."<br>";                                                                                                                                                  
                                                                         }
                                                                    }
                                                                }
                                                                if($rowRecord['field_type'] == "radio")
                                                                {
                                                                    for($i=0;$i<=9;$i++){
                                                                         if($value_arr_new[$i] != ""){
                                                                             echo "<input id='radio_$i_".lowercase_un($rowRecord['label'])."' value='".$value_arr_new[$i]."' class='validate[required] checkbox' type='radio' name='name_".lowercase_un($rowRecord['label'])."' /> ".$text_arr_new[$i];                                                                                                                                                  
                                                                         }
                                                                    }
                                                                }
                                                                if($rowRecord['field_type'] == "select")
                                                                {
                                                                    echo "<select id='select_".lowercase_un($rowRecord['label'])."' class='validate[required]' name='name_".lowercase_un($rowRecord['label'])."'>";
                                                                    for($i=0;$i<=9;$i++){
                                                                         if($value_arr_new[$i] != ""){
                                                                             echo "<option value='".$value_arr_new[$i]."'>".$text_arr_new[$i]."</option>";                                                                                                                                                  
                                                                         }
                                                                    }
                                                                    echo "</select>";
                                                                }
                                                                
                                                            }
                                                        }
                                                        elseif($rowRecord['field_type'] == "text")
                                                        {
                                                            ?>
                                                            <input id='text_<?php echo lowercase_un($rowRecord['label']) ?>' class="detail_text_field validate[required]" type="text" name="name_<?php echo lowercase_un($rowRecord['label']) ?>" />
                                                            <?php   
                                                        }
                                                        elseif($rowRecord['field_type'] == "textarea")
                                                        {
                                                            ?>
                                                            <textarea id='textarea_<?php echo lowercase_un($rowRecord['label']) ?>' rows="5" cols="40" class="wysiwyg validate[required]" name="name_<?php echo lowercase_un($rowRecord['label']) ?>"></textarea>
                                                            <?php
                                                        }
                                                        ?>
                                                        </div>
                                                        <div class="clear"></div>        
                                                     </div>                                                                                                  
                                                     <div class="clear"></div>                                                                                             
                                                <?php                                                
                                            }
                                            ?>
                                            <input type="hidden" value="<?php echo $data_submission_type_id ?>" name="data_submission_type_id" />
                                            <input type="submit" value="Submit Form" name="cmdSubmit" />
                                        </form>
                                        <?php
                                    }
                                    ?>
                                </div>
                                <?php
                           }
                           ?>     
					</div><!-- end .content-box-content -->

				</div><!-- end .content-box -->								
				<div class="clear"></div>
			</div><!-- end #content -->
		
	          <div id="push"></div>
        </div>
    </div><!-- end #bokeh and #container -->
    <?php include('inc/footer.php');?>				
[/php]

Ok, so if you need it to send 2 emails then you can’t redirect and exit after the first one is sent.

[php]
$rsDef = mysql_query("select n.email as nhs_email,d.email as def_email,n.mail as nhs_mail,d.username,d.site_name,d.region from defibrillator d join nhs_department n on d.region=n.region where defibrillator_id = ".fsSafeNum(“login_id”));
$rowDef = mysql_fetch_array($rsDef);

	$message = "Dear Admin,<br><br>";
	$message .= 'A Defibrillator user with username "'.$rowDef['username'].'" and site name "'.$rowDef['site_name'].'" has submitted '.$data_submission_type.' data as follows:<br>';
	$message .= "<b>Submission Date</b>: ".date("d/m/Y")."<br>";
	$message .= $form_data."<br/><br/>";
	$message .= "Your next <b>$data_submission_type</b> data submission date is <i><b>$next_date_1</b></i><br /><br />";      
	  
	$footer = str_replace($admin_email_address_var,$admin_email_address,$footer_email_template);
	$footer = str_replace($current_year_var,date("Y"),$footer);
	
	if($rowDef['nhs_mail'] == 'Yes') {
		// send first email
		sendMail($admin_email_address,"$data_submission_type Data submitted by $rowDef[site_name]",$message,$footer,$rowDef['def_email']);
	}
	// send second email
	sendMail($admin_email_address,"$data_submission_type Data submitted by $rowDef[site_name]",$message,$footer,$rowDef['nhs_email']);
	
	// redirect
	ob_clean();
	header("location:".$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
	exit;
}
mysql_free_result($rsRecord);

[/php]

Thanks M@tt,

You make it look so easy! Works a treat…

Thanks you very much :smiley: :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service