0

Im a bit stuck. I have a database which holds a list of various medical treatments. A medical officer would select a medical aid and the page will display all available treatments for that Medical aid. The officer would then tick the treatment he will be performing, Upload a file, type a message and then submit the file, message and selected treatments to a mail page which will mail out the details.

The page is reading the database and is creating a check box list of all available treatments for the selected medical aid, so if there are 10 treatments for "Mighty Med" i will see 10 lines, each with its own check box. The problem i am having is when the form is submitted only the file and message goes through. The items selected dont reach the post page. If i do not read from the database and have all the treatment lines coded onto the page then all items selected go through.

This is my form that reads from the database and displays the check box list:

<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

<title>Mr X-Ray</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
    if(!empty($_POST['Name']) && !empty($_POST['SurName']))
    {
        $MedaidNumber = mysqli_real_escape_string($link, $_POST['MedaidNumber']);
        $Name = mysqli_real_escape_string($link, $_POST['Name']);
        $SurName = mysqli_real_escape_string($link, $_POST['SurName']);
        $MedAid  = mysqli_real_escape_string($link, $_POST['taskOption']);
        $PracticeNum = mysqli_real_escape_string($link, $_POST['PracticeNum']);
        $Date = mysqli_real_escape_string($link, date("Y-m-d"));

        $checkusername = mysqli_query($link,"SELECT * FROM users WHERE PracticeNum = '".$PracticeNum."'");

         if(mysqli_num_rows($checkusername) == 0)
         {
            echo "<h1>Error</h1>";
            echo "<p>Sorry, that your practice number is not valid. Please go back and try again.</p>";
         }
         else
         {
            $registerquery = mysqli_query($link,"INSERT INTO patients (MedaidNumber, Name, SurName, MedAid, PracticeNum, Date) VALUES('".$MedaidNumber."', '".$Name."', '".$SurName."', '".$MedAid."', '".$PracticeNum."', '".$Date."')");


            if ($registerquery)
            {
                echo "<h1>Success</h1>";
                echo "<p>Your $MedAid patient was successfully added. Please select the treament given.</p>";

                // this is where i am stuck, the query reads the database and creates a check box list of all treatments
                // i can tick all the treatments and i can type out a message and upload a file.
                $result = mysqli_query($link,"SELECT TreatmentName FROM treatment where Medaid = '".$MedAid."'");
                if ($result) {
                    while($row = mysqli_fetch_array($result)){
                        echo "<input type='checkbox' name='TreatmentName[]' value='".$row['TreatmentName']."'>"
                                        .$row['TreatmentName'] ;
                        echo "<h1></h1>";

                    }
?>

        <form name="myForm" action="SendMail.php" method="post"enctype="multipart/form-data">
                Message: <textarea name="message" placeholder="Your Message.."></textarea>
                <h1></h1>
                <label for='uploaded_file'>Select A File To Upload:</label>
                <input type="file" name="uploaded_file">
                <h1></h1>
                <input type="submit" name="submit" value="Submit" />

<?php   
                // after clicking on submit the items i ticked dont get posted.
                // my file and message goes across though.                  
                }
            }
        else
        {
            echo "<h1>Error</h1>";
            echo "<p>Sorry, Your patient was not added. Please go back and try again.</p>";    

        }       
     }
}

   elseif 

   (empty($_SESSION['LoggedIn']) && empty($_SESSION['Username']))
   if(empty($_POST['Name']) && empty($_POST['SurName']))
        echo "<h1> no Success</h1>";
        // echo "<a href=\billing\index.php>click here to log in</a>";
        if(empty($_POST['Name']) && empty($_POST['SurName']))
            echo "<h1> no Success post</h1>";
?>

</div>
</body>
</html>

This is my submit form for receiving selected items, attachment and message and sending out the mail :

