mailform script, select from the database

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

NAT
Forum Newbie
Posts: 24
Joined: Tue Mar 21, 2006 3:13 am

mailform script, select from the database

Post by NAT »

Hi,

i want to make a mailfrom script, that selects the email of a user automaticaly from the database and sends mail to a user. This script is usefull for users, where users can send mail to eachother.

Right now i am using a mailto function like this, wich opens automaticaly Outlook Exprees or other Email clients.

Code: Select all

<a href=mailto:$email_p><img src=images/email.jpg border=0></a>
So now i am twaing to make mailform script, wich selects a email adres automaticaly from the database, but i cant, i never made that kind of script.

Can any one explan me wich steps do i have to take to make that script work, so far i made this:

Code: Select all

$msg = "MESSAGE FROM SITE\n";
          $msg .= "Sender's Name: $_POST[sender_name]\n";
          $msg .= "Sender's E-Mail: $_POST[sender_email]\n";
          $msg .= "Message: $_POST[message]\n\n";
          $subject = "message from a user";
		  
		  @mysql_connect(localhost,"zzzzzzz","xxxxxxx");
		  @mysql_select_db("xxxxxxx") or die( "<CENTER> Couldnt find a database.");

	 	  $query = "SELECT * FROM users WHERE email='$email'";
 	 	  $result = mysql_query($query);
	 	  $num = mysql_numrows($result);
     	  if ($num > 0) {
	 
	 	   }
     
		  
		  
          //send the mail
          mail($to, $subject, $msg, $mailheaders);
But this would not work, couse 100% foult. I just dont know how to make that kind of script, i dont wich code do i have write first.
ianhull
Forum Contributor
Posts: 310
Joined: Tue Jun 14, 2005 10:04 am
Location: Hull England UK

Post by ianhull »

Code: Select all

$msg = "MESSAGE FROM SITE\n"; 
          $msg .= "Sender's Name: $_POST[sender_name]\n"; 
          $msg .= "Sender's E-Mail: $_POST[sender_email]\n"; 
          $msg .= "Message: $_POST[message]\n\n"; 
          $subject = "message from a user"; 
        
//Add this
$email = $_POST[sender_email];

//You must also say who the email is going to, you have the variable $to but it is currently empty.
// Maybe it should be
$to = $_POST['to'];


        @mysql_connect(localhost,"zzzzzzz","xxxxxxx"); 
        @mysql_select_db("xxxxxxx") or die( "<CENTER> Couldnt find a database."); 

         $query = "SELECT * FROM users WHERE email='$email'"; 
          $result = mysql_query($query); 
         $num = mysql_numrows($result); 
          if ($num > 0) { 
    
          } 
      
        
        
          //send the mail 
          mail($to, $subject, $msg, $mailheaders);
NAT
Forum Newbie
Posts: 24
Joined: Tue Mar 21, 2006 3:13 am

Post by NAT »

this is not nessasory:

//Add this
$email = $_POST[sender_email];

//You must also say who the email is going to, you have the variable $to but it is currently empty.
// Maybe it should be
$to = $_POST['to'];


my script works white out those lines too. :( :? :? :? :? i need to fiz the query. I want that query selects the email adres of user from the database and then send the email to selected user. Thats the point of the script that trying to write.


But i think the query is wrong:

$query = "SELECT * FROM users WHERE email='$email'";
$result = mysql_query($query);
$num = mysql_numrows($result);
if ($num > 0) {

}
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

There isn't such function called "mysql_numrows"!!! There is a function called mysqli_numrows (http://il.php.net/manual/en/function.my ... m-rows.php)!!!
You may use this:

Code: Select all

<?php
...
$query_res = mysql_query($query); 
$line = -1;
while($results != FALSE)
{
	$line++;
	$results = mysql_result($query_res, $line);	
}
...
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

ok wrote:There isn't such function called "mysql_numrows"!!! There is a function called mysqli_numrows (http://il.php.net/manual/en/function.my ... m-rows.php)!!!
http://il.php.net/manual/en/function.mysql-num-rows.php
(#10850)
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Oops... You are right!!! I didn't noticed that...

But you also made a mistake. You wrote "mysql_numrows" instead of "mysql_num_rows"!!!
NAT
Forum Newbie
Posts: 24
Joined: Tue Mar 21, 2006 3:13 am

Post by NAT »

All my scripts r written with this code,
$result = mysql_query($query);
$num = mysql_numrows($result);

I never had problem with that. The script is not working, it want send any email, and i dont know if the query works .

I want to write a query, that selects a emailadres of a user from the database, en then send to it.
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

So copy this:

Code: Select all

<?php
\\...
$query = "SELECT `email`, `user` FROM users WHERE `user`='$user";//remember to change `user` into unique!!!
$result = mysql_query($query);
$email = mysql_result($result, 0);
if(!$email)
{
die("Problem!!!");
}
\\...
?>
NAT
Forum Newbie
Posts: 24
Joined: Tue Mar 21, 2006 3:13 am

Post by NAT »

i am getting the folowing error:
Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 11 in httpd.www/Test-page/mailform_users2.php on line 56
Problem!!!
this is the line 56:

Code: Select all

$email = mysql_result($result, 0,'email');
I have made enoughf users in DB, but the script cant find any. :(
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

I had a syntex error in my last post, copy this and check it again:

Code: Select all

$query = "SELECT `email`, `user` FROM users WHERE `user`='".$user."'";//remember to change `user` into
NAT
Forum Newbie
Posts: 24
Joined: Tue Mar 21, 2006 3:13 am

Post by NAT »

i am getting stil the same error with this guery too.

Code: Select all

$query = "SELECT `email`, `name` FROM users WHERE `name`='".$name."'";//remember to change `user` into
By the way i also wrote this query and i ma getting also the same error

Code: Select all

$query = "SELECT * FROM users WHERE name='$name'";
I dont think there is somethink worng with query write now, but entire script
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Copy this and see if there are problems:

Code: Select all

mysql_connect(localhost,"zzzzzzz","xxxxxxx") or die(mysql_error());
        mysql_select_db("xxxxxxx") or die(mysql_error());
NAT
Forum Newbie
Posts: 24
Joined: Tue Mar 21, 2006 3:13 am

Post by NAT »

this is what i have:

Code: Select all

@mysql_connect(localhost,"xxxxxxx","xxxxxxxx");
@mysql_select_db("xxxxxxx") or die( "<CENTER> The database couldnt be found.");
$query = "SELECT * FROM users WHERE name='".$name."'";
$result = mysql_query($query); 
$email = mysql_result($result, 0,'email'); 
if(!$email) 
{ 
die("Probleem!!!"); 
}
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

Replace:

Code: Select all

@mysql_connect(localhost,"xxxxxxx","xxxxxxxx");
@mysql_select_db("xxxxxxx") or die( "<CENTER> The database couldnt be found.");
With:

Code: Select all

mysql_connect("localhost","zzzzzzz","xxxxxxx") or die(mysql_error());
mysql_select_db("xxxxxxx") or die(mysql_error());
NAT
Forum Newbie
Posts: 24
Joined: Tue Mar 21, 2006 3:13 am

Post by NAT »

done that and stil the same error


Warning: mysql_result(): Unable to jump to row 0 on MySQL result index 11 in
Post Reply