[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive]

MySQL Perl SQL Insert Example



Here we add two records to the database using an INSERT statement. The data to be entered can be gathered from an html form.

# Use the DBI module
use DBI qw(:sql_types);

# Declare local variables

my ($databaseName, $databaseUser, $databasePw, $dbh);
my ($stmt, sth, @newRow);
my ($telephone);

# Set the parameter values for the connection
$databaseName = "DBI:mysql:yourWebSite_com";
$databaseUser = "yourLoginId";
$databasePw = "yourLoginPassword";

# Connect to the database
# Note this connection can be used to 
# execute more than one statement
# on any number of tables in the database

$dbh = DBI->connect($databaseName, $databaseUser, 
    $databasePw) || die "Connect failed: $DBI::errstr\n";

# Create the statement. 
$stmt = "INSERT INTO Phonebook (Id, Name, Telephone)
         VALUES (‘BBBBB’, ‘Joe Smith’, ‘212-555-1212’)";

# Prepare and execute the SQL query
$sth = $$dbh->prepare($$stmt) 
    || die "prepare: $$stmt: $DBI::errstr";
$sth->execute || die "execute: $$stmt: $DBI::errstr";

# INSERT does not return records

# Clean up the record set
$sth->finish();

# We could add another record here as well

# Create the statement. 
$stmt = "INSERT INTO Phonebook (Id, Name, Telephone)
         VALUES (‘CCCCC’, ‘Marcy Jones’, ‘402-555-1212’)";

# Prepare and execute the SQL query
$sth = $$dbh->prepare($$stmt) 
    || die "prepare: $$stmt: $DBI::errstr";
$sth->execute || die "execute: $$stmt: $DBI::errstr";

# Clean up the record set and the database connection
$sth->finish();
$dbh->disconnect();

>>> NEXT >>>

[an error occurred while processing this directive]