0

I'm trying to use different variables in a mysql insert query. I've no problem with all string variables, but I can't achieve to make work a double variable when is NULL.

$TemperatureMaxC = NULL;
if (array_key_exists('temperaturemaxc', $component_data))
{
    if ((string)$component_data["temperaturemaxc"] != '') {
        $TemperatureMaxC = "'".(string)$component_data["temperaturemaxc"]."'";
    }        
}
$TemperatureMaxC_cast = ($TemperatureMaxC == NULL ? NULL : $TemperatureMaxC);

$query = $this->db->query("INSERT INTO `Components`
                                        (`ManufacturerName`
                                        ,`ManufacturerPN`
                                        ,`IsDesignPart`
                                        ,`DesignPartID`
                                        ,`Published`
                                        ,`Author`
                                        ,`InternalStock`
                                        ,`LastModified`
                                        ,`TemperatureMaxC`)
                                VALUES
                                        (".$ManufacturerName."
                                       ,".$ManufacturerPN."
                                       ,1
                                       ,(SELECT `DesignPartID`
                                           FROM `DesignToManufacturingReference`
                                           WHERE (`Part Number` = ".$PartNumber." AND `Value1` = ".$Value1." AND `Value2` = ".$Value2." ))
                                       ,".$Published."
                                       ,".$Author."
                                       ,".$InternalStock."
                                       ,".$LastModified."
                                       ,".$TemperatureMaxC_cast."
                                       )");

I've to write the query different when $TemperatureMaxC_cast is NULL?

Thank you in advance :)

OriolBur
  • 11
  • 6
  • 1
    Use prepared statements. You'll never have to worry about quoting or SQL injection. It seriously makes things a lot saner. Check the links for [PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php) and [mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – aynber Oct 11 '17 at 19:23
  • ^ this. What you have done should never be done (no offense, we've all done it) – CptMisery Oct 11 '17 at 19:37

0 Answers0