Unable to insert a record into MySQL database using DBI

user3181823

I am trying to insert a record into a MySQL database using Perl DBI. I am not getting any errors but the insert is not working. However, I am able to successfully fetch records from the database using DBI.

Here is the code that does the insert:

#!"C:\xampp\perl\bin\perl.exe"

use diagnostics;
use DBI;
use strict;
use warnings;

my $driver = "mysql"; 
my $database = "mysql";
my $dsn = "DBI:$driver:database=$database";
my $userid = "root";
my $password = "password";

my $buffer;
my @pairs;
my $pair;
my $name;
my $value;
my %FORM;

# Read in text
my $ENV;
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
    $buffer = $ENV{'QUERY_STRING'};
}

# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%(..)/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}

my $first_name= $FORM{name};
my $address = $FORM{address};
my $city  = $FORM{city};
my $occupation  = $FORM{occupation};
my $age  = $FORM{age};

my $dbh = DBI->connect("dbi:mysql:dbname=mysql", "root", "password",{ AutoCommit =>  0,RaiseError => 1}, ) or die ("Couldn't connect to database: ") , $DBI::errstr;

# my $sth = $dbh->prepare("INSERT INTO persons
                        # (FirstName, LastName,Address,City)
                        # values
                        # ($first_name, $last_name,$address,$city)");
my $query = "insert into userrecords(Address,Age,City,Name,Occupation)
             values (?, ?, ?, ?, ?) ";

my $statement = $dbh->prepare($query) or die ("Couldn't connect to database: "), $DBI::errstr;
$statement->execute($address,$age,$city,$name,$occupation) or die ("Couldn't connect to database: "), $DBI::errstr;

$dbh->disconnect();

my $URL = "http://.....:81/cgi-bin/showdata.cgi";
print "Location: $URL\n\n";

exit(0);

When I run my code in the Padre IDE, I get the following errors:

