sql error 'query was empty' - mysql

I am not a trained programmer. Whatever I learned is by myself from the net. Please excuse if you find my code and question very amateurish.
I am trying to call matches of buyers against sellers data from the database and automatically mail them their matches.
When I run this code on my system with EasyPhp, it says 'Query was empty". And when I run this on my server, it says "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 '1' at line 1 " where am I going wrong ?
<?php
include('adminmaster.htm');
Echo "<center>Mail Matching Properties</center><br><br>";
mysql_connect("localhost", "username","password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$query= 'SELECT*FROM clientdata WHERE email != " " AND Transaction="Buyer"';
$result=mysql_query($query);
echo mysql_error();
if (!$result){ die("No Buy results");}
$matchrows=mysql_num_rows($result);
if($matchrows==0){ die("No Buy results");}
while($info = mysql_fetch_array($result))
$Price1= $info['Price']+($info['Price']*10/100);
$Price2 = $info['Price']-($info['Price']*10/100);
$una=$info['username'];
$query2='SELECT*FROM clientdata WHERE email!=" " AND Transaction="Seller"';
$result2=mysql_query($query2);
echo mysql_error();
$matchrows2=mysql_num_rows($result2);
if($matchrows2==0){ die("Sell Data Not Found");}
if(!$result2)
{die("Sell Data Not Found");}
while($info2=mysql_fetch_array($result2))
{
if ($info['Detail']==$info2['Detail'])
{
if ($info['Location']==$info2['Location'])
{
if ($Price1 >= $info2['Price'])
{
if ($Price2 <= $info2['Price'])
{
if ($info['username']!=$info2['username'])
{
echo mysql_error();
echo "<table width=600 border = 1 style='text-align:left';>
<tr>
<td><b>Clients</b></td>
<td><b><font color='maroon'>BUYER</font></b></td>
<td><b><font color='maroon'>SELLER</font></b></td>
</tr>
<tr>
<td><b>Date Posted:</b></td>
<td>$info[Date]</td>
<td>$info2[Date]</td>
</tr>
<tr>
<td><b>Title:</b></td>
<td>$info[propheading]</td>
<td>$info2[propheading]</td>
</tr>
<tr>
<td><b>Transaction:</b></td>
<td>$info[Transaction]</td>
<td>$info2[Transaction]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Detail:</b></td>
<td style='background-color:lightgrey'>$info[Detail]</td>
<td style='background-color:lightgrey'>$info2[Detail]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Price:</b></td>
<td style='background-color:lightgrey'>$info[Price]</td>
<td style='background-color:lightgrey'>$info2[Price]</td>
</tr>
<tr>
<td><b>Additional Data:</b></td>
<td>$info[AdditionalData]</td>
<td>$info2[AdditionalData]</td>
</tr>
<tr>
<td><b>Location:</b></td>
<td>$info[Location]</td>
<td>$info2[Location]</td>
</tr>
<tr>
<td><b>Agent/Individual:</b></td>
<td>$info[Agent_Ind]</td>
<td>$info2[Agent_Ind]</td>
</tr>
<tr>
<td><b>Name:</b></td>
<td>$info[Title] $info[firstname] $info[lastname]</td>
<td>$info2[Title] $info2[firstname] $info2[lastname]</td>
</tr>
<tr>
<td><b>Phone:</b></td>
<td>$info[Phone]</td>
<td>$info2[Phone]</td>
</tr>
<tr>
<td><b>Email:</b></td>
<td>$info[Email]</td>
<td>$info2[Email]</td></tr>
</table>";
$tomail=$info2['Email'];
if ($tomail=="anitgopinath#gmail.com"|$tomail=="anit#landshoppe.com"|$tomail=="info#landshoppe.com") {continue;}
$headers ="From:info#landshoppe.com"."\r\n";
$headers .='Bcc:anit#landshoppe.com'."\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$subject ="You have a Property Match !";
$message="<html></head><body>
<table width=600 border = 1 style='text-align:left';>
<caption><b><font color='maroon'>YOUR PROPERTY MATCH!</font></b></caption>
<tr>
<td><b>Date Posted:</b></td>
<td>$info[Date]</td>
</tr>
<tr>
<td><b>Title:</b></td>
<td>$info[propheading]</td>
</tr>
<tr>
<td><b>Transaction:</b></td>
<td>$info[Transaction]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Detail:</b></td>
<td style='background-color:lightgrey'>$info[Detail]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Price:</b></td>
<td style='background-color:lightgrey'>$info[Price]</td>
</tr>
<tr><td><b>Additional Data:</b></td><td>$info[AdditionalData]</td></tr>
<tr><td><b>Location:</b></td><td>$info[Location]</td></tr>
<tr><td><b>Agent/Individual:</b></td><td>$info[Agent_Ind]</td></tr>
<tr><td><b>Name:</b></td><td>$info[Title] $info[firstname] $info[lastname]</td></tr>
<tr><td><b>Phone:</b></td><td>$info[Phone]</td></tr>
<tr><td><b>Email:</b></td><td>$info[Email]</td></tr>
</table></body><html>";
echo $tomail."<br>";
echo "$message"."<br>";
$mailresult=mysql_query(mail($tomail,$subject,$message,$headers));
if ($mailresult){echo "Mailed<br>";}else{echo "Not Mailed !<br>";}
echo "**********************************Seller Match**************************************************<br>";
$tomail2=$info['Email'];
if ($tomail2=="anitgopinath#gmail.com"|$tomail2=="anit#landshoppe.com"|$tomail2=="info#landshoppe.com"){continue;}
$headers ="From:info#landshoppe.com"."\r\n";
$headers .='Bcc:anit#landshoppe.com'."\n";
$headers .= 'MIME-Version: 1.0'."\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
$subject ="You have a Property Match !";
$message="<html></head><body>
<table width=600 border = 1 style='text-align:left';>
<caption><b><font color='maroon'>YOUR PROPERTY MATCH!</font></b></caption>
<tr>
<td><b>Date Posted:</b></td>
<td>$info2[Date]</td>
</tr>
<tr>
<td><b>Title:</b></td>
<td>$info2[propheading]</td>
</tr>
<tr>
<td><b>Transaction:</b></td>
<td>$info2[Transaction]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Detail:</b></td>
<td style='background-color:lightgrey'>$info2[Detail]</td>
</tr>
<tr>
<td style='background-color:lightgrey'><b>Price:</b></td>
<td style='background-color:lightgrey'>$info2[Price]</td>
</tr>
<tr>
<td><b>Additional Data:</b></td>
<td>$info2[AdditionalData]</td>
</tr>
<tr>
<td><b>Location:</b></td>
<td>$info2[Location]</td>
</tr>
<tr>
<td><b>Agent/Individual:</b></td>
<td>$info2[Agent_Ind]</td>
</tr>
<tr>
<td><b>Name:</b></td>
<td>$info2[Title] $info2[firstname] $info2[lastname]</td>
</tr>
<tr>
<td><b>Phone:</b></td>
<td>$info2[Phone]</td>
</tr>
<tr>
<td><b>Email:</b></td>
<td>$info2[Email]</td>
</tr>
</table></body><html>";
echo $tomail2."<br>";
echo "$message"."<br>";
$mailresult2=mysql_query(mail($tomail2,$subject,$message,$headers));
if ($mailresult2){echo "Mailed<br>";}else{echo "Not Mailed !<br>";}
echo "**********************************Buyer Match**************************************************<br>";
}
}
}
}
}
}
}
?>

First of all, when specifying string literals within SQL request you should use single qoutes:
$query= "SELECT*FROM clientdata WHERE email != ' ' AND Transaction='Buyer'";

Don't want to sound rude, but you better redesign your code, it's a mess right now. Start with a simple 'divide and conquer' method: mark each block of code that does ONE and only ONE thing - and separate into a single function (leaving only its call in the main script). You'll instantly notice the difference it'll make in both reading and updating your code.
The error you spoke about is caused by this line:
$mailresult=mysql_query(mail($tomail,$subject,$message,$headers));
... as the result of mail function is boolean. And when converted into string by mysql_query, it's either an empty string or '1'; both of them, of course, are not valid queries. If you need to check the result, it's just...
$mailresult=mail($tomail,$subject,$message,$headers);
And again, the error itself is simple - but it's hidden in your code so good that five or more SO users failed to find it. ) That's why the code needs restructuring.

Related

Table HTML Code in Inbox Mail from Contact Form

I have a code content of the email body contact form as shown below:
"Here are the book details:\n Book Title: $title \n Writer: $writer \n Publisher: $publisher \n Sender: $name \n Email: $email"
if incoming email inbox result as below:
Here are the book details:
Book Title: ...
Writer: ...
Publisher: ...
Sender: ...
Email: ...
What code should I add when the incoming email inbox results are as follows:
<table>
<tr>
<td class="kiri">Book Title</td>
<td class="kanan">........</td>
</tr>
<tr>
<td class="kiri">Writer</td>
<td class="kanan">.......</td>
</tr>
<tr>
<td class="kiri">Publisher</td>
<td class="kanan">.......</td>
</tr>
<tr>
<td class="kiri">Sender</td>
<td class="kanan">....... (email)</td>
</tr>
</table>
as i used jsfiddle
, i saw the table is also the same as the /n one. But, still you can try
"<table>
<tr>
<td class="kiri">Book Title</td>
<td class="kanan">" .$booktitle . "</td>
</tr>
<tr>
<td class="kiri">Writer</td>
<td class="kanan">" . $bookwriter . "</td>
</tr>
</table>"

Creating table layout

I have created a php file called search.php. I want to show the result on a table
I want a table structure like this.
__________________________
| |________________|
| |________________|
| |________________|
|________|________________|
|________________|
|________________|
On the left side I want to place a picture which will be obtained from database and to the right I will place information about the student photo.
Could you write a code. Can we separate photo and the information column by a vertical line only.(if yes,HOW ?? )
<table>
<tr>
<td rowspan="4" style="border-right: 1px solid black">
<img src"whatever.png" alt="" />img
</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td>4</td>
</tr>
<tr>
<td rowspan="2">
</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
</tr>
</table>
but really: learn html an get used to the boxed model, that is: use divs
<?php
$sql = "Select FROM * `yourtablename` where `id` = 'theidofthestudent'";
if(!mysql_query($sql)){
die(mysql_error());
} else {
echo "<table border="1px" style="text-align:center;" align="justify">";
echo "<th>Photo of student</th>";
echo "<th>Name of the student</th>";
echo "<th>Student Detials</th>";
while($row = mysql_fetch_array($sql)){
echo "<tr>";
echo "<td style="float:left;">' . $row['student_pic'] . "</td>";
echo '<td style="float:right;">' . $row['student_name'] . "</td>";
echo '<td style="float:right;">' . $row['student_details'] . "</td>";
echo "</tr>"
} echo "</table>";
}
?>

HTML EMAIL from PHP not parsing correctly

I am working on a splash page which contains a form. When the form is Submitted my add.php writes the form input fields to a SQL server, then it is supposed to send an html email to the person who filled out the form.
Issue #1: The email is received, but it displays the html markup; not the formatted version of the email.
Issue #2: The "From" field is being populated with "text/html" and displays the webserver: tribeca.websitewelcome.com. It is not showing the specified email.
Here is the contents of add.php
<?php
$name=$_POST['Name'];
$email=$_POST['Email'];
$to = $_POST['Email'];
$subject = 'Company Name Webinar Contact';
$message = '<!DOCTYPE html>
<html>
<head>
<title>Email</title>
</head>
<body style="background-color: #e9e9e9;">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr> <td style="width:600px;"> </td> </tr>
<tr> <td> <table cellspacing="0" cellpadding="0" width="600px" border="0" style="background-color: #fff;margin:0 auto;border:solid 10px #fff;font-family: lucida grande, sans-serif;">
<tr> <td style="height:100px;"> <center> <img src="eq-logo.jpg" /> </center> </td> </tr>
<tr style="height:1px; background-color:#dcdcdc"> <td > </td> </tr>
<tr style="height:1px; background-color:#f1f5ee"> <td > </td> </tr>
<tr style="height:100px;"> <td > <h1 style="color:#e10621;">Thank you for your enquiry!</h1> <h3>Someone from our team will contact you shortly.</h3> <p style="line-height:28px;">We've helped hundreds achieve their financial dreams and look forward to showing YOU how to turn YOUR common "cents" into dollars. Please take a few moments to check out the other areas of our website. </p> <p><b>The Company Name Team</b></p> </td> </tr>
</table> </td> </tr>
<tr> <td> <center> <img src="bottom-shadow.png" /> </center> </td> </tr>
</table>
</body> </html>
';
$headers = 'MIME-Version: 1.0' . '\r\n';
$headers .= 'Content-type:text/html;charset=iso-8859-1' . '\r\n';
$headers .= 'From: <donotreply#companyname.com.au>' . "\r\n";
// Connect
mysql_connect("localhost", "db_user", "user_password") or die(mysql_error()) ;
mysql_select_db("db_name") or die(mysql_error()) ;
// Write
mysql_query("INSERT INTO `table_name` VALUES ('$name', '$email')") ;
// Email
mail($to,$subject,$message,$headers);
header('Location: http://companyname.com.au');
exit();
?>
Additional Notes:
I have tried separating the 'message' out into line by line html as in chris coyiers html email tut with no luck. Here you see the html in its entirety in the message area.
It's your quotes! Use double quotes for your \r\n lines:
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type:text/html;charset=iso-8859-1' . "\r\n";
$headers .= 'From: <donotreply#companyname.com.au>' . "\r\n";

