Jump to content

Recommended Posts

I have an form , which has a few fields, when filled, the form calls a php file called insert.php which inserts the records from the form into a database and also what I'm trying to is send an email of the records within the same script. I'm new to php can anyone help. below is the code.

 

<?php

$con = mysql_connect("localhost","me","mypassword");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("final_project", $con);

 

$sql="INSERT INTO Persons (Username,FirstName, LastName, Department,Issue,Category)

VALUES

('$_POST[username]', '$_POST[Firstname]','$_POST[Lastname]','$_POST[Department]','$_POST[issue]','$_POST[Category]')";

 

if (!mysql_query($sql,$con))

  {

  die('Error: ' . mysql_error());

  }

mysql_close($con)

 

$email = $HTTP_POST_VARS['email'];

$Firstname = $HTTP_POST_VARS['Firstname'];

$Lastname = $HTTP_POST_VARS['Lastname'];

$Issue = $HTTP_POST_VARS['Issue'];

$Category = $HTTP_POST_VARS['Category'];

 

if( mail($email,$Firstname,$Lastname,$Issue,$Category)){

print ("your mail was sucessfully sent to $email");

}else

print "unable to send mail to $to";

 

echo "<br>";

 

echo "You records have been updated";

 

?>

 

i concur with jay that http_post_vars may be deprecated, i do not understand that you used $_POST for database insertion and not for storing into variables, and you are doing double the work, here is how i would code it:

<?php
$con = mysql_connect("localhost","me","mypassword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("final_project", $con);

$username = $_POST['Username'];
$email = $_POST['email'];
$Firstname = $_POST['Firstname'];
$Lastname = $_POST['Lastname'];
$Issue = $_POST['Issue'];
$Category = $_POST['Category'];
$Dept = $_POST[Department];

$sql="INSERT INTO Persons (Username,FirstName, LastName, Department,Issue,Category)
VALUES
('".$username."', '".$Firstname."','".$Lastname."','".$Dept."','".$Issue."','".$Category."')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con)


if( mail($email,$Firstname,$Lastname,$Issue,$Category)){
print ("your mail was sucessfully sent to $email");
}else
print "unable to send mail to $email";

echo "<br>";

echo "You records have been updated"; 

?>

 

check your code against this code. You had a $to variable which was not assigned anything, you should also sanitize your inputs, use functions such as trim, addshashes and strip_tags, hope you get it sorted

You also did not provide any clue as to what your code is actually doing. Is it outputting the "your mail was successfully sent ..." message or the "unable to send mail ..." message?

 

For debugging purposes, add the following two lines of code immediately after the first opening <?php tag on the page to get php to show all errors it detects when your code runs -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

Thanks for your help so far guys. The aim for the code is to insert the data from the form into the database, then send an email with all of the data from the form to the email variable in the form field. So far the data is inserted into the database and if statement seems to run and tells me it is succesful, but i recieve no email.

dude, you're doing the mail wrong

check this link: http://php.net/manual/en/function.mail.php

 

mail($email,$Firstname,$Lastname,$Issue,$Category)

should be:

$msg = $Firstname.'\n'.$Lastname.'\n'.$Issue.'\n'.$Category;
$subj = "Posted data";
mail($email, $subj, $msg)

 

that should work

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.