PHP script sending an empty email

Hello everyone,

I have a script for my web form which logs the data to DB and also emails me the data once submitted. When I fill out the form and hit send, the script logs the data to DB and sends me an email but the email is completely empty. I have fetched the array of the form elements and double checked my script but I am missing something for sure.Does anyone have any ideas?

function send_mail($table,$id){
    global $database;
    global $_MY_EMAIL_ADDRESS;
    $sql="SELECT * FROM {$table} WHERE id={$id}";
    $table="";
    $result_set=$database->query($sql);
    if($result_set===false || $database->num_rows($result_set)==0)
        return false;
    $row=$database->fetch_array($result_set);
    if($table=="one"){
        $table="
            <table>
                <tbody>
                    <tr>
                        <td>ADINIZ SOYADINIZ</td>
                        <td>{$row["isim"]}</td>
                    </tr>
                    <tr>
                        <td>EMAIL</td>
                        <td>{$row["sahis_email"]}</td>
                    </tr>
                    <tr>
                        <td>TELEFON</td>
                        <td>{$row["sahis_tel"]}</td>
                    </tr>
                    <tr>
                        <td>MESAJINIZ</td>
                        <td>{$row["mesaj"]}</td>
                    </tr>
                    <tr>
                        <td>Submitted at</td>
                        <td>{$row["created_at"]}</td>
                    </tr>
                </tbody>
            </table>
        ";
    }else if($table=="two"){
        $table="
        <table>
            <tbody>
                <tr>
                    <td>FİRMA ADI</td>
                    <td>{$row["firma"]}</td>
                </tr>
                <tr>
                    <td>BULUNDUĞUNUZ ŞEHİR</td>
                    <td>{$row["sehir"]}</td>
                </tr>
                <tr>
                    <td>YETKİLİ AD-SOYAD</td>
                    <td>{$row["yetkili"]}</td>
                </tr>
                <tr>
                    <td>EMAIL</td>
                    <td>{$row["email"]}</td>
                </tr>
                <tr>
                    <td>TELEFON</td>
                    <td>{$row["firma_tel"]}</td>
                </tr>
                <tr>
                    <td>BAYİLİK TÜRÜ SEÇİNİZ</td>
                    <td>{$row["bayilikturu"]}</td>
                </tr>
                <tr>
                    <td>Submited at</td>
                    <td>{$row["created_at"]}</td>
                </tr>
            </tbody>
        </table>
        ";
    }
    $subject="New email has been received @ ";
    $body="
    <html>
        <body>
            $table
        </body>
    </html>
    ";
    $headers = "MIME-Version: 1.0" . "\r\n" .
               "Content-type: text/html; charset=UTF-8" . "\r\n";
    return mail($_MY_EMAIL_ADDRESS,$subject,$body,$headers); 
}

?>

Where is $table defined as one.

if($table=="one")

Hi,

Table 1 is below. I also have another table where value is 2. I am using the same script for both forms on the same page. I am receiving the email with subject only. No contents…

    <div class="large-12 columns">

        <label>AD - SOYAD
        <input id="isim" class="input-top-margin required" name="isim" data-error="Adınız ve Soyadınızı belirtiniz." type="text" placeholder="Adınız ve soyadınızı belirtiniz.">
        </label>

        <label>EMAIL
        <input id="sahis_email" class="input-top-margin required" name="sahis_email" data-error="Geçerli bir email giriniz." type="email" placeholder="Email adresinizi belirtiniz.">
        </label>

        <label>TELEFON
        <input id="sahis_tel" class="input-top-margin required" name="sahis_tel" data-error="Telefon numaranızı eksiksiz giriniz." type="tel" placeholder="Telefon numaranızı belirtiniz.">
        </label>

        <label>MESAJINIZ
        <textarea id="mesaj" class="input-top-margin required" type="text" data-error="Mesajınızı giriniz." name="mesaj" placeholder="Lütfen mesajınızı yazınız."></textarea>
        </label>

        <input type="hidden" name="from-form" value="one" />

    </div>

How about changing the structure variable name $table to $content both in table building and within $body (NOT IF CONDITIONS) and for a test just return $body.

You are setting it here from the function call

