Solved

Add email prefix

Posted on 2012-04-03
15
462 Views
Last Modified: 2012-05-10
I'm hoping to add email prefix @mydot.ie
A student have to register a module and picked moduleNo1. A student then provide a student ID and password and selected module on a simple from.
A student ID is 12345 and it is automatically generating the email address using the ID. The email address is now 12345@mydot.ie.
This php code as below is a lecturer query for students name and email address. I've moduleNo1 and moduleNo2, maybe a drop down menu to show a query result of moduleNo1 and moduleNo2 separately? Please advise?

<?php

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$dbname = 'mydotData';

$q=$_GET["q"];
$table = "lecturerTable";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if (!$conn)     
	die('Could not connect: ' . mysql_error());
if (!mysql_select_db($dbname))    
	die("Can't select database");	
	
$result = mysql_query("SELECT * FROM {$table} WHERE staffNumber = '".$q."'");
if (!$result)  
	die("Query to show fields from table failed!" . mysql_error());
	
while($row = mysql_fetch_array($result))
{
echo "<p> First Name: " . $row['firstName'] . "</p>";
echo "<p> Last Name: " . $row['lastName'] . "</p>";
echo "<p> Teaching Module: " . $row['moduleNo1'] . "</p>";
echo "<p> Teaching Module: " . $row['moduleNo2'] . "</p>";
echo "<p> Email: " . $row['email'] . "</p>";
}
//mysql_free_result($result);
mysql_close($conn);
?>

Open in new window

0
Comment
Question by:Redscrapbook
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
  • 2
  • +1
15 Comments
 
LVL 51

Accepted Solution

by:
ahoffmann earned 84 total points
ID: 37805510
what exactly is your problem with the value in $row['email'] ?

BTW, I highly recommend to replace
   $q=$_GET["q"];
by something like:
   $q=preg_replace("/[^a-zA-Z0-9_.-]/","",$_GET["q"]);
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 37805766
When you use a query that says SELECT * FROM... your query is telling the data base to select all columns without regard to the names of the columns.  In other words, this query does not "know" what the columns are!  Inside your while loop you can use var_dump() to print out the contents of the rows from the data base.  Then you can identify the columns you want and use their names (and perhaps aliases) in the SELECT query.  Please do that and post the output here so we can see what is in the data base.  Then we may be able to help you get the information formatted in a way that is useful for your needs.

I agree with ahoffmann about sanitizing the external input before using it in a query string.  You may want to learn about this function before hackers destroy your data base.  NEVER, EVER use unfiltered external input for any purpose whatsoever.
http://us.php.net/manual/en/function.mysql-real-escape-string.php
0
 

Author Comment

by:Redscrapbook
ID: 37806010
This is student table with studentID on field.This is student table on browse and you can see the list of studentID and moduleNo1.This is a Dreamweaver CS5.5 phonegap of a design. A student register a moduleNo1 (Digital Audio) or moduleNo2 (Motion Graphics) with a studentID provided and register.This is my MAIN question of this WHOLE solution. When a lectuer click Digital Audio student list and see a list of students email addresses ONLY.Thank you ahoffmann and Ray for your replies. I appreciated it. My php skills is limited so my apologies beforehand.

I've attached a screen grabs of mamp of students and list of studentsID.
And student register a module of their choice and register.  

As attached image no4: This is my MAIN question of this WHOLE solution.
When a lectuer click Digital Audio student list and see a list of students email addresses ONLY for that module and click Motion Graphics student list and see a list of students email addresses ONLY for that module.

There is no email address in a database. Only studentID. When a lecturer click Digital Audio student list and see a list of students email addresses ONLY for that module.
The query the table data with studentID only and php can attach a prefix @mydot.ie of students email addresses.

Then a lecturer will see 123@mydot.ie, 124@mydot.ie, 127@mydot.ie and so on. Limited to ten students.

I hope I make sense?
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:Redscrapbook
ID: 37817574
To explain better to what I'm talking about.

    $peopleid = "12345";
    $suffix = "@dot.com";
    $email = $peopleid . $suffix;
    where $email' then contains "people@dot.com" with MySQL

This is what I've come up with with string operator.

To explain better to what I'm talking about.
    $studentID = "12345";
    $suffix = "@dot.com";
    $email = $studentID . $suffix;
    where $email' then contains "student@dot.com" with MySQL

This is what I've come up with with string operator.

$emails = array();
$query = "SELECT *, CONCAT('studentID', '@dot.com') AS 'Email' FROM 'student'";
$result = mysql_query($query,$conn_to_database);

while($row = mysql_fetch_object($result)):
   $emails[] = $row->Email;
endwhile;

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 37817645
In PHP you can create the concatenated variable like this
$peopleid = "12345";
$suffix = "@dot.com";
$email = $peopleid . $suffix;

Open in new window

Then you can use the created variable in a query like this:
$sql = "SELECT * FROM people WHERE email = '$email' LIMIT 1";

Open in new window

0
 

