【问题标题】:I need to add +1 for each pressed radio button option and then add its count to mysql database ?我需要为每个按下的单选按钮选项添加 +1,然后将其计数添加到 mysql 数据库?
【发布时间】:2015-02-13 01:31:36
【问题描述】:

大家好,很抱歉有一些简单的问题,但我尝试的时间太长了 不能这样做。所以问题是这样的:我用三个收音机进行了测试 选项,对于每个被按下的选项,我希望该选项 增加 1 或我想要的任何数字,然后添加每个的计数 单选按钮被按下到数据库???伙计们请帮忙!我 有一个名为 'test' 的 mysql 表,其中包含三个元素:'gr' - varchar - 255 - utf8general_ci,'re' - varchar - 255 - utf8general_ci,'ye' - varchar - 255 - utf8general_ci。所以当有人按下例如 值为绿色的单选按钮,我想在我的 sql 表“测试”中使用“gr” ' 增加 1。请大家解释一下所有可以做到的方法。

    <?php

            $a=mysql_connect('xxxx','xxxx','xxxx');

            $b=mysql_select_db('xxxx',$a);


            if ($_POST){


            if ($_POST['color']=='green'){

            $green=$green+1;

            mysql_query('UPDATE test SET gr='.$green.'');

             }

             if ($_POST['color']=='red'){

            $red=$red+1;

            mysql_query('UPDATE test SET re='.$red.'');

             }

             if ($_POST['color']=='yellow'){

            $yellow=$yellow+1;

            mysql_query('UPDATE test SET ye='.$yellow.'');

             }

             }

            ?>

<form action="http://example.com/indexni.php" method="POST  ">
<input type="radio" name="color" value="green" />Green<br>
<input type="radio" name="color" value="red" />Red<br>
<input type="radio" name="color" value="yellow" />Yellow<br>
<input type ="submit" />
</form>

【问题讨论】:

    标签: php html mysql sql forms


    【解决方案1】:

    将数据库中的行类型更改为 int,因此行将是数字,只需将查询替换为以下示例中给出的代码。

    $query = "UPDATE test SET ye = ye+1;";
    mysql_query($query);
    

    【讨论】:

      【解决方案2】:

      你没有定义 $green 的值,是 -255 吗?是否将“int”设置为表测试中的字段类型?此外,您还没有查询到 mysql 的 sql 语句。 Nejc Rodošek 陈述了查询语句的好例子。

      这里是引用的示例代码。

        <?php
           $a=mysql_connect('xxx','xxx','xxx');
           $b=mysql_select_db('xxx',$a);
           $green = -255; //state the value for updating the value in the       "test"
      
              $voteTable = "<html>";
                   $voteTable .= "<head>";
                   $voteTable .= "<meta charset='UTF-8'>";
                        $voteTable .= "<title>color</title>";
                   $voteTable .= "</head>";
                        $voteTable .= "<body>";
                              $voteTable .= "<form action='testColor.php' method='POST'>";
                                    $voteTable .= "<table border=1>";
                                         $voteTable .= "<tr>";
                                                $voteTable .= "<th colspan=2 > Please choose: </th>";
                                         $voteTable .= "</tr>";
                                         $voteTable .= "<tr>";
                                                $voteTable .= "<td>Green</td>";
                                                $voteTable .= "<td><input type='radio' name='color' value='green' /></td>";
                                         $voteTable .= "</tr>";
                                         $voteTable .= "<tr>";
                                                $voteTable .= "<td>Blue</td>";
                                                $voteTable .= "<td><input type='radio' name='color' value='blue' /></td>";            
                                         $voteTable .= "</tr>";
                                         $voteTable .= "<tr>";
                                                $voteTable .= "<td>Red</td>";
                                                $voteTable .= "<td><input type='radio' name='color' value='red' /></td>";
                                         $voteTable .= "</tr>";
                                    $voteTable .= "</table><br/><br/>";
                                    $voteTable .= "<br/><input type='submit' name='submit' value='Go!'  /><br/>";
                              $voteTable .= "</form>";
                   $colorForm .= "</body>";
              $colorForm .= "</html>";
              echo $colorForm;
      
          if(isset($_POST["submit"])){
              if ($_POST['color']=='green'){//example of green, blue and red are same with blue
                   $green+=1;
                   $query = "UPDATE test SET gr='" . $green . "'";
                   mysql_query($query);
              }
         }
      

      ?>

      【讨论】:

        猜你喜欢
        • 2012-04-12
        • 2019-02-24
        • 2013-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-21
        • 2012-10-05
        • 1970-01-01
        相关资源
        最近更新 更多