473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

query help!

Hi all,

I am new to perl - just started to learn it a few weeks ago. Basically, what I am trying to do is a "register user" script using a mySQL database.

I have managed to do the insert query (see below). But my problem is the following:

user table contains in the right order:
- email (primary key)
- password
- first name
- last name
- scientist
- observer

so when the script is run, it first needs to check that no user already registered is using that email address (i know the query will not be done if it exists but i want the page to be redirected to "login.html" with a GET parameter so that I can let the user know why it did not work).

I am having problems getting the select query working...

Could anybody show me a way to do it using the fetch method in perl?

Also, i would appreciate if you could explain so i can understand what I was doing wrong. Here is the original code I have working for the registering process.

Thanks,

olo
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use CGI;
  3. use DBI;
  4.  
  5. $cgiquery = CGI::new();
  6. $|=1;
  7.  
  8. $fname = $cgiquery->param("fname");
  9. $lname = $cgiquery->param("lname");
  10. $email = $cgiquery->param("email");
  11. $password = $cgiquery->param("password");
  12. $observer = $cgiquery->param("observer");
  13. $scientist = $cgiquery->param("scientist");
  14.  
  15. my $dbh;
  16.  
  17. $dbh = DBI->connect("DBI:mysql:database=ornisoft;host=localhost",
  18.             "root","student",{'RaiseError' => 1} );
  19.  
  20. $query = 'INSERT INTO users 
  21.             VALUES("'.$email.'", "'.$password.'", "'.$fname.'", "'.$lname.'", "'.$scientist.'", "'.$observer. '");';
  22.  
  23.  
  24.  
  25. $sth = $dbh->prepare($query);
  26. $sth->execute();
  27.  
  28. $sth->finish();
  29.  
  30. print $cgiquery->redirect('http://localhost/~student/login.html');
Oct 28 '07 #1
11 2664
eWish
971 Expert 512MB
Welcome to TSDN olobraecky!!!

Here is some commented code that should help you in the right direction. The '?' in the statements are called placeholders. The DBI will escape the data when placeholders are used.

