Jump to content

Send results of sql query via PHP


kmadden84

Recommended Posts

Hello everyone.

 

I'm learning, bare with me.

 

I'm trying to set up a serial number retrieval record.  Customer enters email in html form -> goes to my php page -> if email found, serial sent ->else echo"not found"

 

This is what I have so far.  Can't get it to work.  I don't think I have a firm enough grasp of the syntax yet.

 

<?php

  $db = mysql_connect("local","user","pass");
		if(!$db) die("Error connecting to MySQL database.");
		mysql_select_db("database" ,$db);

if($_POST['formSubmit'] == "Submit")

$varEmail = $_POST['formEmail'];
$to=$_POST['formEmail']; 
  
{ 
    require_once "Mail.php"; 
    
$email = mysql_query("SELECT Serial FROM PurchaseRecord WHERE Email LIKE '$varEmail'");
  
if ($email >="1")

function sendmail($varEmail){
    
   $host = "smtp.gmail.com"; 
   $username = "email login"; 
   $password = "email password"; 
   
   $subject = "Your  Serial Number"; 
   $from = "[email protected]"; 
   $body = "<table border='1'>;
   $body .=<th>Serial:</th>";
   
while($row = mysql_fetch_array($email)){    
  
  $body .="<tr>";
  $body .= "<td>{$email['Email']}</td>";
    }
     $body .="</table>";
  mysql_close($con);
   ";
  $headers = "From: [email protected]";
  mail($to,$subject,$body,$headers);
  echo "Mail sent to $to";
}
else
{
echo "Serial Not found please try again"

?>

 

Any help, advice, point in the right direction,would be much appreciated

 

Thanks

 

Kevin

 

MOD EDIT:

 . . . 

BBCode tags added.

Link to comment
https://forums.phpfreaks.com/topic/265286-send-results-of-sql-query-via-php/
Share on other sites

Here's what I see

 

$varEmail and $to contain the same value.

 

You should use mysql_real_escape_string on values before you use them in a query. It prevents SQL injection. You can google more about that, tons of material.

 

mysql_query will return a special, resource type variable, or boolean FALSE if it fails. For readability, you should check for that, rather than if it's >="1". You probably also want curly-braces for that conditional.

 

There's no need to make a sendmail function here. It's one-time use code. Take that out altogether.

 

If you want to check if any rows were found, use the function mysql_num_rows. That is probably what you want to verify is >= "1". Keep in mind though, surrounding 1 in double quotes makes it a string. Though PHP will still parse it as an integer, it's better to write it as >= 1

 

In your while loop, you've assigned your fetch'ed row to the $row variable, yet you try to access this using the $email variable, which still contains that special resource, not an array.

 

Finally, try this without trying to send an e-mail. Simply echo out your $body variable to the browser. Make sure it works up to here before trying to do behind-the-scene stuff, like sending an e-mail.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.