Author Comment

by:Redscrapbook
ID: 37817651
Did you see my earlier post as soon you post yours as below::

$emails = array();
$query = "SELECT *, CONCAT('peopleID', '@dot.com') AS 'Email' FROM 'people'";
$result = mysql_query($query,$conn_to_database);

while($row = mysql_fetch_object($result)):
    $emails[] = $row->Email;
endwhile;

Open in new window

0
 

Author Comment

by:Redscrapbook
ID: 37817661
Thanks Ray fro your reply. What is the different between post as above and below?
$sql = "SELECT * FROM people WHERE email = '$email' LIMIT 1"; 

Open in new window


Will it show email list maybe ten emails? Is
$email' LIMIT 1"; 

Open in new window

instead of limit one and could I adjust limit to ten?
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 37817982
... instead of limit one and could I adjust limit to ten?
Uhh, sure.  But email addresses are unique in all the world.  How would a limit of 10 rows be expected to produce a result that would be different from LIMIT 1?  If you want to step back from the technical details of this question and just describe in plain language what you're trying to achieve we may be able to show you a well-understood design pattern.
0
 

Author Comment

by:Redscrapbook
ID: 37818127
All will be same email in a same company example @dot.com with student number as studentID see as below.

---------------------------------------------
12345@mydot.ie
---------------------------------------------
12345@mydot.ie
---------------------------------------------
23454@mydot.ie
---------------------------------------------
45691@mydot.ie
---------------------------------------------
2354@mydot.ie
---------------------------------------------
1234@mydot.ie
---------------------------------------------
00065@mydot.ie
---------------------------------------------
76532@mydot.ie
---------------------------------------------
99009@mydot.ie
---------------------------------------------
00001@mydot.ie
---------------------------------------------
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 166 total points
ID: 37840027
0
 

Author Comment

by:Redscrapbook
ID: 37840205
------------------------------------------------------
|  studentID  | firstName| lastName  |  
------------------------------------------------------
| 123             | Kermit      | Frog           |
| 124             | Gonzo      |Great           |
| 125             | Cookie     |mosnter      |
------------------------------------------------------
FIGURE 1

This is what we will see on a database studentTable, you'll see studentID, firstName and lastName. The database does not have students email addresses as above. The email address is @mydot.ie and all students will have a same email address with a studentID.

For example as below (FIG 2) a studentID is 123 and there is no email address. A string operator to create the concatenated variable like this 123@mydot.ie . That is one example.

------------------------------------------------------
|  studentID  | firstName| lastName  |  
------------------------------------------------------
| 123             | Kermit      | Frog           |
------------------------------------------------------
FIGURE 2

What I'm looking for is query return with ten email addresses (FIG 3) as below with database record as FIG1.

---------------------------------------------
12345@mydot.ie
---------------------------------------------
12345@mydot.ie
---------------------------------------------
23454@mydot.ie
---------------------------------------------
45691@mydot.ie
---------------------------------------------
2354@mydot.ie
---------------------------------------------
1234@mydot.ie
---------------------------------------------
00065@mydot.ie
---------------------------------------------
76532@mydot.ie
---------------------------------------------
99009@mydot.ie
---------------------------------------------
00001@mydot.ie
---------------------------------------------
FIGURE 3

NOTE TO REMEMBER: I'm using jQuery 1.6.4 so a script has to work with it. Thanks.
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 166 total points
ID: 37840223
$query = "SELECT *, CONCAT('peopleID', '@dot.com') AS 'Email' FROM 'people' LIMIT 10";
0
 

Author Comment

by:Redscrapbook
ID: 37840269
This is what I've come up with so far with database connectivity that works. When I run it on MAMP to test on a localhost and see a white blank page. I could not figure out why?

This is what is on studentTable database.
------------------------------------------------------
|  studentID  | firstName| lastName  |  
------------------------------------------------------
| 123             | Kermit      | Frog           |
| 124             | Gonzo      |Great           |
| 125             | Cookie     |mosnter      |
------------------------------------------------------


See my previous post that is relevant for your information particularly ID: 37817645 that is relevant to this question as above.



<?php

	// 1 create a database connection
  	$conn = mysql_connect('localhost', 'root', 'root') or die ('Error connecting to mysql');
	//$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
	
	// 2 select a database to use
 	$databaseName = "collegeData"; 
 	$tableName = "studentTable";	
 	$conn = mysql_select_db($databaseName) or die( "Unable to select database");	


	
	$emails = array();
	$query = "SELECT *, CONCAT('studentID', '@mydot.com') AS 'Email' FROM 'studentID'  LIMIT 10";
	$result = mysql_query($query,$conn_to_database);

	while($row = mysql_fetch_object($result)):
   	 $emails[] = $row->Email;
	endwhile;

	//mysql_free_result($result);
	mysql_close($conn);

?>

Open in new window

0
 

Author Closing Comment

by:Redscrapbook
ID: 37953968
Thank you everyone who has contributed to the question.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question