Untested Code
Expand|Select|Wrap|Line Numbers
  1. #This query is used to check to see if the user exists.
  2. my $check_user = $dbh->prepare(qq{SELECT email FROM table_name WHERE email = ?});
  3.  
  4. #This query will insert the user data into the database.
  5. my $insert_user = $dbh-prepare(qq{INSERT INTO table_name(email, password, first_name, last_name, scientist, observer) VALUES(?,?,?,?,?,?)});
  6.  
  7. #This is the redirect url.
  8. my $redirect_url = 'http://somewhere.com/redirect_page.html';
  9.  
  10.  
  11. #Check to see if the user is already in the db.
  12. if ($email) {
  13.  
  14.  #If this returns true then the user is already in the db. 
  15.  if( my ($existing_user) = $dbh->selectrow_array($ckeck_user, undef, ( $email ))){
  16.  
  17.   #If the user exists then redirect.
  18.    if ($existing_user) {
  19.     $cgiquery->redirect($redirect_url);
  20.  
  21.   #Since the user did not exist insert into db 
  22.    } else {
  23.     $insert_user->execute($email,$password, $fname, $lname, $scientist, $observer );
  24.   }
  25.  
  26.  
  27.    $check_user->finish();
  28.    $insert_user->finish();
Also, it appears that you are not validating the user data. That can be unsafe. I strongly recommend that you check the user data before using it. There is a module that you can use to check and validate the email address called Email::Valid

Checking other data can be done with a regex
Expand|Select|Wrap|Line Numbers
  1. if ($cgiquery->param('email') =~ /^(?:([\w\s+\d\']+))$/) {
  2.      $email = $1;
  3. }
Oct 28 '07 #2
hi eWish,

I've just tried your implementation. But i get the same problem "500 Internal Server Error" which has been driving me mad all day.

I knew that I would have to validate data but i wanted to get the checking users feature to work first.

here is the exact code of my file:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use CGI;
  3. use DBI;
  4.  
  5. $cgiquery = CGI::new();
  6. $|=1;
  7.  
  8. $fname = $cgiquery->param("fname");
  9. $lname = $cgiquery->param("lname");
  10. $email = $cgiquery->param("email");
  11. $password = $cgiquery->param("password");
  12. $observer = $cgiquery->param("observer");
  13. $scientist = $cgiquery->param("scientist");
  14.  
  15. my $dbh;
  16.  
  17. $dbh = DBI->connect("DBI:mysql:database=ornisoft;host=localhost", "root","student",{'RaiseError' => 1} );
  18.  
  19. my $check_user = $dbh->prepare(qq{SELECT email FROM table_name WHERE email = ?});
  20.  
  21. my $insert_user = $dbh-prepare(qq{INSERT INTO table_name(email, password, first_name, last_name, scientist, observer) VALUES(?,?,?,?,?,?)});
  22.  
  23. my $redirect_url = 'http://localhost/~student/login.html/';
  24.  
  25. if ($email) {
  26.  
  27.    if( my ($existing_user) = $dbh->selectrow_array($ckeck_user, undef, ( $email ))){
  28.     if ($existing_user) {
  29.         $cgiquery->redirect($redirect_url);
  30.     } else {
  31.         $insert_user->execute($email,$password, $fname, $lname, $scientist, $observer );
  32.     }
  33.    }
  34. }
  35. $check_user->finish();
  36. $insert_user->finish();
did i make a mistake? or did i misunderstand your post?

sorry if i'm being silly, but perl is really new to me... it's for a uni coursework... php is so much more my thing... lol

thanks for ur help!

olo
Oct 28 '07 #3
eWish
971 Expert 512MB
When posting code please use the [code][/code] tags. I have added the code tags in your posts. Please use this them with all future posts. Thank You.

Replace the first part of your code with the following.
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl 
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI;
  7. use CGI::Carp qw/fatalsToBrowser/;  #Used to help debug
  8.  
  9. my $cgiquery = CGI->new;
  10.  
  11. print $cgiquery->header; 
One of the reasons you are getting the error is because you are not printing the proper headers. Using the CGI::Carp module will print errors to the screen. This is a big help when debugging.
Oct 28 '07 #4
eWish
971 Expert 512MB
I did not realize that this was course work until after I posted my reply. Please read the Posting Guidlines.

Regards,
Kevin
Oct 28 '07 #5
Indeed, that debugging feature is really useful.

I'm still having difficulties to get it working though:

Global symbol "$fname" requires explicit package name at register.cgi line 10.
Global symbol "$lname" requires explicit package name at register.cgi line 11.
Global symbol "$email" requires explicit package name at register.cgi line 12.
Global symbol "$password" requires explicit package name at register.cgi line 13.
Global symbol "$observer" requires explicit package name at register.cgi line 14.
Global symbol "$scientist" requires explicit package name at register.cgi line 15.
Global symbol "$email" requires explicit package name at register.cgi line 27.
Global symbol "$ckeck_user" requires explicit package name at register.cgi line 29.
Global symbol "$email" requires explicit package name at register.cgi line 29.
Global symbol "$email" requires explicit package name at register.cgi line 33.
Global symbol "$password" requires explicit package name at register.cgi line 33.
Global symbol "$fname" requires explicit package name at register.cgi line 33.
Global symbol "$lname" requires explicit package name at register.cgi line 33.
Global symbol "$scientist" requires explicit package name at register.cgi line 33.
Global symbol "$observer" requires explicit package name at register.cgi line 33.
Execution of register.cgi aborted due to compilation errors.

man, perl is soooooo hard... i've never been struggling so much with a server-side language...
Oct 28 '07 #6
i see... so i'm not allowed to ask for help?

as i mentioned, i'm trying to understand (which is why i asked if you could explain)

if not, i understand and i'll try to do it by myself.

let me know,

olo
Oct 28 '07 #7
eWish
971 Expert 512MB
You are getting those message's because you are not delaring your $variables first. I would bookmark perldoc.perl.org this is a great resource.
Oct 28 '07 #8
eWish
971 Expert 512MB
i see... so i'm not allowed to ask for help?

as i mentioned, i'm trying to understand (which is why i asked if you could explain)

if not, i understand and i'll try to do it by myself.

let me know,

olo
We are here to help everyone. When it comes to course work we will try to guide you in the proper direction and help you understand why your code is not working rather than just offering up solutions.

Regards,

Kevin
Oct 28 '07 #9
Kevin,

Glad we got that sorted because I was expecting nothing more from this forum.

I'm now trying some more code using the notes I had taken last week. But that is not even working :s

it's a simple select query that is supposed to output email adresses (from my db) into html table.

Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use CGI;
  5. use CGI::Carp qw/fatalsToBrowser/;  #Used to help debug
  6. use DBI;
  7. my $cgiquery = CGI->new;
  8.  
  9. print $cgiquery->header;
  10.  
  11. my $dbh;
  12. my $sth;
  13. my $resptr;
  14. my $email;
  15.  
  16. $dbh = DBI->connect("DBI:mysql:database=XXX;host=localhost", "XXX","XXX",{'RaiseError' => 1} );
  17.  
  18. $sth = $dbh->prepare("SELECT * FROM users");
  19. $sth->execute();
  20.  
  21. print "<table border=2>\n";
  22. while ( $resptr = $sth->fetchrow_hashref() )
  23. {
  24.    print "<tr>";
  25.    print "<td>" . $resptr->{"$email"}."</td>";
  26.    print "</tr>\n";
  27. }
  28.  
  29. print "</table>";
  30.  
strange thing is that there are no errors. table is shown but no data inside it.

there must be something wrong with line 25...
Oct 28 '07 #10
eWish
971 Expert 512MB
You are correct, your error is in line 25. You don't have the hashref correct.
Expand|Select|Wrap|Line Numbers
  1. print "<td>" . $resptr->{email}."</td>";
Oct 28 '07 #11
hey Kevin,

finally got it working! i was actually really close in the beginning.

thanks for your help! i'm now gonna work on fields validation!

olo
Oct 28 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
7
by: Simon Bailey | last post by:
How do you created a query in VB? I have a button on a form that signifies a certain computer in a computer suite. On clicking on this button i would like to create a query searching for all...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
36
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I...
5
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the...
10
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.