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

  • SEARCH
  • Home
  • 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 7597777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:14:25+00:00 2026-05-30T22:14:25+00:00

I have input fields, but if the users leave it blank, i want to

  • 0

I have input fields, but if the users leave it blank, i want to insert a null value to my database.

I request values like this:

$val1 = htmlspecialchars($_REQUEST["val"], ENT_QUOTES);

And then insert them to the DB:

INSERT INTO `table` ( `val1` , `val2` , `val3`) 
VALUES ('$val1', '$val2', '$val3');

I have tried removing the ‘ around val1, val2, val3 – but then nothing gets inserted into the DB if there is no value. I have also tried something like this:

if (!empty($val1)) {
$val1 = "'".$val1."'";
} else {
$val1 = NULL;}

And then without the ‘ around the values – still no insert to the DB

  • 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-05-30T22:14:26+00:00Added an answer on May 30, 2026 at 10:14 pm

    Ok, first this

    INSERT INTO table ( val1 , val2 , val3)
    VALUES (‘val1’, ‘val2’, ‘val3’);

    will only ever insert the literal values “val1”, “val2” and “val3”. You aren’t using any PHP variables.

    Secondly, use prepared statements and parameter binding to perform your queries to protect yourself from SQL injection. For example, using PDO

    $pdo = new PDO(/* DSN, username and password, just read the docs */);
    
    $stmt = $pdo->prepare('INSERT INTO table (val1, val2, val3) VALUES (?, ?, ?)');
    
    $params = array(
        isset($_REQUEST['val1']) ? $_REQUEST['val1'] : null,
        isset($_REQUEST['val2']) ? $_REQUEST['val2'] : null,
        isset($_REQUEST['val3']) ? $_REQUEST['val3'] : null
    );
    
    $stmt->execute($params);
    

    Also, you shouldn’t HTML encode values going into a database. Perform the encoding when you display the value in an HTML document.

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

Sidebar

Related Questions

I have an input text field, which has a value something by default, but
I have some input fields in a page and i want to validate if
I have 2 input fields and i am using it with a DatePicker like
I have a form that asks users to input numbers into multiple form fields.
I have a input field for users & others like email, telephone etc. I
I have three input fields for collecting telephone numbers from users. I display one
I have input fields, which I process with AJAX and send it on another
I have two input fields, and without changing their names (i.e., I have to
I hope I can explain this right I have two input fields that require
I have a form with many input fields. When I catch the submit form

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.