****Error*********
Useless use of a variable in void context at InsertRecord.cgi line 50 (#1)
    (W void) You did something without a side effect in a context that does
    nothing with the return value, such as a statement that doesn't return a
    value from a block, or the left side of a scalar comma operator.  Very
    often this points not to stupidity on your part, but a failure of Perl
    to parse your program the way you thought it would.  For example, you'd
    get this if you mixed up your C precedence with Python precedence and
    said

        $one, $two = 1, 2;

    when you meant to say

        ($one, $two) = (1, 2);

    Another common error is to use ordinary parentheses to construct a list
    reference when you should be using square or curly brackets, for
    example, if you say

        $array = (1,2);

    when you should have said

        $array = [1,2];

    The square brackets explicitly turn a list value into a scalar value,
    while parentheses do not.  So when a parenthesized list is evaluated in
    a scalar context, the comma is treated like C's comma operator, which
    throws away the left argument, which is not what you want.  See
    perlref for more on this.

    This warning will not be issued for numerical constants equal to 0 or 1
    since they are often used in statements like

        1 while sub_with_side_effects();

    String constants that would normally evaluate to 0 or 1 are warned
    about.

Useless use of a variable in void context at InsertRecord.cgi line 59 (#1)
Useless use of a variable in void context at InsertRecord.cgi line 60 (#1)

Use of uninitialized value in transliteration (tr///) at InsertRecord.cgi line
        23 (#2)
    (W uninitialized) An undefined value was used as if it were already
    defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.

    To help you figure out what was undefined, perl will try to tell you the
    name of the variable (if any) that was undefined. In some cases it cannot
    do this, so it also tells you what operation you used the undefined value
    in.  Note, however, that perl optimizes your program and the operation
    displayed in the warning may not necessarily appear literally in your
    program.  For example, "that $foo" is usually optimized into "that "
    . $foo, and the warning will refer to the concatenation (.) operator,
    even though there is no . in your program.

Use of uninitialized value $ENV{"REQUEST_METHOD"} in string eq at
        InsertRecord.cgi line 24 (#2)
Use of uninitialized value $buffer in split at InsertRecord.cgi line 29 (#2)
Location: http://.......:81/cgi-bin/showdata.cgi

Press any key to continue . . .

***********END***********************

What is the issue?

Dave Cross

What happens when you replace your code with this:

#!"C:\xampp\perl\bin\perl.exe"
use strict;
use warnings;

use diagnostics;
use DBI;
use CGI qw[param redirect];    

my $driver   = "mysql"; 
my $database = "mysql";
my $dsn      = "DBI:$driver:database=$database";
my $userid   = "root";
my $password = "password";

my $dbh = DBI->connect("dbi:mysql:dbname=mysql", "root", "password",
    { AutoCommit =>  0,RaiseError => 1}, )
    or die "Couldn't connect to database: ", $DBI::errstr;

my $query = "insert into userrecords(Address,Age,City,Name,Occupation)
values (?, ?, ?, ?, ?) ";

my $statement = $dbh->prepare($query)
    or die "Couldn't connect to database: " , $DBI::errstr;
$statement->execute(param('address'), param('age'), param('city'),
                    param('name'), param('occupation'))
    or die "Couldn't connect to database: " , $DBI::errstr;    
$dbh->disconnect();

my $URL = "http://.....:81/cgi-bin/showdata.cgi";
print redirect($URL);

I've basically made two changes:

  1. Use the CGI.pm module to handle the CGI interaction (getting the parameters and printing the redirection header).
  2. Fixed your "void context" errors by removing the misplaced parentheses in all of your calls to die.

I'm made no substantive changes to the code, but at least we now have a clean version to go with.

Update: D'oh. It's obvious now the code is cleaned up a bit. If you have "Autocommit" turned off, then you need to commit your changes. Add $dbh->commit between the calls to execute() and disconnect().

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to insert data into mysql database using AJAX

From Dev

How to insert datagridview record into mysql database using foreach

From Dev

Moodle unable to insert record into database php

From Dev

Unable to Insert Data into MySQL Database using (PHP, AJAX, JQ)

From Dev

Unable to insert data in MySQL database

From Dev

MySQL INSERT record using SELECT

From Dev

Unable to insert new record in SQLite using Emulators

From Dev

Unable to INSERT dates and times into MYSQL database with PHP

From Dev

IOS unable to insert data in to mysql database

From Dev

unable to Insert values in database mysql with image

From Dev

Unable to INSERT dates and times into MYSQL database with PHP

From Dev

Unable to insert null values using setNull into the MySql database using Java on the fly?

From Dev

Unable to insert values into database using ajax call

From Dev

unable to insert into database using msqli_query

From Dev

unable to insert data in database using json

From Dev

unable to insert into database using msqli_query

From Dev

Unable to insert everything in database using laravel

From Dev

mongodb - unable to insert record

From Dev

Unable to insert record in MongoDB

From Dev

Duplicate database record on insert

From Dev

perl DBI connect to mysql database without password

From Dev

Unable to insert into table using PDO in mysql

From Dev

Unable to insert data using mysql query in php

From Dev

Unable to insert data in a MySQL table using PHP

From Dev

Unable to insert data on mysql using php

From Dev

How to insert record into database using spring jpa when there is no conflict

From Dev

Insert records to mysql database with php using Ajax

From Dev

Generate and insert date into MySQL database using php

From Dev

Insert a value in a Mysql database using Python

Related Related

  1. 1

    Unable to insert data into mysql database using AJAX

  2. 2

    How to insert datagridview record into mysql database using foreach

  3. 3

    Moodle unable to insert record into database php

  4. 4

    Unable to Insert Data into MySQL Database using (PHP, AJAX, JQ)

  5. 5

    Unable to insert data in MySQL database

  6. 6

    MySQL INSERT record using SELECT

  7. 7

    Unable to insert new record in SQLite using Emulators

  8. 8

    Unable to INSERT dates and times into MYSQL database with PHP

  9. 9

    IOS unable to insert data in to mysql database

  10. 10

    unable to Insert values in database mysql with image

  11. 11

    Unable to INSERT dates and times into MYSQL database with PHP

  12. 12

    Unable to insert null values using setNull into the MySql database using Java on the fly?

  13. 13

    Unable to insert values into database using ajax call

  14. 14

    unable to insert into database using msqli_query

  15. 15

    unable to insert data in database using json

  16. 16

    unable to insert into database using msqli_query

  17. 17

    Unable to insert everything in database using laravel

  18. 18

    mongodb - unable to insert record

  19. 19

    Unable to insert record in MongoDB

  20. 20

    Duplicate database record on insert

  21. 21

    perl DBI connect to mysql database without password

  22. 22

    Unable to insert into table using PDO in mysql

  23. 23

    Unable to insert data using mysql query in php

  24. 24

    Unable to insert data in a MySQL table using PHP

  25. 25

    Unable to insert data on mysql using php

  26. 26

    How to insert record into database using spring jpa when there is no conflict

  27. 27

    Insert records to mysql database with php using Ajax

  28. 28

    Generate and insert date into MySQL database using php

  29. 29

    Insert a value in a Mysql database using Python

HotTag

Archive