Need some help with this piece of code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Keroberos
Forum Newbie
Posts: 1
Joined: Tue Oct 01, 2002 9:53 pm
Location: Singapore

Need some help with this piece of code

Post by Keroberos »

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);

?>
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

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 :)
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

It's a good idea to use $_POST, $_GET...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

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:

Code: Select all

&lt;input type="hidden" name="action" value="submit" /&gt;
Do not use mysql_db_query() use mysql_select_db() and mysql_query() instead.
Then try this code:

Code: Select all

&lt;?php 
if ($_POST&#1111;'action'] == 'submit'){ 
	$connection = mysql_connect ('localhost', 'username', 'password') or die(mysql_error()); 
	mysql_select_db('database_name') or die(mysql_error());
	
	$Name = $_POST&#1111;'Name'];
	$Address = $_POST&#1111;'Address'];
	$UniversityCompany = $_POST&#1111;'UniversityCompany'];
	$PositionCourseofstudy = $_POST&#1111;'PositionCourseofstudy'];
	$E_mail = $_POST&#1111;'E_mail'];

	$query = "INSERT INTO person_db 
		VALUES ('$Name','$Address','$UniversityCompany','$PositionCourseofstudy','$E_mail')"; 

	$result = mysql_query($query) or die(mysql_error().'&lt;p&gt;'.$query.'&lt;/p&gt;'); 
	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
}
?&gt;
(Replace $_POST with $HTTP_POST_VARS if you are using PHP 4.0.6 or below.)

Mac
Post Reply