After the customer fills the form, the form data is sent to mysql and an email gets sent to me with the last form data that the customer submitted. But, the email "last mysql data" is not going as inline text. Please help me. Sample code is given below.

`

<?php
define('DB_NAME', 'XXXXXXX');
define('DB_USER', 'XXXXXXX');
define('DB_PASSWORD', 'XXXXXXX');
define('DB_HOST', 'localhost');
$link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
//Start Posting the data in Mysql database from Form Input
$value = $_POST['input1'];
$value2 = $_POST['MAmount'];
$sql = "INSERT INTO demo (input1, MAmount) VALUES ('$value', '$value2')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
//start print the database
$data = mysql_query("SELECT * FROM demo ORDER BY ID DESC LIMIT 1")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>ID:</th> <td>".$info['ID'] . "</td> ";
Print "<th>Input1:</th> <td>".$info['input1'] . "</td> ";
Print "<th>MAmount:</th> <td>".$info['MAmount'] . " </td></tr>";
}
Print "</table>";
mysql_close();
//end print the database on form processing page
//start emailing the data
date_default_timezone_set('Asia/Kolkata');
require_once('class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer();
//$body = "gdssdh";
//$body = preg_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://XXXXXXX.XXXXXXX.org"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = " https://www.astaguru.com/ "; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "XXXXXXX.com"; // GMAIL username
$mail->Password = "XXXXXXX"; // GMAIL password
$mail->SetFrom('contact@XXXXXXXX.com', 'HAL');
//$mail->AddReplyTo("XXXXXXX', 'First Last");
$mail->Subject = "XXXXXXX";
//THE PROBLEM IS HERE WHEN I WANT TO SEND THE DATA AS INLINE TEXT TO EMAIL FROM MYSQL IT IS NOT WORKING. ONLY "PRINT THE DATA" IS SENDING TO EMAIL.
$body = 'Print the data';
mysql_connect("localhost","XXXXXXX","XXXXXXX");
@mysql_select_db("XXXXXXX");
$query["SELECT * FROM demo ORDER BY ID DESC LIMIT 1"];
$result = mysql_query($query);
//while ($row = mysql_fetch_array ($result)) {
// $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "XXXXXXX";
$mail->AddAddress($address, "user2");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>

`

It's incredibly hard to read your code because it is not properly indented.

That aside, a quick glance lets me see you are using PHP's deprecated mysql library instead of its replacement, the mysqli library. Additionally, you have a serious MySQL injection bug on line 16. You absolutely want to use real_escape_string() as so (my example code uses the MySQL instead of the MySQLi library, even though it's defunct):

$value = mysql_real_escape_string($_POST['input1'], $link);
$value2 = mysql_real_escape_string($_POST['MAmount'], $link);
$sql = "INSERT INTO demo (input1, MAmount) VALUES ($value, $value2)";

Now as far as your bug, I'm confused by your question. On line 56, you have $body = 'Print the data'; and then on line 63, you have $mail->MsgHTML($body); so only "Print the data" is going into the email message body. Instead, you can replace line 56 with something like: $body = "The new data entered is input $value with amount $value2";

Looks like your email issue is causing more drama than a bad Wi-Fi connection! The problem is in $query["SELECT FROM demo ORDER BY ID DESC LIMIT 1"];—this syntax is invalid. Fix it to $query = "SELECT FROM demo ORDER BY ID DESC LIMIT 1";. Also, fetch the data properly and append it to $body like this:

$row = mysql_fetch_array($result);
$body = "ID: {$row['ID']}, Input1: {$row['input1']}, MAmount: {$row['MAmount']}";

Here's the code I've been using for about 15 years now - but a bit more simplified:

function dbConnect($type = ''){
    $conn = mysqli_connect( DBHOST, DBUSER, DBPASS, DBDB);
    if (!$conn) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }
    return $conn;
}
function dbQuery($con,$Q){
    $R = mysqli_query($con,$Q) or die('Error with Query:'.$Q.' Error:'.mysqli_error($con));
    return $R;
}
function dbCheckRows($R,$KEY = false){
    //$data = $R->fetchAll(PDO::FETCH_ASSOC);
    while($row = mysqli_fetch_assoc($R)){
        $data[] = $row;
    }
    if(isset($data)){
        $D = array();
        if($KEY != false){
            foreach($data as $k=>$v){
                $D['data'][$v[$KEY]] = $v;
            }
        }else{
            $D['data'] = $data;
        }
        if(empty($D['data'])){
            $D['rows'] = 0;
        }else{
            $D['rows'] = count($D['data']);
        }
    }else{
        $D['rows'] = 0;
        $D['data'] = false;
    }
    //var_dump($D);
    return $D;
}

To use it you just do:

$DB = dbConnect();

$Q1 = "SELECT 1 `id`, 'test' `str`;";
$R1 = dbQuery($DB,$Q1);
$D1 = dbFetchAssoc($R1, false);

var_dump($D1);

maybe I should of copywrited it..

my guess is you should escape the query better as you have a column called ID and this is probably an SQL keyword:

SELECT * FROM `demo` ORDER BY `ID` DESC LIMIT 1

and the above mysql connection code should solve any other problem you have on the SQL side

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.