<?php

 if($_POST && isset($_FILES['uploaded_file']))
{

    $from_email         = 'somebody@mycorp.com'; //from mail, it is mandatory with some hosts
    $recipient_email    = 'somebody@mycorp.com'; //recipient email (most cases it is your personal email)

    //Capture POST data from HTML form and Sanitize them,
    $sender_name    = filter_var($_POST["patientsurName"], FILTER_SANITIZE_STRING); //sender name
    $reply_to_email = filter_var($_POST["Doctor"], FILTER_SANITIZE_STRING); //sender email used in "reply-to" header
    $subject        = filter_var($_POST["Doctor"], FILTER_SANITIZE_STRING); //get subject from HTML form
    $message        = filter_var($_POST["message"], FILTER_SANITIZE_STRING); //message
    $code = implode( "\r\n" .'  ', $_POST['TreatmentName'] );
    $FullMessage = 
                    'Hello
                    ' ."\r\n" .
                    'You have received a new form entry from ' . $_POST['doctor'] . '
                    ' ."\r\n" .
                                        'Patient Name :   ' . $_POST['patientName']. '
                    ' .
                                        'Patient Surname :   ' . $_POST['patientsurName']. '
                    ' .
                                        'Patient Medical Aid Number :   ' . $_POST['medicalAidNumber']. '
                    ' ."\r\n" .
                                       ' These are the codes used :   
                    ' ."\r\n" .
                                        '  ' . $code . '
                    ' ."\r\n" . 

                                        ' ' . $_POST['doctor']. ' has attached the following comment: 

                    ' .
                               $_POST['message'];



    //Get uploaded file data
    $file_tmp_name    = $_FILES['uploaded_file']['tmp_name'];
    $file_name        = $_FILES['uploaded_file']['name'];
    $file_size        = $_FILES['uploaded_file']['size'];
    $file_type        = $_FILES['uploaded_file']['type'];
    $file_error       = $_FILES['uploaded_file']['error'];

    if($file_error > 0)
    {
        die('Upload error or No files uploaded');
    }
    //read from the uploaded file & base64_encode content for the mail
    $handle = fopen($file_tmp_name, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $encoded_content = chunk_split(base64_encode($content));

    $boundary = md5("sanwebe");
    //header
    $headers = "MIME-Version: 1.0\r\n";
    $headers .= "From:".$from_email."\r\n";
    $headers .= "Reply-To: ".$reply_to_email."" . "\r\n";
    $headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";

    //plain text
    $body = "--$boundary\r\n";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n";
    $body .= "Content-Transfer-Encoding: base64\r\n\r\n";
    $body .= chunk_split(base64_encode($FullMessage));

    //attachment
    $body .= "--$boundary\r\n";
    $body .="Content-Type: $file_type; name=".$file_name."\r\n";
    $body .="Content-Disposition: attachment; filename=".$file_name."\r\n";
    $body .="Content-Transfer-Encoding: base64\r\n";
    $body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
    $body .= $encoded_content;

    $sentMail = @mail($recipient_email, $subject, $body, $headers);
    if($sentMail) //output success or failure messages
    {      
        die('Thank you for your email. <a href=\billing\patientDetails.php> click here to start billing a new patient</a>');
    }else{
        die('Could not send mail! Please check your PHP mail configuration.');  

    }
  echo('PAge offline.'); 
}
?>

Can you please assist in showing me where im going wrong, why would it work if i manually typed out and created a list and not work from my loop?

Thanks

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
M-Corp
  • 137
  • 1
  • 3
  • 15
  • 1
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Mar 12 '17 at 13:47

1 Answers1

0

The problem here was that i put in the

<form name="myForm" action="SendMail.php" method="post"enctype="multipart/form-data">

in the wrong place.

It needed to be above the array. I originally had it below the array.

here is what the code looks like completed. :

<?php include "base.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

<title>-Ray</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))

if(!empty($_POST['Name']) && !empty($_POST['SurName']))
{
    $MedaidNumber = mysqli_real_escape_string($link, $_POST['MedaidNumber']);
    $Name = mysqli_real_escape_string($link, $_POST['Name']);
    $SurName = mysqli_real_escape_string($link, $_POST['SurName']);
    $MedAid  = mysqli_real_escape_string($link, $_POST['taskOption']);
    $PracticeNum = mysqli_real_escape_string($link, $_POST['PracticeNum']);
     $Date = mysqli_real_escape_string($link, date("Y-m-d"));


     $checkusername = mysqli_query($link,"SELECT * FROM users WHERE PracticeNum = '".$PracticeNum."'");

     if(mysqli_num_rows($checkusername) == 0)
     {
        echo "<h1>Error</h1>";
        echo "<p>Sorry, that your practice number is not valid. Please go back and try again.</p>";
     }
     else
     {
$registerquery = mysqli_query($link,"INSERT INTO patients (MedaidNumber, Name, SurName, MedAid, PracticeNum, Date) VALUES('".$MedaidNumber."', '".$Name."', '".$SurName."', '".$MedAid."', '".$PracticeNum."', '".$Date."')");


        if($registerquery)
        {
            echo "<h1>Success</h1>";
            echo "<p>Your $MedAid patient was successfully added. Please select the treament given.</p>";


            ?>

                                                                        <form name="myForm" action="SendMail.php" method="post"enctype="multipart/form-data">

    <div class="container">
<form>

            <?php   
            $result = mysqli_query($link,"SELECT TreatmentName FROM treatment where Medaid = '".$MedAid."'");
                                if ($result) {

                                while($row = mysqli_fetch_array($result)){
                                    echo "<input type='checkbox' name='codes[]' value='".$row['TreatmentName']."'>"
                                        .$row['TreatmentName'] ;
                                     echo "<h1></h1>";

                                }
                                    ?>


                                                    Message: <textarea name="message" placeholder="Your Message.."></textarea>
                                        <h1></h1>
                                        <label for='uploaded_file'>Select A File To Upload:</label>
                                        <input type="file" name="uploaded_file">
                                        <h1></h1>
                                        <input type="submit" name="submit" value="Submit" />
  <h1></h1> <a href=\billing\>click here to Return to the main Login Screen</a>
    </form>
                                          <?php             


                                }



if ($MedAid == 'GEMS'){




                              ?>

                                          <?php             




                                }

                                }



        else
        {
            echo "<h1>Error</h1>";
            echo "<p>Sorry, Your patient was not added. Please go back and try again.</p>";    

            echo " ".$MedaidNumber.", ".$Name.", ".$SurName.", ".$MedAid.", ".$PracticeNum." ,".$Date."";


        }       
     }
}
else
{
    ?>



    <?php
}
   elseif 

   (empty($_SESSION['LoggedIn']) && empty($_SESSION['Username']))
   if(empty($_POST['Name']) && empty($_POST['SurName']))
   echo "<h1> no Success</h1>";
    // echo "<a href=\billing\index.php>click here to log in</a>";
        if(empty($_POST['Name']) && empty($_POST['SurName']))
   echo "<h1> no Success post</h1>";


?>
</body>
</html>

Its a silly mistake that cost a lot of time.

M-Corp
  • 137
  • 1
  • 3
  • 15