Multiple Headers Occur

This is supposed to be a simple high scores table. but everytime it pulls another player from the database it looks like this.
(table headers)
player
(table headers)
player
(table headers)
player
and so on you get the picture... I want it to just show the header one time at the top of the list of players... I had it working at one time and don't remember what I changed that caused it to do this, does anyone know how to fix it?
Here is my code:
<?php
$result = mysql_query("SELECT * FROM users ORDER BY explores DESC LIMIT 20;")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
?>
<table class="reg-box" style="width: 600px; text-align: center;" align="center" cellspacing="5">
<tbody>
<tr>
<th colspan="2">Trainer</th>
<th colspan="2">Explores</th>
</tr>
<tr>
<td colspan="2"><?php echo "".$row['username'].""; ?></td>
<td colspan="2"> <?php echo $row['explores']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
The problem with your code was the table is getting created in the loop, leading to the multiple headers. Instead remove the creation of the table from the while loop and then try to update all the result set you get from the query you fired in a loop. Try this.
$result = mysql_query("SELECT * FROM users ORDER BY explores DESC LIMIT 20;")
or die(mysql_error());
?>
<table class="reg-box" style="width: 600px; text-align: center;" align="center" cellspacing="5">
<tbody>
<tr>
<th colspan="2">Trainer</th>
<th colspan="2">Explores</th>
</tr>
<?php
while($row = mysql_fetch_array( $result )) {
<tr>
<td colspan="2"><?php echo "".$row['username'].""; ?></td>
<td colspan="2"> <?php echo $row['explores']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

PHP Array doesn't show all the data from database

I have a very basic and simple script that should display records from my database. The problem: it doesn't show all the records. I've tried it even with the most simple mysql
($sql="SELECT * FROM $tbl_name";) but still some records are missing (mostly the first of the list that isn't shown).
So here is my code (it's all on 1 page):
<?php
$host="localhost";
$username="***";
$password="***";
$db_name="***";
$tbl_name="***";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE rowNameOne >= 0.01 AND rowNameTwo='2013'";
if ($_GET['sort'] == 'one')
{
$sql .= " ORDER BY one ASC";
}
elseif ($_GET['sort'] == 'two')
{
$sql .= " ORDER BY two ASC";
}
elseif ($_GET['sort'] == 'three')
{
$sql .= " ORDER BY three ASC";
}
elseif($_GET['sort'] == 'four')
{
$sql .= " ORDER BY four ASC";
}
elseif($_GET['sort'] == 'five')
{
$sql .= " ORDER BY five ASC";
}
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<body onload="parent.alertsize(document.body.scrollHeight);">
<br />
<table cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" colspan="5">
<font>Titel</font>
</td>
<tr>
<td>Titel one</td>
<td>Titel two</td>
<td>Titel three</td>
<td>Titel four</td>
<td>Titel five</td>
</tr>
<tr>
<td colspan="5" class="noBorder">
<?php
while($rows=mysql_fetch_array($result)){
?>
<a href="pageName.php?id=<? echo $rows['id']; ?>" >
<table width="100%">
<tr>
<td><? echo $rows['rowNameOne']; ?></td>
<td><? echo $rows['rowNameTwo']; ?></td>
<td><? echo $rows['rowNameThree']; ?></td>
<td><? echo $rows['rowNameFour']; ?></td>
<td><? echo $rows['rowNameFive']; ?></td>
</tr>
</table>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
<?php
}
?>
</a>
</td>
</tr>
</table>
It's a very basic code, easy as can be I would say, but still it's missing records, not displaying everything that's in the database. What am I doing wrong?
Thanks for the help!
Before you start the loop, you do this:
$rows=mysql_fetch_array($result);
Then the loop condition is:
while($rows=mysql_fetch_array($result)){
So the first result is never shown. I would advice to remove the first statement, since you're not using its results between that statement and the loop.
On a related note, please consider moving to PDO or mysqli.