#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    Cape Town, WP, South Africa
    Posts
    39
    Rep Power
    14
    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.

    cerlbinperl.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',' ',' ')
    &#0124; &#0124; 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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Location
    Alkmaar, The Netherlands
    Posts
    123
    Rep Power
    15
    Do it like this, using the prepare() and execute() functions:

    cerlbinperl.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',' ',' ') &#0124; &#0124; 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

IMN logo majestic logo threadwatch logo seochat tools logo