Record won't update MySQL with PHP - php

I cannot for the life of me figure out why the below will not update the record. It gives me a success message but does not actually update the record. I have gone through tons off revisions and tried everything I can think of. Can anyone else see where the issue is?
<?php
$id = $_POST['id'];
$username = $_POST['username'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$company = $_POST['company'];
$rep1 = $_POST['rep1'];
$rep2 = $_POST['rep2'];
$rep3 = $_POST['rep3'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$cell = $_POST['cell'];
$service = $_POST['service'];
$license = $_POST['license'];
$expdate = $_POST['expdate'];
$active = $_POST['active'];
$userlevel = $_POST['userlevel'];
$host="XXXXXXX"; // Host name
$username="XXXXXX"; // Mysql username
$password="XXXXXXX"; // Mysql password
$db_name="XXXXXXX"; // Database name
$tbl_name="users"; // Table name
// Connect to server and select database.
$con = mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect");
// update data in mysql database
$sql="UPDATE $tbl_name SET id='".$id."', fname='".$fname."', lname='".$lname."', email='".$email."', company='".$company."', rep1='".$rep1."', rep2='".$rep2."', rep3='".$rep3."', phone='".$phone."', fax='".$fax."', cell='".$cell."', service='".$service."', license='".$license."', expdate='".$expdate."', active='".$active."', userlevel='".$userlevel."'".
"WHERE id = '".$id."'";
$result=mysqli_query($con,$sql);
// if successfully updated.
if($result){
$emailID = "info#domain.com";
$subject = "Registration notification from. $fname . through website";
$body = <<<EOD
<table cellspacing="0" cellpadding="1" border="1">
<tbody>
<tr>
<td style="padding: 5px 10px;" width="150">Name: </td>
<td style="padding: 5px 10px;">$fname $lname</td>
</tr>
<tr>
<td style="padding: 5px 10px;" width="150">Mobile: </td>
<td style="padding: 5px 10px;">$cell</td>
</tr>
<tr>
<td style="padding: 5px 10px;" width="150">Email: </td>
<td style="padding: 5px 10px;">$email</td>
</tr>
<tr>
<td style="padding: 5px 10px;" width="150">License Number: </td>
<td style="padding: 5px 10px;">$license</td>
</tr>
</tbody>
</table>
EOD;
$headers = "From: info#domain.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n";
mail($emailID, $subject, $body, $headers );
echo "<h4>Thank you for updating your account info. We will authorize your account and notify you once we have verified your license number and expiration.</h4>";
}
else {
echo "ERROR";
}
?>

Your last value doesn't have a whitespace after it, so the where keyword is "stuck" to it.
Just add a space before the where and you should be OK:
$sql="UPDATE $tbl_name SET id='".$id."', fname='".$fname."', lname='".$lname."', email='".$email."', company='".$company."', rep1='".$rep1."', rep2='".$rep2."', rep3='".$rep3."', phone='".$phone."', fax='".$fax."', cell='".$cell."', service='".$service."', license='".$license."', expdate='".$expdate."', active='".$active."', userlevel='".$userlevel."'".
" WHERE id = '".$id."'"; // Note the additional space here

Your problem is general: a mess of periods and extra quotes means you can't see litle things like the space you needed in the query. Also you don't have a mysql error generated on query fail, that would have helped.
Other problems: you're typing too much. Why make variables for each post value that have the same name as the key when you can just construct your query? Also you need to escape your values.
Here's my take:
$con = mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect");
$id=mysql_real_escape_string($id);
foreach($_POST as $k=>$v){
//exclude id
if($k!='id'){
//escape values
$escaped_value= mysql_real_escape_string($v);
//set up array for update query
$query_parts[]="$k='$escaped_value'";
//create variables with the same name as the key
${$k}=$v;
}
}
$sql="UPDATE $tbl_name SET ".implode(",",$query_parts)." $WHERE id = '$id' ";
$result=mysqli_query($con,$sql) or die(mysql_error());

Related

php to mysql data retrieval

I am trying to retrieve data from mysql database that has 4 rows. I need to send this as an table in email using php. I have built a script for this, but the issue is the script is not emailing all the rows, its just emailing the last row or if i pass a specific parameter with "where" in db query.
Any help from anyone is appreciated. Thank you in advance.
Attached the db output and php code as well.
<?php
date_default_timezone_set('America/Los_Angeles');
$today = date("j-F-Y g:i:s a"); // March 10, 2001, 5:16 pm
// DB Connect.
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = 'test123#'; // Password
$db_name = 'util'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = "select * from srvr1";
$result = mysqli_query($conn, $sql);
if (!$result) {
die ('SQL Error: ' . mysqli_error($conn));
}
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$File_system = $row[0];
$IP = $row[1];
$Capacity = $row[2];
$Available = $row[3];
$Used = $row[4];
$Percentage = $row[5];
# Compare Percentage and alert.
$subject = "Critical | FH NetApp-NetBackup Space Utilization..!";
$message1 = "
<html>
<body>
<p> Hi Team,<br><br> The Server utilization is <b style='color:red'> critical</b>. Please find the below utilization details.</p>
<table>
<tr>
<th> File_System </th>
<th> IP </th>
<th> Total_capacity </th>
<th> Available_Capacity </th>
<th> Used_Capacity </th>
<th> Percentage </th>
</tr>
<tr>
<td> $File_system </td>
<td> $IP </td>
<td> $Capacity </td>
<td> $Available </td>
<td> $Used </td>
<td> $Percentage </td>
</tr>
</table>
<p style='font-size:15px'> Data generated at:<b> $today EST.</b><p>
<p>Regards, <br>
Backup Team. </p>
</body>
</html>";
$headers[] = 'From: Srvr19utilization#util.com'; // Sender's Email
$headers[] = 'Cc: santosh.kowshik20#gmail.com'; // Carbon copy to Sender
$headers[] = 'MIME-Version: 1.0 charset=".$encoding."';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message1, 70);
// Send Mail By PHP Mail Function
mail($to, $subject, $message, implode("\r\n", $headers));
}
}
?>
I have been trying to shuffle around the loop in the code, it did not help.
Try and rearrange your code a little bit. First create the 'top' of the html, then go through all the results and append them to the html and then finish the html.
I haven't tested the code beneath but it should work :-)
<?php
date_default_timezone_set('America/Los_Angeles');
$today = date("j-F-Y g:i:s a"); // March 10, 2001, 5:16 pm
// DB Connect.
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = 'test123#'; // Password
$db_name = 'util'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn) {
die('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = "select * from srvr1";
$result = mysqli_query($conn, $sql);
if (!$result) {
die('SQL Error: ' . mysqli_error($conn));
}
if (mysqli_num_rows($result) > 0) {
$subject = "Critical | FH NetApp-NetBackup Space Utilization..!";
$message1 = "
<html>
<body>
<p> Hi Team,<br><br> The Server utilization is <b style='color:red'> critical</b>. Please find the below utilization details.</p>
<table>
<tr>
<th> File_System </th>
<th> IP </th>
<th> Total_capacity </th>
<th> Available_Capacity </th>
<th> Used_Capacity </th>
<th> Percentage </th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
$File_system = $row[0];
$IP = $row[1];
$Capacity = $row[2];
$Available = $row[3];
$Used = $row[4];
$Percentage = $row[5];
# Compare Percentage and alert.
$message1 .= "
<tr>
<td> $File_system </td>
<td> $IP </td>
<td> $Capacity </td>
<td> $Available </td>
<td> $Used </td>
<td> $Percentage </td>
</tr>";
}
$message1 .= "</table>";
$message1 .= "<p style='font-size:15px'> Data generated at:<b> $today EST.</b><p>
<p>Regards, <br>
Backup Team. </p>
</body>
</html>";
$headers[] = 'From: Srvr19utilization#util.com'; // Sender's Email
$headers[] = 'Cc: santosh.kowshik20#gmail.com'; // Carbon copy to Sender
$headers[] = 'MIME-Version: 1.0 charset=".$encoding."';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message1, 70);
// Send Mail By PHP Mail Function
mail($to, $subject, $message, implode("\r\n", $headers));
}
?>
About conditional formatting
No worries. You can try and use nested ternaries (shorthand for if/else... to keep it short :-))
Example snippet from above code:
...
$Percentage = $row[5];
$color = ($Percentage > 90) ? '#FCE901' : (($Percentage > 85) ? '#FF0000' : '#00E526');
# Compare Percentage and alert.
$message1 .= "
<tr>
<td> $File_system </td>
<td> $IP </td>
<td> $Capacity </td>
<td> $Available </td>
<td> $Used </td>
<td style=\"background-color:$color\"> $Percentage </td>
</tr>";
The issue is somewhere on your database end. Based on your var_dump output:
object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(6) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }
You only have 1 result being returned. You should start there. Maybe you're not referencing the table you think you are. Who knows right now, but you php looks fine. Investigate your DB
Your PHP is not fine stubben gave you the answer
<?php
date_default_timezone_set('America/Los_Angeles');
$today = date("j-F-Y g:i:s a"); // March 10, 2001, 5:16 pm
// DB Connect.
$db_host = 'localhost'; // Server Name
$db_user = 'root'; // Username
$db_pass = 'test123#'; // Password
$db_name = 'util'; // Database Name
$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$conn)
{
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = "select * from srvr1";
$result = mysqli_query($conn, $sql);
if (!$result)
{
die ('SQL Error: ' . mysqli_error($conn));
}
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$File_system = $row[0];
$IP = $row[1];
$Capacity = $row[2];
$Available = $row[3];
$Used = $row[4];
$Percentage = $row[5];
Above You have a Percentage from 1 Row of Data
Below you use that to decide the eMail subject - only 1 subject per email - so 1 row of data per email
# Compare Percentage and alert.
if($Percentage > 90)
{
$subject = "Critical | FH NetApp-NetBackup Space Utilization..!";
$message1 = "
<html>
<head>
<style>
body,h6 {
font-family: Segoe UI,Helvetica,courier;
}
th {
border: 1px solid black;
border-collapse: collapse;
background-color: #04D1E8;
padding: 10px;
text-align: center;
}
td {
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
text-align: center;
}
.cri {
background-color: #FF0000;
}
</style>
</head>
<body>
<p> Hi Team,<br><br> The Server utilization is <b style='color:red'> critical</b>. Please find the below utilization details.</p>
<table>
<tr>
<th> File_System </th>
<th> IP </th>
<th> Total_capacity </th>
<th> Available_Capacity </th>
<th> Used_Capacity </th>
<th> Percentage </th>
</tr>
<tr>
<td> $File_system </td>
<td> $IP </td>
<td> $Capacity </td>
<td> $Available </td>
<td> $Used </td>
<td class='cri'> $Percentage </td>
</tr>
</table>
<p style='font-size:15px'> Data generated at:<b> $today EST.</b><p>
<p>
Regards, <br>
Backup Team.
</p>
</body>
</html>
";
$headers[] = 'From: Srvr19utilization#util.com'; // Sender's Email
$headers[] = 'Cc: santosh.kowshik20#gmail.com'; // Carbon copy to Sender
$headers[] = 'MIME-Version: 1.0 charset=".$encoding."';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message1, 70);
// Send Mail By PHP Mail Function
mail($to, $subject, $message, implode("\r\n", $headers));
}
else if($Percentage > 85)
{
$subject = "Warning | FH NetApp-NetBackup Space Utilization..!";
$message1 = "
<html>
<head>
<style>
body,h6{
font-family: Segoe UI,Helvetica,courier;
}
th {
border: 1px solid black;
border-collapse: collapse;
background-color: #04D1E8;
padding: 10px;
text-align: center;
}
td {
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
text-align: center;
}
.war {
background-color: #FCE901;
}
</style>
</head>
<body>
<p> Hi Team,<br><br> The Server utilization is <b style='color:yellow'> above normal</b>. Please find the below utilization details.</p>
<table>
<tr>
<th> File_System </th>
<th> IP </th>
<th> Total_capacity </th>
<th> Available_Capacity </th>
<th> Used_Capacity </th>
<th> Percentage </th>
</tr>
<tr>
<td> $File_system </td>
<td> $IP </td>
<td> $Capacity </td>
<td> $Available </td>
<td> $Used </td>
<td class='war'> $Percentage </td>
</tr>
</table>
<p style='font-size:15px'> Data generated at:<b> $today EST.</b></p>
<p>
Regards, <br>
Backup Team.
</p>
</body>
</html>
";
$headers[] = 'From: Srvr19utilization#util.com'; // Sender's Email
$headers[] = 'Cc: santosh.kowshik20#gmail.com'; // Carbon copy to Sender
$headers[] = 'MIME-Version: 1.0 charset=".$encoding."';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message1, 70);
// Send Mail By PHP Mail Function
mail($to, $subject, $message, implode("\r\n", $headers));
}
else
{
$subject = "Normal | FH NetApp-NetBackup Space Utilization..!";
$message1 = "
<html>
<head>
<style>
body,h6{
font-family: Segoe UI,Helvetica,courier;
}
th {
border: 1px solid black;
border-collapse: collapse;
background-color: #04D1E8;
padding: 10px;
text-align: center;
}
td {
border: 1px solid black;
border-collapse: collapse;
padding: 10px;
text-align: center;
}
.nor {
background-color: #00E526;
}
</style>
</head>
<body>
<p> Hi Team,<br><br> The Server utilization is <b style='color:green'> normal. </b>Please find the below utilization details.</p>
<table>
<tr>
<th> File_System </th>
<th> IP </th>
<th> Total_capacity </th>
<th> Available_Capacity </th>
<th> Used_Capacity </th>
<th> Percentage </th>
</tr>
<tr>
<td> $File_system </td>
<td> $IP </td>
<td> $Capacity </td>
<td> $Available </td>
<td> $Used </td>
<td class='nor'> $Percentage </td>
</tr>
</table>
<p style='font-size:15px'> Data generated at:<b> $today EST.</b></p>
<p>
Regards, <br>
Backup Team.
</p>
</body>
</html>
";
$headers[] = 'From: Srvr19utilization#util.com'; // Sender's Email
$headers[] = 'Cc: santosh.kowshik20#gmail.com'; // Carbon copy to Sender
$headers[] = 'MIME-Version: 1.0 charset=".$encoding."';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message1, 70);
// Send Mail By PHP Mail Function
mail($to, $subject, $message, implode("\r\n", $headers));
}
}
}
?>

