Jump to content

Recommended Posts

Hi All,

 

I have a bit of a problem inserting a NULL value into my table.

 

I have three field that I join and insert into one column. However, if all three fields are empty I need it to insert a null value and not 0.

 

Please could someone help, here is what I have so far:

 

First doing some sanitisation :

    // SANITIZE AND VALIDATE THE DATA BEING PROCESSED BY THE FORM
$user_id = filter_input(INPUT_POST, 'user_id', FILTER_SANITIZE_STRING); 
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING); 
$sales_reference = filter_input(INPUT_POST, 'sales_reference', FILTER_SANITIZE_STRING);
$msisdn1 = !empty($msisdn1) ? "'$msisdn1'" : NULL; 
$msisdn2 = !empty($msisdn2) ? "'$msisdn2'" : NULL; 
$msisdn3 = !empty($msisdn3) ? "'$msisdn3'" : NULL; 
$msisdn = !empty($msisdn1 . " " .$msisdn2 . " " . $msisdn3) ? "'$msisdn'" : NULL; 
$identity = filter_input(INPUT_POST, 'identity', FILTER_SANITIZE_STRING); 
$sale_type = filter_input(INPUT_POST, 'sale_type', FILTER_SANITIZE_STRING);

Then the INSERT Statements:

 if ($insert_stmt = $mysqli->prepare("
INSERT INTO 
usr_retentions_sales
(
user_id
, username
,   sales_reference
,   msisdn
, identity
,   sale_type
) 
VALUES (?, ?, ?, ?, ?, ?)"))
{
$insert_stmt->bind_param(
'issiii'
,  $user_id
, $username
,  $sales_reference
,  $msisdn
, $identity
,  $sale_type
);
 
Link to comment
https://forums.phpfreaks.com/topic/294741-inserting-null-if-fields-are-blank/
Share on other sites

The second $msisdn isn't defined.  Even if it were, this looks a bit odd.

$msisdn = !empty($msisdn1 . " " .$msisdn2 . " " . $msisdn3) ? "'$msisdn'" : NULL; 

The number of values you are binding doesn't match the number of placeholders.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.