doujiene2845 2013-07-25 16:01
浏览 22

如果某些值为空,则将$ _POST值插入MySQL表中

I have (in short) code like this:

...
mysql_query("INSERT INTO table1 (x,y,z) VALUES (".$_POST['a'].",".$_POST['b'].",\"".$_POST['c']."\")");
...

a is numeric value, b is numeric but empty in form and c is string value, which can be empty(there is no problem).

Tthen for MySQL it looks like this:

INSERT INTO table1 (x,y,z) VALUES (2,,"");

Problem is part of values, where is nothing like in b. Is there any solution to get it work? In database settings, or in PHP. I have big table and it is not possible to check every $_POST value.

thanks

  • 写回答

3条回答 默认 最新

  • du958642589 2013-07-25 16:04
    关注

    Just test for $_POST['b'] value, replacing it by null if empty or not set (adapt the solution to the empty value of $_POST['b'] you're getting from your form):

    mysql_query("INSERT INTO table1 (x,y,z)
        VALUES (".$_POST['a'].",".((isset($_POST['b']) || $_POST['b']=="") ? $_POST['b'] : "NULL").",\"".$_POST['c']."\")");
    

    As an aside, don't use mysql_* extension as they’re deprecated. Use PDO or MSQLi instead.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