-5
$sql  = "INSERT INTO `employee_master` ( em_first_name, em_middle_name, em_last_name, em_gender, em_DOB, em_DOJ, em_mobile_no, em_alternate_mobile_no, em_landline_no, em_permanent_address, em_corresponding_address, em_email_id ) VALUES(

        '" . $params["txtFirstName"] . "',
        '" . $params["txtMiddleName"] . "', 
        '" . $params["txtLastName"] . "',

        '" . $params["selGender"] . "',
        '" . $params["txtDOB"] . "',
        '" . $params["txtDOJ"] . "',
        '" . $params["txtMobileNo"] . "',
        '" . $params["txtAlternateMobileNo"] . "',
        '" . $params["txtLandlineNo"] . "',
        '" . $params["txtPermanentAddressTextarea"] . "',
        '" . $params["txtCorrespondingAddressTextarea"] . "',
        '" . $params["txtEmailID"] . "');  ";
    //echo $sql;
    echo $result = mysqli_query($this->conn, $sql) or die("Error to insert User.");

}

($('#selReligion').val() == -1 ) ? " " : $('#selReligion').val(),

Here I want to use Conditional operator. Right Now..if selGender is null that It inserts -1 but i want to insert 0 or null

J doe
  • 47
  • 6
  • You code is at risk of [SQL Injection](https://stackoverflow.com/questions/601300/what-is-sql-injection). You should use prepared statements with parameters. – litelite Sep 01 '17 at 12:20
  • You're already using an API that supports **prepared statements** with bounded variable input, you should utilize parameterized queries with placeholders (prepared statements) to protect your database against [SQL-injection](http://stackoverflow.com/q/60174/)! Get started with [`mysqli::prepare()`](http://php.net/mysqli.prepare) and [`mysqli_stmt::bind_param()`](http://php.net/mysqli-stmt.bind-param). – Qirel Sep 01 '17 at 12:21
  • 3
    It's not clear exactly what you're asking. Do you want to insert `-1`, `0` or `null`? – Qirel Sep 01 '17 at 12:23
  • 2
    And what have you tried? Have you looked up what the conditional operator is? When you actually use it, what doesn't work? – David Sep 01 '17 at 12:25
  • – J doe Sep 01 '17 at 12:26
  • The above code is from code that designed like that – J doe Sep 01 '17 at 12:26
  • If the option is not selected -1 will be inserted but I don't want to insert -1. I want to insert null – J doe Sep 01 '17 at 12:27
  • 1
    @Karthik: Then why not just make that value empty in the HTML? – David Sep 01 '17 at 12:28
  • But I am Validating selGender If it is -1 that should alert. – J doe Sep 01 '17 at 12:30
  • 2
    `0 IS NOT NULL` – TGrif Sep 01 '17 at 12:33
  • Please do not dump code in comments. Modify your original post to add any new information. – Jay Blanchard Sep 01 '17 at 12:34
  • yes I want to use null – J doe Sep 01 '17 at 12:35
  • @Karthik: The conditional operator you've edited into the question is JavaScript, not PHP. It's not clear at all what the actual problem is here or what specifically you're trying to accomplish. – David Sep 01 '17 at 12:50

1 Answers1

0

Use the ternary operator. It will insert the 0 instead of -1, as you require. if $params["selGender"] will not be null then it will insert the value otherwise 0. and you can change the condition in that according to your need.

'" . (!empty($params["selGender"])) ? $params["selGender"] : 0 . "',

EDITED:

Other way to achieve this is:

Take a variable:

$selGender = '0';
if(!empty($params["selGender"]))
   $selGender = $params["selGender"];

And then use this variable:

'" . $selGender . "',
Code Lღver
  • 15,573
  • 16
  • 56
  • 75