-1

我有一个包含许多字段的 PHP 表单。我想要的是动态地将字段添加到在 PostgreSQL 表中插入值的查询中,具体取决于这些字段的内容。如果字段(例如日期、整数等)为空,我不希望它出现在查询中,因为当我尝试插入表时它会导致“语法错误”,因为它等待插入值。请帮忙!

编辑

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)' 
4

1 回答 1

3

在您的 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);

如果有任何问题请提及。

于 2013-10-23T12:27:37.333 回答