动态 SQL 插入


Dynamic SQL INSERT

我有一个PHP表单,它有很多字段。我想要的是动态地将字段添加到查询中,该查询根据这些字段的内容在 PostgreSQL 表中插入值。如果一个字段(例如日期、int 等)为空,我不希望它在查询中,因为当我尝试插入表格时,它会导致"语法错误",因为它等待要插入的值。请帮忙!

编辑

INSERT INTO hcd_customermeters ("meterSN", "contractCode",
                "deviceManufacturer", "deviceType",
                "firstInstallationDate", "diameter",
                "pipeID", "operatorCreator", "warehouseDeliveryDate",
                "recordCreation", "SRID") 
            VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)' 

在您的 mysql 查询中定义变量名称不正确的更改名称:

PHP 变量的规则:

1) variable starts with the $ sign, followed by the name of the variable
2) variable name must start with a letter or the underscore character
3) **variable name cannot start with a number**
4) variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case sensitive ($y and $Y are two different variables)

$sql_insert = 'INSERT INTO hcd_customermeters ("meterSN", "contractCode", "deviceManufacturer", "deviceType", "firstInstallationDate", "diameter", "pipeID", "operatorCreator", "warehouseDeliveryDate", "recordCreation", "SRID") 
            VALUES ("'.$variableName."', "'.$variableName."', "'.$variableName."',"'.$variableName."', "'.$variableName."', "'.$variableName."', "'.$variableName."', "'.$variableName."', "'.$variableName."', "'.$variableName."',"'.$variableName."')';

mysql_query($sql_insert);

如果有任何问题,请提及。