dongxiao_0528 2012-02-05 17:44
浏览 60

如何使用PHP将未应答的单选按钮问题作为NULL值发布到MySQL数据库中?

I am using the script below to store values for dynamically created forms in to a MySQL database. Users can return to the form after it is saved to update information.

The application uses radio button form elements, which are only sent through POST if they are answered. The default value in the MySQL database is set to NULL. So if a question is not answered, it remains NULL, which is great.

However, I also use jQuery to allow certain form elements to be reset (i.e. radio buttons can be cleared).

If a user answers a radio button question, saves the form, returns at a later date, and clears the radio button question, the value never gets set back to NULL in my database.

Any ideas on how I can make this happen?

<?php 

require_once('../../includes/connect.php');

$org_id = $_SESSION['ORG_ID'];
$user_id = $_SESSION['USER_ID'];

$dbh = get_org_dbh($org_id);

$sql = "UPDATE health_forms SET ";
$values = array();

foreach ($_POST as $field => $value){

    $sql .= "$field=?, ";
    array_push($values, $value);

}

$sql = rtrim($sql,", ");   
$sql .= " WHERE id=?;
";
array_push($values, $user_id);

try { 

    $sth = $dbh->prepare($sql);
    $sth->execute($values);
}  

header("location: ../index.php?i=completed");

?>
  • 写回答

3条回答 默认 最新

  • dstbp22002 2012-02-05 17:55
    关注

    Set the default to NULL for whatever column you are nullifying, then just skip it in your SQL request. That has worked wonderfully for me. You can do this easily from PHPmyadmin.

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码