将单选按钮的值发送到数据库的不同字段

发布于 2024-09-16 20:08:44 字数 868 浏览 3 评论 0原文

我有一个提交表单,其中包含 1 组单选按钮。

<div id="defectclass">
        <input id="def1" type="radio" class="defect" name="defect" value="1"/>S
        <input id="def2" type="radio" class="defect" name="defect" value="1" />A
        <input id="def3" type="radio" class="defect" name="defect" value="1" />B
        <input id="def4" type="radio" class="defect" name="defect" value="1" />C
</div>

除此之外,我在数据库表中有4个字段,即:

  1. S
  2. A
  3. B
  4. C

我在提交后想要:

    - if def1 are checked send value to field "S"
     - if def2 are checked send value to field "A"
     - if def3 are checked send value to field "B"
     - if def4 are checked send value to field "C"
     - if all not checked or null send to all fields value="0"

我该怎么做,因为我从未尝试过这个?

i have a submit form that consist of 1 group radio button.

<div id="defectclass">
        <input id="def1" type="radio" class="defect" name="defect" value="1"/>S
        <input id="def2" type="radio" class="defect" name="defect" value="1" />A
        <input id="def3" type="radio" class="defect" name="defect" value="1" />B
        <input id="def4" type="radio" class="defect" name="defect" value="1" />C
</div>

beside that i have 4 fields at DB table, that is :

  1. S
  2. A
  3. B
  4. C

i want after submit:

    - if def1 are checked send value to field "S"
     - if def2 are checked send value to field "A"
     - if def3 are checked send value to field "B"
     - if def4 are checked send value to field "C"
     - if all not checked or null send to all fields value="0"

how do i do that bcoz i've never try this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧时浪漫 2024-09-23 20:08:44
  1. 您只需创建 1 个字段。
  2. 如果您需要数字来表示这些字母,请创建一个数组

像这样,

$defects_arr = array(0,"S","A","B","C");

这个数组可以在许多任务中使用:

  • 用数字表示这些字母,
  • 以验证用户输入,
  • 动态构建此输入

所以,在收到 POST 数据后,只需将其与此数组进行比较:

$defect = '0';
if(!empty($_POST['defect'])) {
  $key = array_search($_POST['defect'],$defect_arr));
}

您将在 $key 中看到一个数字多变的。该号码应存储在数据库中。

  1. You have to make just 1 field.
  2. If you need numbers to represent these letters make an array.

like this

$defects_arr = array(0,"S","A","B","C");

this array can be used in many tasks:

  • to represent these letters with numbers
  • to validate user rinput
  • to build this input dynamically

So, upon receiving your POST data just compare it against this array:

$defect = '0';
if(!empty($_POST['defect'])) {
  $key = array_search($_POST['defect'],$defect_arr));
}

and you will have a number in the $key variable. This number should be stored in the database.

逆蝶 2024-09-23 20:08:44
<div id="defectclass">
        <input id="def1" type="radio" class="defect" name="defect['s']" value="1"/>S
        <input id="def2" type="radio" class="defect" name="defect['a']" value="1" />A
        <input id="def3" type="radio" class="defect" name="defect['b']" value="1" />B
        <input id="def4" type="radio" class="defect" name="defect['c']" value="1" />C
</div>

这将为您提供 $_POST['defect']['s'], &tc。这样您就会知道要采取哪条更新路径。有关此语法的更多信息,请参阅 http://php.net/faq.html

<div id="defectclass">
        <input id="def1" type="radio" class="defect" name="defect['s']" value="1"/>S
        <input id="def2" type="radio" class="defect" name="defect['a']" value="1" />A
        <input id="def3" type="radio" class="defect" name="defect['b']" value="1" />B
        <input id="def4" type="radio" class="defect" name="defect['c']" value="1" />C
</div>

This will give you $_POST['defect']['s'], &tc. so you'll know which update path to take. See http://php.net/faq.html for more about this syntax.

不奢求什么 2024-09-23 20:08:44

通过查看defect的可能值,在表定义中将

defect VARCHAR(1)更改为defect VARCHAR(1)DEFAULT "0" >

这简化了您在 php 脚本中插入行/更新列的代码

,解析表单后,添加 switch 语句来决定更新哪一列。



// I assumed the row already exists

mysql_connect (localhost, $username, $password);
@mysql_select_db ($database) or die("Error");

$defect = mysql_real_escape_string($_POST["defect"]); //parse the defect field
$id = mysql_real_escape_string$_POST["id"]); //primary key of the table
$flag = True;

if(!empty($_POST['defect']))
{

   switch($defect)
   {
       case "S" : $var = "S";
                  break;
       case "A" : $var = "A";
                  break;
       case "B" : $var = "B";
                  break;
       case "C" : $var = "C";
                  break;
       default : $flag = False;
   }

   if($flag)
   {
      $query = "UPDATE TABLE_NAME SET" + $var + " = '1' WHERE Id = '$id'";
      mysql_query($query);
   }

   mysql_close(); 
}


By looking at the possible values of defect, in your table definition,

change defect VARCHAR(1) to defect VARCHAR(1) DEFAULT "0"

This simplifies your code to insert row/update columns

in your php script, after parsing the form, add a switch statement to decide which column to update.



// I assumed the row already exists

mysql_connect (localhost, $username, $password);
@mysql_select_db ($database) or die("Error");

$defect = mysql_real_escape_string($_POST["defect"]); //parse the defect field
$id = mysql_real_escape_string$_POST["id"]); //primary key of the table
$flag = True;

if(!empty($_POST['defect']))
{

   switch($defect)
   {
       case "S" : $var = "S";
                  break;
       case "A" : $var = "A";
                  break;
       case "B" : $var = "B";
                  break;
       case "C" : $var = "C";
                  break;
       default : $flag = False;
   }

   if($flag)
   {
      $query = "UPDATE TABLE_NAME SET" + $var + " = '1' WHERE Id = '$id'";
      mysql_query($query);
   }

   mysql_close(); 
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文