function send_mail($table,$id){

Then setting it to empty

$table="";

Then looking for a value of “one”

if($table=="one")

By changing the building name

if($table=="one"){
        $content="

That should fix it.

Do you really have table names one and two?

Hello,

My DB tables are named “contact_requests” and “bayi_basvurusu” I used “one” and “two” in my form structure as hidden input names to differentiate the forms from each other and used them to send the incoming entries from the form to the correct MYSQL tables. I am no expert at this thing maybe there was a better way to do it (there probably is :smile: )I will make the changes right now and let you know how it goes. Btw, I appreciate the help.

Hello,

I made all the changes but I am not sure if I broke something as I have received the following error:

Error: Invalid Data given.You have an error in your SQL syntax; check
the manual that corresponds to your MySQL server version for the right
syntax to use near ‘WHERE id=3’ at line 1

With the previous script everything works fine but only it doesn’t send the data.

Hmm. I would send actual table names on processing the form based off the hidden post input… Adjust as needed.

if(isset($_POST['one'])):
    send_mail("contact_requests",$id);
endif;

if(isset($_POST['two'])):
    send_mail("bayi_basvurusu",$id);
endif;

Then would just change those IF conditions to look for those table names to define the output. Something like this.

function send_mail($table,$id){
    global $database;
    global $_MY_EMAIL_ADDRESS;
    $sql="SELECT * FROM $table WHERE id=$id";
    $content="";
    $result_set=$database->query($sql);
    if($result_set===false || $database->num_rows($result_set)==0)
        return false;
    $row=$database->fetch_array($result_set);
    if($table=="contact_requests"){
        $content="
            <table>
                <tbody>
                    <tr>
                        <td>ADINIZ SOYADINIZ</td>
                        <td>{$row["isim"]}</td>
                    </tr>
                    <tr>
                        <td>EMAIL</td>
                        <td>{$row["sahis_email"]}</td>
                    </tr>
                    <tr>
                        <td>TELEFON</td>
                        <td>{$row["sahis_tel"]}</td>
                    </tr>
                    <tr>
                        <td>MESAJINIZ</td>
                        <td>{$row["mesaj"]}</td>
                    </tr>
                    <tr>
                        <td>Submitted at</td>
                        <td>{$row["created_at"]}</td>
                    </tr>
                </tbody>
            </table>
        ";
    }else if($table=="bayi_basvurusu"){
        $content="
        <table>
            <tbody>
                <tr>
                    <td>F&#304;RMA ADI</td>
                    <td>{$row["firma"]}</td>
                </tr>
                <tr>
                    <td>BULUNDU&#286;UNUZ &#350;EH&#304;R</td>
                    <td>{$row["sehir"]}</td>
                </tr>
                <tr>
                    <td>YETK&#304;L&#304; AD-SOYAD</td>
                    <td>{$row["yetkili"]}</td>
                </tr>
                <tr>
                    <td>EMAIL</td>
                    <td>{$row["email"]}</td>
                </tr>
                <tr>
                    <td>TELEFON</td>
                    <td>{$row["firma_tel"]}</td>
                </tr>
                <tr>
                    <td>BAY&#304;L&#304;K TÜRÜ SEÇ&#304;N&#304;Z</td>
                    <td>{$row["bayilikturu"]}</td>
                </tr>
                <tr>
                    <td>Submited at</td>
                    <td>{$row["created_at"]}</td>
                </tr>
            </tbody>
        </table>
        ";
    }
    $subject="New email has been received @ ";
    $body="
    <html>
        <body>
            $content
        </body>
    </html>
    ";
    $headers = "MIME-Version: 1.0" . "\r\n" .
               "Content-type: text/html; charset=UTF-8" . "\r\n";
    return mail($_MY_EMAIL_ADDRESS,$subject,$body,$headers); 
}

Unfortunately no luck. Maybe I am making a mistake somewhere which is causing the problem but I can’t seem to find it.

My bad. Single quote the $id
$sql="SELECT * FROM $table WHERE id='$id'";
Getting query results with this change?

Hi,

I am little lost at the moment. Can you take a look at the code below. I have tried to incorporate what you have said but I guess I did not structure the the first part as you instructed.

My DB table names are “contact_requests” and “bayi_basvuru”

<?php require_once("database.php"); $output["success"]=false; $output["message"]="Nothing processed!"; if(!isset($_POST)){ $output["success"]=false; $output["message"]="Invalid Request"; goto end; } $sql=""; $table=""; if($_POST["from-form"]=="one"){ $table="contact_requests"; $sql="INSERT INTO contact_requests( isim, sahis_email, sahis_tel, mesaj, created_at )VALUES( '{$_POST["isim"]}', '{$_POST["sahis_email"]}', '{$_POST["sahis_tel"]}', '{$_POST["mesaj"]}', NOW() )"; }else if($_POST["from-form"]=="two"){ $table="bayi_basvuru"; $sql="INSERT INTO bayi_basvuru( firma_adi, sehir, faaliyet_alani, yetkili_adi, yetkili_email, yetkili_tel, firma_web, created_at )VALUES( '{$_POST["firma_adi"]}', '{$_POST["sehir"]}', '{$_POST["faaliyet_alani"]}', '{$_POST["yetkili_adi"]}', '{$_POST["yetkili_email"]}', '{$_POST["yetkili_tel"]}', '{$_POST["firma_web"]}', NOW() )"; } $result_set=$database->query($sql); if($result_set!==false){ send_mail($table, $database->insert_id()); $output["success"]=TRUE; $output = header('Location: http://www.insuladd.com.tr/zurb/iletisim/tesekkurler.php'); } else{ $output["success"]=false; $output["message"]="Error while processing your request! Try again later!"; } end: echo json_encode($output); if(isset($_POST['one'])): send_mail("contact_requests",$id); endif; if(isset($_POST['two'])): send_mail("bayi_basvurusu",$id); endif; function send_mail($table,$id){ global $database; global $_MY_EMAIL_ADDRESS; $sql="SELECT * FROM $table WHERE id='$id'"; $content=""; $result_set=$database->query($sql); if($result_set===false || $database->num_rows($result_set)==0) return false; $row=$database->fetch_array($result_set); if($table=="contact_requests"){ $content="
ADINIZ SOYADINIZ {$row["isim"]}
EMAIL {$row["sahis_email"]}
TELEFON {$row["sahis_tel"]}
MESAJINIZ {$row["mesaj"]}
Submitted at {$row["created_at"]}
"; }else if($table=="bayi_basvurusu"){ $content="
FİRMA ADI {$row["firma"]}
BULUNDUĞUNUZ ŞEHİR {$row["sehir"]}
YETKİLİ AD-SOYAD {$row["yetkili"]}
EMAIL {$row["email"]}
TELEFON {$row["firma_tel"]}
BAYİLİK TÜRÜ SEÇİNİZ {$row["bayilikturu"]}
Submited at {$row["created_at"]}
"; } $subject="New email has been received @ "; $body=" $content "; $headers = "MIME-Version: 1.0" . "\r\n" . "Content-type: text/html; charset=UTF-8" . "\r\n"; return mail($_MY_EMAIL_ADDRESS,$subject,$body,$headers);

OK so you are defining $table with DB table name… Remove those example lines I posted.


if(isset($_POST['one'])):
send_mail("contact_requests",$id);
endif;

if(isset($_POST['two'])):
send_mail("bayi_basvurusu",$id);
endif;
1 Like

Ok I did. So I have only used the second portion of your code starting with " function send_mail($table,$id){ " and also wrapped the id in single quotes like this : $sql=“SELECT * FROM $table WHERE id=‘$id’”;

Would that be all?

Ok solved. Actually I have realized another issue where I defined the global email, there was an underscore missing.

It seems to be working fine now. Many many thanks…

Ok solved. Actually I have realized another issue where I defined the global email, there was an underscore missing.

It seems to be working fine now. Many many thanks…Aha. The first form is working perfectly fine but when I submit the second form it says “Invalid data given” ???

Hard to say. Just go through some trouble shooting to pin down the issue. Double check DB fields for that second table and POST names in processing are as sent by the form. Double check that you are getting results inserted to DB and insert_id() is present.

Funny thing is I haven’t changed a single thing on the script other than what you have recommended. It was submitting the form before and now it doesn’t, all fields and ids match also.
Anyhow, I will keep diggin’ post the answer once I find out the issue here… Thanks…

Well what part is not working? You do understand that the code I posted in post# 11 was just a loose example explaining to define the real DB table names based off of your hidden input, not literal code to use. In any case you were already defining $table as the DB table name.

Looks like in the function we have different database table names or IF condition value check. Compare to what it is supposed to be and correct.

bayi_basvuru
bayi_basvurusu

Note that I got the name bayi_basvurusu from post# 5 that you made.

Fantastic news for me and kudos to you, it is working now :smiley: I had completely overlooked those two letters at the end. It is all fixed up now. Thanks a lot.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.