Hi, I'm new to this forum. I just started php and I've a small problem.
I just want to know if the following piece of code is the correct way to insert data into the database and at the same time, send e-mail to 2 different addresses.
Any help with this would appreciated. Thanks.
<?php
if ($submit == "click"){
$connection = mysql_connect ("", "", "");
if ($connection == false){
echo mysql_errno().": ".mysql_error()."<BR>";
exit;
}
$query = "insert into person_db values ('$Name','$Address','$UniversityCompany','$PositionCourseofstudy','$E_mail')";
$result = mysql_db_query ("person_db", $query);
if ($result){
echo "Success!";
}
else{
echo mysql_errno().": ".mysql_error()."<BR>";
}
mysql_close ();
}
else{
echo "
$recipient = "elely@nus.edu.sg,eleadc@nus.edu.sg";
$subject = "Software download Person details";
$message = "Hello!\n\n'$Name','$Address','$UniversityCompany','$PositionCourseofstudy','$E_mail'\n\n";
mail ($recipient, $subject, $message);
?>
Need some help with this piece of code
Moderator: General Moderators
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
3 things:
1. your mysql_connect needs non-null values
(ie. mysql_connect("localhost", "username", "password"))
2. once you connect, you need to select a database
(ie. mysql_select_db("name_of_database"))
3. set $result using mysql_query, its easier
(ie. $result = mysql_query($query))
hope this sheds some light on things
1. your mysql_connect needs non-null values
(ie. mysql_connect("localhost", "username", "password"))
2. once you connect, you need to select a database
(ie. mysql_select_db("name_of_database"))
3. set $result using mysql_query, its easier
(ie. $result = mysql_query($query))
hope this sheds some light on things
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
For next time: It is easier to help if you actually tell us what the problem with the code is. Does it give you any error messages? What does it do/not do that it shouldn't/should not?
That said, firstly to check if a form has been submitted, set a hidden variable within that form that you can check on the next page:
Do not use mysql_db_query() use mysql_select_db() and mysql_query() instead.
Then try this code:
(Replace $_POST with $HTTP_POST_VARS if you are using PHP 4.0.6 or below.)
Mac
That said, firstly to check if a form has been submitted, set a hidden variable within that form that you can check on the next page:
Code: Select all
<input type="hidden" name="action" value="submit" />Then try this code:
Code: Select all
<?php
if ($_POSTї'action'] == 'submit'){
$connection = mysql_connect ('localhost', 'username', 'password') or die(mysql_error());
mysql_select_db('database_name') or die(mysql_error());
$Name = $_POSTї'Name'];
$Address = $_POSTї'Address'];
$UniversityCompany = $_POSTї'UniversityCompany'];
$PositionCourseofstudy = $_POSTї'PositionCourseofstudy'];
$E_mail = $_POSTї'E_mail'];
$query = "INSERT INTO person_db
VALUES ('$Name','$Address','$UniversityCompany','$PositionCourseofstudy','$E_mail')";
$result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>');
mysql_close();
$recipient = "elely@nus.edu.sg,eleadc@nus.edu.sg";
$subject = "Software download Person details";
$message = "Hello!\n\n'$Name','$Address','$UniversityCompany','$PositionCourseofstudy','$E_mail'\n\n";
mail ($recipient, $subject, $message);
} else {
// show your form again or do something else
}
?>Mac