Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 2nd, 2001, 05:32 AM
dc1 dc1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: London
Posts: 2 dc1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy

Sorry if this is a no brainer to every one but I just cant get it to work !
I have 2 pages that search and insert data to my mysql database the only problem is that the insert script does not work infact it seems to do nothing !
I have used the same passwords etc on both forms snd the search form works I will include a section of the insert form in this post to see if anyone has any ideas
Thanks a lot
newbie under pressure.



#!/usr/bin/perl

# ---> insert.cgi <---
# CGI to insert data into the SQL database
use CGI;
use DBI;
$q = new CGI;

# Definitions - You need to change these if you use a different db
$db = "inventory";
$table = "machines";
$user = "root";
$password = "iamnotanumber";
# End of definitions

HTML code for page etc

# Build and execute the SQL statement
$SQLstatement = "insert into $table (alias,ip,canonical,owner,misc,ostype)
values (\"$alias\",\"$ip\",\"$canonical\",\"$owner\"
\"$misc\",\"$ostype\"";
dbh->do($SQLstatement);

# print "$name added to database<br><br>";
# print $q->end_html;
}

Reply With Quote
  #2  
Old March 2nd, 2001, 10:17 AM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Wink

The syntax for your INSERT wrong which is why it doesn't work. I don't how much you read up on using DBI but if you look at the 'do()' function you'll see that you cannot just put scalars in wherever you want which in essence, is what you are trying to do. You must hold the place of the variable which is to be inserted with a '?'. then you pass the the scalars which will be used as arguments to the 'do()' function.

So it should look like this:
Code:
$dbh->do("INSERT INTO table VALUES( ?, ?, ?, ? )", undef, $val1, $val2, $val3, $val4 );


If you are doing this INSERT inside any kind of loop, I would recommend using the 'prepare/execute' method rather that the 'do()' method for reasons of efficiency.

Hope that helps.
__________________
- dsb -
Perl Guy

Reply With Quote
  #3  
Old March 2nd, 2001, 10:19 AM
randymorphis randymorphis is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2000
Location: TN USA
Posts: 6 randymorphis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Looks like you might have forgotten a closing parenthesis:

$SQLstatement = "insert into $table (alias,ip,canonical,owner,misc,ostype)
values (\"$alias\",\"$ip\",\"$canonical\",\"$owner\"
\"$misc\",\"$ostype\")";

Reply With Quote
  #4  
Old March 2nd, 2001, 10:42 AM
dc1 dc1 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: London
Posts: 2 dc1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

cheers randymorphis ive tried that it fails to load at all with it in ??
Also to dsb I dont know a lot about 'do()'function but does this make sense (didnt include this in my previous post)sorry to be a pain




sub printform {

print $q->header;
print $q->start_html(-title=>'Network Machines Database',
-BGCOLOR=>'black',
-TEXT=>'white');
print "<CENTER><H1>Network Machines Database</H1></CENTER><HR>";
print $q->startform;
print "Alias: ", $q->textfield(-name => alias,
-size => 16), "<BR>";
print "IP address : ", $q->textfield(-name => ip
-size => 16), "<BR>";
print "Canonical name: ", $q->textfield(-name => canonical,
-size => 16), "<BR>";
print "Owner: ", $q->textfield(-name => owner,
-size => 16), "<BR>";
print "Misc: ", $q->textfield(-name => misc,
-size => 16), "<BR>";
print "OS type: ", $q->textfield(-name => ostype,
-size => 16), "<BR>";
print $q->submit;
print $q->endform;
print $q->end_html;
}

sub results {

($alias, $ip, $canonical, $owner, $misc, $ostype);

$alias = $q->param('alias');
$ip = $q->param('ip');
$canonical = $q->param('canonical');
$owner = $q->param('owner');
$misc = $q->param('misc');
$ostype = $q->param('ostype');

print $q->header;
print $q->start_html(-title=>'Database Results',
-BGCOLOR=>'black',
-TEXT=>'white');

# Tell the script that we will be using
# a MySQL database
($drh, $dbh);
$drh = DBI->install_driver( 'mysql' );

# Establish a connection with the database
# $dbh = $drh->connect($db, $user, $password);

# A simple check to see if we connected
if (!$dbh) {
print "Cannot connect: $DBI::errstr<BR>";
print $q->end_html;
die;
}

# Build and execute the SQL statement
$SQLstatement = "insert into $table (alias,ip,canonical,owner,misc,ostype)
values (\"$alias\",\"$ip\",\"$canonical\",\"$owner\"
\"$misc\",\"$ostype\"";
dbh->do($SQLstatement);

# print "$name added to database<br><br>";
# print $q->end_html;
}



Reply With Quote
  #5  
Old March 2nd, 2001, 10:58 AM
dsb dsb is offline
PerlGuy
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2001
Posts: 714 dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level)dsb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 15 h 44 m 20 sec
Reputation Power: 36
Send a message via AIM to dsb
Talking

Take a look at my last post. Note the syntax I used for the statement. Variables cannot be interpolated into SQL queries like the way you have in your statement:
Code:
$SQLstatement = "insert into $table (alias,ip,canonical,owner,misc,ostype) 
values (\"$alias\",\"$ip\",\"$canonical\",\"$owner\" 
\"$misc\",\"$ostype\")";    # wrong - this is variable interpolation into your query


You must use placeholders when you are creating SQL statements that will hold variables like so:
[code]
$dbh->do( "INSERT INTO tablename VALUES( ?, ?, ?, ? )", undef, $val1,$val2,$val3,$val4 ); # ?'s hold spot for scalars - scalar args to do fill in the blanks held by ?'s


[Edited by dsb on 03-02-2001 at 10:01 AM]

Reply With Quote
  #6  
Old March 2nd, 2001, 03:34 PM
JonLed JonLed is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2000
Location: Indiana
Posts: 614 JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level)JonLed User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 h 49 m 49 sec
Reputation Power: 9
You have _have_ to use place holders, but you should never just put it right in the middle of the code. Here's another method you can use that will work fine:
Code:
$sql = "INSERT INTO table (field1, field2) VALUES (".$dbh->quote($field1).", ".$dbh->quote($field2).")";

$sth = $dbi->do($sql) or die $dbh->errstr;

Although I don't usually create a variable to hold the sql statement itself, I find that to just take up extra room and memory (so you'd just put the sql statement in the do() sub).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > perl to sql page


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway