Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Entering a NULL value into MYSQL

Featured Replies

Here I am trying to enter the 'price'. If no price is entered I want the table value inputted to be NULL not 0.00 (which is what it is doing no matter what I have tried.

The tables 'price' column is set to DECIMAL(6,2) NULL.

 

 

if (preg_match ('/^\d\.{1,5}$/', $trimmed['price'])) {

$pr = mysqli_real_escape_string ($dbc, $trimmed['price']);

} else {

$pr = mysqli_real_escape_string ($dbc, "NULL");

 

After the 'else' is not working ... can anyone help?

I would do it like this:

 

<?php

if(!empty($_POST)){

if(!isset($_POST['price'])){

mysql_query("INSERT INTO db_name (price) VALUES('NULL')")or die(mysql_error());

}else{

mysql_query("INSERT INTO db_name (price) VALUES('".$_POST['price']."')")or die(mysql_error());

}

}else{

echo"<form method=post action=".$_SERVER['PHP_SELF'].">

Price: <input type=text name=price>

<input type=submit value=Submit!></form>";

}

?>

 

Maybe I have a typo here, but that's basically what I'd do.

As long as the default is set in Mysql to NULL, you can insert in by using price = ''.

Good point, jamest. If the default is NULL you can use this script:

 

<?php

if(!empty($_POST)){

mysql_query("INSERT INTO db_name (price) VALUES('".$_POST['price']."')")or die(mysql_error());

}else{

echo"<form method=post action=".$_SERVER['PHP_SELF'].">

Price: <input type=text name=price>

<input type=submit value=Submit!></form>";

}

?>

Good point, jamest. If the default is NULL you can use this script:

 

<?php

if(!empty($_POST)){

mysql_query("INSERT INTO db_name (price) VALUES('".$_POST['price']."')")or die(mysql_error());

}else{

echo"<form method=post action=".$_SERVER['PHP_SELF'].">

Price: <input type=text name=price>

<input type=submit value=Submit!></form>";

}

?>

No you shouldn't. You should never put any $_GET or $_POST data straight in to the database. It is not secure. You need to do some prior validation to make sure it is safe to put in the database.

It's the basics. And I thought it would be only accessible for only one person.

also 'null' is the string null either use:

update `table` set `field` = ''

or

update `table` set `field` = null

 

just not

update `table` set `field` = 'null'

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.