Fetch each client data and send it to email

I, My stack on something and really seems not get better.
Is it possible to have a script that fetch individually each client row data and send it to their email address?
Still trying but I can't find answer.
Anyone with great suggestions?
here is my code:
<?php
$sqlSelect = "SELECT
cl.client_name,
DATE(jb.date),
jb.job_number,
jb.repair,
st.site_name
FROM
job_cards jb
INNER JOIN
clients cl ON (cl.client_id = jb.client_id)
LEFT JOIN
sites st on (st.site_id = jb.site_id)
WHERE
jb.completed = 1
AND cl.client_id = jb.client_id
ORDER BY cl.client_name ASC";
$tresult = mysql_query($sqlSelect);
while($userData = mysql_fetch_assoc($tresult))
{
if($i%2==0)
$classname = 'evenRow';
else if($i%2==1)
?>
<tr class='<?php if(isset($classname)) echo $classname;?>'>
<td width=250>
<?php
//echo $userData['client_name)'];
echo mysql_real_escape_string ($userData['client_name]); ?>
</td>
<td width=100>
<?php echo mysql_real_escape_string ($userData['DATE(jb.date)']); ?>
</td>
<td width=50>
<?php echo mysql_real_escape_string($userData['job_number']);?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['site_name']);?>
</td>
<td>
<?php echo mysql_real_escape_string($userData['repair']);?>
</td>
<?php
$to .= "realconcept#usa.com";
$subject ="Monthly Reports for company";
$headers .='MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n";
$headers .= 'From: System Admin <mike#guardre.com>' . "\r\n";//<noreply#example.com>'
$message .="
Company Name : ".$userData['client_name']."\n".
"Date : ".$userData['DATE(jb.date)']."\n".
"Site : ".$userData['site_name']."\n".
"Description : ".$userData['repair']."\n";
if(mail('realconcept#usa.com', $subject, $message, $header))
$i++;
}
?>
**What is wrong with my code?**s

unable to receive email through contact form here is my code

I am unable to receive email.here is my code.and i am trying to find my error but i am unable to find.so plzz anybody help me to solve this one.i am doubtful too about my code.here is my code which i have in my php file.my mail()
functions return true and i have modified my xamp config files.
<?php
require("connection.php");
?>
<?php
if(isset($_REQUEST))
{
$first = $_REQUEST['firstname'];
$middle = $_REQUEST['middlename'];
$last = $_REQUEST['lastname'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['phone'];
$message =$_REQUEST['message'];
$query=" INSERT INTO `contact`(`id`,`first-name`, `middle-name`, `last-name`, `email`, `phone`, `message`) VALUES ('','$first','$middle','$last','$email','$phone','$message')";
$result = mysql_query($query);
if($result)
{
echo "data entered";
}
else
{
echo "data is not entered";
}
$to = "mehmood.asif31#gmail.com";
$subject = "Message From Contact Us Page";
$headers = "From:mehmood.asif31#gmail.com \r\n";
$headers .= "Bcc:mehmood.asif31#gmail.com \r\n";
$message = '<div style="margin:0 auto; padding:0px; width:800px">
<p style="font:bold 28px Arial, Helvetica, sans-serifl ; color:#006699; padding:0 0 0px 0; ">Feedback Message</p>
<div style=" margin:0 0 20px 0; font-family:Arial, Helvetica, sans-serif; font-size:15px; padding:10px; margin:0px; line-height:22px;">
<table>
<tr>
<td><strong>Name:</strong></td>
<td>'.$first.'</td>
</tr>
<tr>
<td><strong>Email ID:</strong></td>
<td>'.$email.'</td>
</tr>
<tr>
<td><strong>Phone No:</strong></td>
<td>'.$phone.'</td>
</tr>
<tr>
<td><strong>Message:</strong></td>
<td>'.$message.'</td>
</tr>
</table>
</div>
</div>';
//echo $message; exit;
mail($to, $subject, $message, $headers);
unset($_POST);
}
?>

PHP Variable not outputting in full

I'm working in PHP and I have a simple shopping cart which adds items when you click the add button.
Everything is collected in a variable called $cartOutput
When I echo it, it gives me everything in the cart as expected. Same with var_dump . Everything is there
However, when I try to put it in an email and send it off. It cuts off the first item. Can anyone think of why this might be?
Nothing filters it before it is put into the email. It is simply what is in the variable
here is an example...
// e.g of the php variable being assembled for each item
$cartOutput .= "<tr>";
$cartOutput .= "<td>" . $product_name . "</td>";
$cartOutput .= "<td>$" . $price . "</td>";
// emailing the variables off here
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$company = $_POST['company'];
$name = $_POST['name'];
$address = $_POST['address'];
$address2 = $_POST['address2'];
$commercialAdd = $_POST['commercial'];
$residentialAdd = $_POST['residential'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal_code = $_POST['postal_code'];
$email = $_POST['email'];
$special_instructions = $_POST['special_instructions'];
$date = date("Y/m/d");
$time = date("h:i:sa");
$to = "xxx#gmail.com";
$header = "Cc:xxx#somedomain.com \r\n";
$subject = "Email Order - $company ($date - $time)";
$message = <<<EOD
<h1>Email Order - $date - $time </h1>
<h3><strong><u>Company:</u></strong> $company</h3>
<h3><strong><u>Name:</u></strong> $Name </h3>
<h3><strong><u>Address:</u></strong> $address<br>
$address2</h3>
<h3><strong><u>Residential:</u></strong> $commercialAdd </h3>
<h3><strong><u>Commercial:</u></strong> $residentialAdd </h3>
<h3><strong><u>City:</u></strong> $city</h3>
<h3><strong><u>Province:</u></strong> $province</h3>
<h3><strong><u>Postal Code:</u></strong> $postal_code</h3>
<h3><strong><u>Phone Number:</u></strong> $phone</h3>
<h3><strong><u>Email:</u></strong> $email</h3>
<h3><strong><u>Special Instructions:</u></strong> $special_instructions</h3>
<table>
<thead>
<tr>
<th>Item</th>
<th>Price</th>
<th>Weight (Kg)</th>
<th>Qty</th>
<th>Subtotal</th>
<th></th>
</tr>
</thead>
<tbody>
$cartOutput
<tr>
<td class="totals"><strong>Total</strong></td>
<td class="totals"> </td>
<td class="totals">$weightTotal kg</td>
<td class="totals">$quantityTotal</td>
<td class="totals">$ $cartTotal</td>
<td class="totals"> </td>
<tr>
</tbody>
</table>
EOD;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
header("location: complete.php");
exit();
}
else {
echo "Order could not be sent. Please try again or contact our office for assistance";
}
}
Probably you are running into an email format issue. mail() requires the body to end each line in \r\n AND be less than 70 characters each. You will need to encode you HTML or include it as an attachment. See this set of instructions for an example.

How to embed HTML with PHP array in e-mail message?

I am trying to send an email whith the order details after visitor submit the order. I am trying to embed my order array result in a html tag but it is not working. Help please
senmail.php
<?php
$to = "member#yahoo.com";
$from = "sales#thesite.com";
$headers = "From: $from";
$subject = "Custom computer Order Confirmation Nunber_ $orderid";
$message = "
<html>
<body>
<p> This doesn't render well</p>
<table style="margin-left:50px; text-align:left;">
<tr>
<th style="width:20%; border-bottom:solid 1px #000;">Item ID</th>
<th style="width:40%; border-bottom:solid 1px #000">Description</th>
<th style="width:15%; border-bottom:solid 1px #000">Quantity</th>
<th style="width:20%; border-bottom:solid 1px #000">Unit Price</th>
</tr>
<?php
foreach($_SESSION["cart_array"]as $item):
$item_id=$item['part_id'];
$sql = mysqli_query($con,"SELECT * FROM product_description
WHERE product_id='$item_id' LIMIT 1");
While($row=mysqli_fetch_array($sql)){
$product_name=$row["name"];
}
?>
<tr>
<td><?php echo $item['part_id'] ?></td>
<td><?php echo $product_name ?></td>
<td><?php echo $item['quantity'] ?></td>
<td><?php echo $item['price'] ?></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>
";
$mailsent = mail($to, $subject, $message, $headers);
.........
?>
bored enough to fix it all for you:
<?php
$to = "member#yahoo.com";
$from = "sales#thesite.com";
$headers = "From: $from";
$subject = "Custom computer Order Confirmation Nunber_ $orderid";
$message = '
<html>
<body>
<p> This doesn\'t render well</p>
<table style="margin-left:50px; text-align:left;">
<tr>
<th style="width:20%; border-bottom:solid 1px #000;">Item ID</th>
<th style="width:40%; border-bottom:solid 1px #000">Description</th>
<th style="width:15%; border-bottom:solid 1px #000">Quantity</th>
<th style="width:20%; border-bottom:solid 1px #000">Unit Price</th>
</tr>';
foreach($_SESSION["cart_array"] as $item){
$item_id = $item['part_id'];
$sql = mysqli_query($con,"SELECT * FROM product_description
WHERE product_id='$item_id' LIMIT 1");
While ( $row = mysqli_fetch_array($sql) ){
$product_name = $row["name"];
}
$message .= '
<tr>
<td>'.$item['part_id'].'</td>
<td>'.$product_name.'</td>
<td>'.$item['quantity'].'</td>
<td>'.$item['price'].'</td>
</tr>';
}
$message .= ' </table>
</body>
</html>';
$mailsent = mail($to,$subject,$message,$headers);
//.........
?>
i only fixed the basics, this is still far from ideal
You cannot embed PHP code inside strings and magically have it execute.
The correct approach would be to store the message in a string and use a processing loop to add to it. Such a result would look like:
$msg = "Beginning of message\n";
foreach($_SESSION["cart_array"]as $item) {
$item_id=$item['part_id'];
$sql = mysqli_query($con,"SELECT * FROM product_description
WHERE product_id='$item_id' LIMIT 1");
while ($row = mysqli_fetch_array($sql)) {
$product_name = $row["name"];
$msg .= "<td>$product_name</td>";
// Other message parts go here
}
$msg .= "End of message\n";
Alternately, you can go a very terrible route and use something like eval, but because I discourage such behavior I will not be providing an example.
You're not escaping the quotes in $message. use ' or \" for html instead. Also you don't need the extra PHP tags in the middle of $message as you're still writing in PHP. Inside your foreach concatenate $message with .= and by the end it will be a string which you can submit with your email. I haven't tested it but I cleaned up your code and made the suggested changes below.
$message = "
<html>
<body>
<p> This doesn't render well</p>
<table style="margin-left:50px; text-align:left;">
<tr>
...
Should be
$message = "
<html>
<body>
<p> This doesn't render well</p>
<table style=\"margin-left:50px; text-align:left;\">
<tr>
...
Here you go
<?php
$to = "member#yahoo.com";
$from = "sales#thesite.com";
$headers = "From: $from";
$subject = "Custom computer Order Confirmation Nunber_ $orderid";
$message = "<html>
<body>
<p>This doesn't render well</p>
<table style=\"margin-left:50px; text-align:left;\">
<tr>
<th style=\"width:20%; border-bottom:solid 1px #000\">Item ID</th>
<th style=\"width:40%; border-bottom:solid 1px #000\">Description</th>
<th style=\"width:15%; border-bottom:solid 1px #000\">Quantity</th>
<th style=\"width:20%; border-bottom:solid 1px #000\">Unit Price</th>
</tr>";
foreach( $_SESSION["cart_array"] as $item )
{
$item_id = $item["part_id"];
$sql = mysqli_query( $con, "SELECT * FROM product_decsription WHERE product_id='".$item_id."' LIMIT 1;" );
if($row=mysqli_fetch_array($sql))
{
$message .= "
<tr><td>".$item['part_id']."</td>
<td>".$row['name']."</td>
<td>".$item['quantity']."</td>
<td>".$item['price']."</td></tr>";
}
}
$message .= "</table></body></html>";
$mailsent = mail( $to, $subject, $message, $headers );
?>
EDIT: the headers to use html need to be:
$headers = "From: ".$from."\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Categories