in reply to Re^7: DBI, place holders and CGI forms
in thread DBI, place holders and CGI forms

Your select advice -
select col1, col2, col3 from tab_name where (? is null or foo = ?) a +nd (? is null or bar = ?) and (? is null or baz = ?)
.

Replies are listed 'Best First'.
Re^9: DBI, place holders and CGI forms
by mje (Curate) on Jun 17, 2011 at 15:51 UTC

    I don't see how this applies to inserts. Provide an example of what you are trying to do.

      I have a got a HTML form. It has multiple fields. The data should be inserted into the database using DBI. I'd like to use placeholders in the query. They aren't all required fields. I was wondering if I could use your method for dynamically inserting values into the database, or should I just insert everything after initialising in the general form $fieldname = $cgi->param('fieldname') || null;?

        There is no null keyword in Perl.

        The value you're most likely thinking of is undef, and that's what $cgi->param(...) returns if no such field is found. So most likely, the below code already does what you might want:

        $fieldname = $cgi->param('fieldname')

        Note that the browser might send empty strings instead of missing values, depending on how your form is set up. But that's a different problem.