Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:34:51+00:00 2026-06-04T00:34:51+00:00

Suppose your form has some fields and some are just can be simply left

  • 0

Suppose your form has some fields and some are just can be simply left blank by the user to be filled on later.

How do you guys handle it before handling it to your SQL query?

I’m still quite new to PHP and here’s how I did it.

function checkVAR( $str )
{
  $val = htmlentities( $_REQUEST[$str] , ENT_QUOTES , "UTF-8" );
  if ( $val == "" )
    return "NULL";
  return "'" . $val . "'";
}

So later on my query

$query = "INSERT INTO tblName ( idCol , col2 , col3 ) " .
  "VALUES (" . checkVAR('idcol') . "," . checkVAR('col2') . "," . checkVAR('col3') . ");"

I find my method to still quite tedious I wonder if there are other better ways of handling this kind of situation. This function of mine does not do well on returning numbers so I had a separate function that does the quite the same except that it return 0 instead of null.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:34:53+00:00Added an answer on June 4, 2026 at 12:34 am

    First, you should not be encoding HTML characters before you input data into the database. What happens if you want that data to be sent via some non-HTML means of display? It is probably gonna look like gobbledy gook. You should be, at the very least, escaping string data and ensuring numeric data types are cast to the appropriate types. Better yet would be to use prepared statements.

    I’m going to assume that you’re properly using the mysqli extension because the older mysql_* API has been deprecated. You are using the improved extension, right? Right? Ok!

    function getNullOrEscaped($string)
    {
        // casting ensures that we are passing a string to mysqli_real_escape_string
        // and allows us to pass objects with __toString implemented
        $string = (string) $string;
        return ($string === '') ? null : mysqli_real_escape_string($string);
    }
    

    However, I would probably look at using prepared statements

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose your JSON data has a key like @id ... with a non-alphanum character...
Suppose your for-sale software product uses some free open-source library at runtime. Therefore, you
suppose you update your code to revision 10 which is broken. You want to
Suppose I have the following HTML: <form id=myform> <input type='checkbox' name='foo[]'/> Check 1<br/> <input
This question has been asked, in one form or another, a dozen times here,
I'm having a windows form. In this form I've datagrid control which has few
How can I find out if the user pressed F5 to refresh my page
What I am trying to achieve is a form that has a button on
Suppose I have a multi-dimensional array of the form: array ( array('Set_ID' => 1,
Suppose that for every Form in a WinForms application, you want to change the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.