-3

I am trying to integrate CGI with DBI . I am taking the results from the cgi and adding it on to the DBI . But cgi script not able connect to the DBI . Script is mentioned below . The script is not able to connect with DBI as its failing at connection phase .

use CGI qw/:standard/;
use Apache::DBI;
use DBI ;
    my $driver = "mysql"; 
           my $database = "testdb";
           my $dsn = "DBI:mysql:testdb";
           my $userid = "testuser";
           my $password = "test123";

    print
           header,
           start_html('Simple Script'),
           h1('Simple Script'),
           start_form,
           "What's your First name? ",textfield('First name'),p,
           "What's your Last name? ",textfield('Last name'),p,
           "What's your Age? ",textfield('Age'),p,
           "What's your Income? ",textfield('Income'),p,
            popup_menu(-name=>'Sex',
              -values=>['Male','Female']),p,
           submit,
           end_form,
           hr,"\n";
           if (param) {
           my $FIRST_NAME=em(param('First name')) ;
           my $LAST_NAME=em(param('Last name')) ;
           my $AGE=em(param('Age')) ;
           my $INCOME=em(param('Income')) ;
           my $SEX =em(param('Sex')) ;

           print 
           $FIRST_NAME ,p,     
           $LAST_NAME,p, 
           $SEX,p, 
           $AGE,p, 
           $dsn,p
           $INCOME ;    



            my $dbh = DBI->connect($dsn,"root","" ) or die "Can't connect: ",$DBI::errstr;

           my $stmt ;
           my $sql = "INSERT INTO TEST_TABLE (FIRST_NAME, LAST_NAME, SEX, AGE, INCOME ) values (?,?, ?, ?, ?)";

           my   $stmt = $dbh->prepare($sql);    
           $stmt->execute($FIRST_NAME,$LAST_NAME, $SEX, $AGE, $INCOME) or die $DBI::errstr;

           }       
           print end_html;
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • 2
    Any error message?You should use $userid and $passowrd to connect: `DBI->connect($dsn,$userid,$password)` – Jens May 11 '17 at 12:50
  • 1
    "`print $FIRST_NAME ,p, ...`" ... that's a crime against humanity. What do you think **`my $AGE=em(param('Age')) ;`** does? – Sinan Ünür May 11 '17 at 12:53
  • 2
    @Sinan ["CGI.pm is so ugly, it makes blind children cry!"](https://www.youtube.com/watch?v=jKOqtRMT85s) :) – simbabque May 11 '17 at 12:57
  • 2
    There's a `$dsn` in your code that's not declared. `$stmt` is declared twice. Turn on `use strict` and `use warnings`, fix the problems they report and then [edit] your post and update the code please. – simbabque May 11 '17 at 12:59
  • 1
    Do you really want to tie your program to [old and buggy versions of CGI.pm](https://metacpan.org/pod/distribution/CGI/lib/CGI.pod#HTML-Generation-functions-should-no-longer-be-used)? – Sinan Ünür May 11 '17 at 13:05
  • 1
    `print $FIRST_NAME ,p, $LAST_NAME,p,...` I'm not sure what scares me most - the Perl or the HTML it generates! – Dave Cross May 11 '17 at 13:16
  • $dns is declared my $dsn = "DBI:mysql:testdb"; and $stmt is declare twice has no issue . – Ajay Chopra May 11 '17 at 13:17
  • Please help in how I can connect DBI through CGI script .The connect one code do not work . – Ajay Chopra May 11 '17 at 13:18
  • 2
    @AjayChopra: What is the error that you get (please edit your question to add this information. – Dave Cross May 11 '17 at 13:20
  • 1
    Your DSN assumes that the database is on the same server as the web server. Is that correct? – Dave Cross May 11 '17 at 13:20
  • 2
    @AjayChopra Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). We cannot divine what is happening on your computer without you showing us the error messages you get etc. – Sinan Ünür May 11 '17 at 13:20
  • 1
    In your code, you define variables for `$userid` and `$password`, in the call to `connect()` you ignore those variables and use hard-coded values. Is that the problem? And does your root user really have an empty password? – Dave Cross May 11 '17 at 13:29
  • I dont get any error message .yes I have root userid and dont have any password . – Ajay Chopra May 11 '17 at 13:34
  • I did debugging by printing the values all values are printed above the connect statement and after the connect statement it is not showing. – Ajay Chopra May 11 '17 at 13:36
  • 1
    @AjayChopra: You say you don't get an error message. Your code gives a "can't connect" error if the connection fails. Therefore it seems that the connection doesn't fail. What makes you think that the connection fails? – Dave Cross May 11 '17 at 14:14
  • 1
    @DaveCross He is not looking in the error log. OP: First, try to debug on the command line. – Sinan Ünür May 11 '17 at 14:42
  • 1
    @AjayChopra Is what Sinan says right? Are you looking in the web server error log? What does it say there? – Dave Cross May 11 '17 at 14:44
  • 2
    @AjayChopra: By the way, that `use Apache::DBI` is doing nothing for you in a CGI environment. You should just remove it. – Dave Cross May 11 '17 at 14:46

1 Answers1

2

There's not nearly enough information here to give you an answer. In particular, there is no way that we can work out what the problem is if we don't know what the error is. You say there's no error message. That's very unlikely, as your code explicitly dies with an error message if the connection fails. This leaves us with two possibilities:

  • Your program is connecting successfully
  • There's an error message, but you haven't seen it yet.

Let's assume it's the second option. Perhaps you don't realise that CGI programs deliberately don't send their error messages to the browser (this is a security feature and you shouldn't try to change it) and that you need to look in the web server error log.

Here are three ways that you could see the error message.

  • Look in the error log. No, we don't know where the error log is. It depends on the configuration of your server. If we knew which environment you are using then we could take a guess. But as it is, you'll need to ask someone who knows how the server is configured.
  • Run your program on the command line. Yes, I know that it's a CGI program. But it's often easiest to debug programs by running them on the command line. You'll need to pass a parameter in as that triggers your program connecting to the database. Then, if there's an error it will be written to STDERR (which is your console).
  • Add use CGI::Carp 'fatalsToBrowser'; to your code. Remember above where I said that you shouldn't change the fact that CGI errors aren't written to the browser? Well, this is how you override that. It's a terrible idea and I really don't recommend it (mainly because you should remove it again before you put the code into production - and everyone always forgets that and leaves a gaping great security hole in their web server!) but it works in extreme situations where you really can't find out where the error log is and you don't know how to use a command line.

So one of those methods will tell you what the error is. If you edit your question to add that vital detail, then I'm sure someone here will be able to help solve your problem.

Some other tips:

  • Apache::DBI is doing nothing useful here. Just remove it.
  • Learn to format your code.
  • The HTML generation functions have been recognised as a terrible idea since the last millennium and now they are deprecated. Please don't use them. Use a templating system instead.
  • The <p> element in HTML is a container, not a separator.
  • Please read CGI::Alternatives and learn something about how web applications are written in Perl in the 21st century.
Dave Cross
  • 68,119
  • 3
  • 51
  • 97