|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi there
Is it possible to have an insert into sql in a for loop, below is the code that prints the parameters but does not insert the code into my database. c erlbinperl.exe;use CGI; use DBI; $q = new CGI; print $q->header; print "<html> <head> </head> <body><table>"; $check = $q->param('1&&size'); $voy = $q->param("voyage"); $repid = $q->param("repid"); $trade = $q->param("trade"); $dbh = DBI->connect('DBI:ODBC:salesforecast',' ',' ') | | die print "cant connect to ODBC"; for ($s=1; $s<10;$s++) { $checkforecast = $q->param("$s&&forcast"); $customerid = $q->param("$s&&customerid"); $customer = $q->param("$s&&customer"); $amount =$q->param("$s&&amount"); $siz = $q->param("$s&&size"); $type = $q->param("$s&&type"); $weight = $q->param("$s&&weight"); $pol = $q->param("$s&&POL"); $pod = $q->param("$s&&pod"); if ($checkforecast eq "on") { $sth = $dbh->do("INSERT INTO Forecast ( Voyage, CustomerID, Size, Type, Weight, POL, POD, Trade, Repid ) VALUES ('$voy', '$customerid', '$siz', '$type', '$weight', '$pol', '$pod', '$trade', '$repid')"); print "<tr> <td>$checkforecast</td> <td>$voy</td> <td>$customer</td> <td>$amount</td> <td>$siz</td> <td>$type</td> <td>$weight</td> <td>$pol</td> <td>$pod</td> <td>$trade</td> <td>$repid</td> </tr>"; } } print "</table></body></html>"; $sth->finish; $dbh->disconnect; Any help would be aprreciated |
|
#2
|
|||
|
|||
|
Do it like this, using the prepare() and execute() functions:
c erlbinperl.exe;use CGI; use DBI; $q = new CGI; print $q->header; print "<html> <head> </head> <body><table>"; $check = $q->param('1&&size'); $voy = $q->param("voyage"); $repid = $q->param("repid"); $trade = $q->param("trade"); $dbh = DBI->connect('DBI:ODBC:salesforecast',' ',' ') | | die print "cant connect to ODBC"; $sth = $dbh->prepare("INSERT INTO Forecast ( Voyage, CustomerID, Size, Type, Weight, POL, POD, Trade, Repid ) VALUES (?,?,?,?,?,?,?,?,?)"); for ($s=1; $s<10;$s++) { $checkforecast = $q->param("$s&&forcast"); $customerid = $q->param("$s&&customerid"); $customer = $q->param("$s&&customer"); $amount =$q->param("$s&&amount"); $siz = $q->param("$s&&size"); $type = $q->param("$s&&type"); $weight = $q->param("$s&&weight"); $pol = $q->param("$s&&POL"); $pod = $q->param("$s&&pod"); if ($checkforecast eq "on") { $sth = $dbh->execute($voy,$customerid,$siz,$type,$weight,$pol,$pod,$trade,$repid); print "<tr> <td>$checkforecast</td> <td>$voy</td> <td>$customer</td> <td>$amount</td> <td>$siz</td> <td>$type</td> <td>$weight</td> <td>$pol</td> <td>$pod</td> <td>$trade</td> <td>$repid</td> </tr>"; } } print "</table></body></html>"; $sth->finish; $dbh->disconnect; ------------------ Good luck, Bas ------------------ E-mail me at: b.vandermeijden@pecoma.nl |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Insert into (sql) in for loop |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|