d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Php Help > Need Help Invoking A Function
Add Reply New Topic New Poll
Member
Posts: 25,879
Joined: Jan 14 2008
Gold: 1,000.20
Mar 5 2017 07:01pm
so i'm trying to invoke a function inside a variable. here is the snippet of code below.
Basically i want the $to variable to come from function get_mail_adress. I know when you invoke the function you are supposed to set the parameters. The problem is inside the database where i am getting my information from there are multiple email addresses so it will never be the same. Also, the type has three different options. Not sure how to invoke it. Let me know what you guys think

function get_mail_adress($Name, $Type){

if($Type == 'A'){
return "thebossemail39@mailinator.com";
}
else{
$s = "SELECT email FROM Users WHERE User = '$Name'";
$t = mysql_query($s) or die (mysql_error());
$r = mysql_fetch_array($t);
return $r["Email"];
}

}

function mymail($Mail, $to, $subject, $message, $headers){
if($Mail == 'Yes'){

$to = get_mail_adress($Name, $Type);
$subject = "IT 20";
$message = "zz";
$headers = "";


mail ($to, $subject, $message, $headers );
//only way to get it to email to myself
$headers = 'MIME-Version: 1.0';
$headers = 'Content-type: text/html; charset=iso-8859-1';
echo "<br><br>Email Sent";
}

}
Member
Posts: 3,197
Joined: May 4 2013
Gold: 1,457.00
Mar 5 2017 07:16pm
That code should work. Are you getting errors?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 5 2017 07:16pm
Quote
The problem is inside the database where i am getting my information from there are multiple email addresses so it will never be the same.


are you saying "user" is not a unique column so you can have multiple users with the same name? if that's the case, you have two solutions:

1) if it's supposed to only have 1 email per user, then use a surrogate key (eg: user_id) and pass in user_id instead of name
2) if you want to let the user have multiple emails, it's a functional question, not a technical one. perhaps you can let the user pick which email to use?

This post was edited by carteblanche on Mar 5 2017 07:16pm
Member
Posts: 25,879
Joined: Jan 14 2008
Gold: 1,000.20
Mar 5 2017 07:17pm
Quote (nuvo @ 5 Mar 2017 21:16)
That code should work. Are you getting errors?


it wont grab the email from the function above.

It will execute, but if i select $Type 'A' it should email to the specified email address in the return, which it does not.

Also, if i select a different $Type, it will execute but no email will be sent.

if i change $to to hard code, any given email address for instance, it will work and send the email. Which is why i am confused

Quote (carteblanche @ 5 Mar 2017 21:16)
are you saying "user" is not a unique column so you can have multiple users with the same name? if that's the case, you have two solutions:

1) if it's supposed to only have 1 email per user, then use a surrogate key (eg: user_id) and pass in user_id instead of name
2) if you want to let the user have multiple emails, it's a functional question, not a technical one. perhaps you can let the user pick which email to use?


by surrogate do you mean create a specific variable for each email address? currently there are 200 users each with a unique email. The program is supposed to simulate an ATM that emails you when you have either deposited or withdrew $

This post was edited by diehard20 on Mar 5 2017 07:31pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 5 2017 07:57pm
Quote (diehard20 @ Mar 5 2017 08:17pm)
it wont grab the email from the function above.

It will execute, but if i select $Type 'A' it should email to the specified email address in the return, which it does not.

Also, if i select a different $Type, it will execute but no email will be sent.

if i change $to to hard code, any given email address for instance, it will work and send the email. Which is why i am confused


did you try lower case?

Code
return $r["email"];


have you debugged to see what's happening? i dont see where your $Name and $Type are coming from, so make sure they're set correctly. try hard coding them to see if you're getting what you expect


Quote
by surrogate do you mean create a specific variable for each email address? currently there are 200 users each with a unique email. The program is supposed to simulate an ATM that emails you when you have either deposited or withdrew $

that's not what you said earlier, so you can disregard.
Member
Posts: 25,879
Joined: Jan 14 2008
Gold: 1,000.20
Mar 5 2017 07:59pm
here is the entire file. $Name and $Type are coming from an HTML5 page where i set radio buttons and text field for the info. They are then passed to another file that sets them to the variables, $Name, $Pass, $Amount etc. which are then passed into this file via the get_case function.

e/ i should say the other file is the main php file that invokes these.

e/ #2. Trying with the terminology the best i can. I learned php this afternoon for this and i'm just stuck lol.

<?php

function get_case($Name, $Pass, $Amount, $Type, $Mail)
{
return $Type;
}
function admin ($Name, $Pass)
{
if($Name != "Admin" || $Pass != "007")
{
echo"bad credentials";
exit;
}
return;
}

function user($Name, $Pass, $Amount, $Type)
{
//validates the users credentials
$s = "select * from Users where User = '$Name' and Pass = '$Pass'";
$t = mysql_query($s) or die (mysql_error());
if(mysql_num_rows($t) == 0)
{
echo"bad credentials";
exit;
}
//protects from overdrafts
$r = mysql_fetch_array($t);
if($r["current_balance"] < $Amount and $Type == 'W')
{
echo"Overdrawn account, withdrawl denied";
echo$r["current_balance"];
exit;
}
}


function get_mail_adress($Name, $Type)
{
//echo"you're in the mail function";
if($Type == 'A')
{
return "thebossemail39@mailinator.com";
}
else
{
$s = "SELECT email FROM Users WHERE User = '$Name'";
$t = mysql_query($s) or die (mysql_error());
$r = mysql_fetch_array($t);
return $r["Email"];
}

}

function mymail($Mail,$Name,$Type)
{

if($Mail == 'Yes')
{

$to = get_mail_adress($Name, $Type);
$subject = "IT 20";
$message = "zz";
$headers = "";


mail ($to, $subject, $message, $headers );
//only way to get it to email to myself
$headers = 'MIME-Version: 1.0';
$headers = 'Content-type: text/html; charset=iso-8859-1';
echo "<br><br>Email Sent";
}

}







function update($Name, $Amount, $Type)
{
//add a row to T
$s = "insert into Transactions values('$Name', '$Type', '$Amount', NOW())";
echo "<br>SQL insert to Transactions: $s.<br><br>";
mysql_query($s) or die (mysql_error());



//if D add amount to current_balance
if($Type == "D")
{
$s = "Update Users set current_balance = current_balance + '$Amount' where User = '$Name'";
mysql_query($s) or die (mysql_error());
echo "<br>SQL statement for update function is: $s";
}


echo"</table>";


//if W add withdrawl amount form current_balance
if($Type == "W")
{
$s = "Update Users set current_balance = current_balance - '$Amount' where User = '$Name'";
mysql_query($s) or die (mysql_error());
echo "<br>SQL statement for update function is: $s";
}
}
//useless function
function sql ( $Type, $Name, &$s1, &$s2)
{

if ($Type != 'A')
{
$s1 = ("select * from Users where User = ('$Name') ");

}
if ($Type == 'A')
{
$s1 = ("select * from Users ");
$s2 = ("select * from Transactions");

}





}

//i combined get_a with sql, could not figure out how to do sql function so i just integrated it, it was more efficient this way

function get_A($Type)
{

if($Type == 'A')
{
$x = "SELECT * FROM Users";
$y = mysql_query($x) or die (mysql_error());

echo"<table>";

while($row = mysql_fetch_array($y)){
echo"<tr><td>" . $row['User'] . "<tr><td>" . $row['Pass'] . "<tr><td>" . $row['email'] . "<tr><td>"
. $row['fullname'] . "<tr><td>" . $row['address'] . "<tr><td>" . $row['initial_balance'] . "<tr><td>"
. $row['current_balance'] . "<tr><td>";
}
echo"</table>";

}




}
//did the same thing as above, integrated sql function.
function get_T($Type)
{
if($Type == 'A')
{
$n = "SELECT * FROM Transactions";
$p = mysql_query($n) or die (mysql_error());

echo"<table>";
while($row = mysql_fetch_array($p)){
echo"<tr><td>" . $row['User'] . "<tr><td>" . $row['type'] . "<tr><td>" . $row['amount'] . "<tr><td>"
. $row['date'] . "<tr><td>";
}
echo"</table>";
}
}





?>

This post was edited by diehard20 on Mar 5 2017 08:03pm
Member
Posts: 25,879
Joined: Jan 14 2008
Gold: 1,000.20
Mar 5 2017 08:35pm
i figured it out,

thanks all
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll