dswqz24846 2016-05-02 05:47
浏览 44
已采纳

如何使用php保存数据库中的多个单选按钮而不保存按钮值

how to save multiple radio button in database using php without save button value.

my code :

$user_id = $_POST['user_id'];
foreach ( $_POST as $key => $val ) {   
    if ($key <> 'user_id') {
        $bidder_interst_insert="INSERT INTO bidder_interest_list(id, bidder_id, bidder_interest_name) VALUES ('','$user_id','$val')";
        $bidder_interst_insert_result = mysql_query($bidder_interst_insert);
        if (mysql_affected_rows() > 0) {
            $interest_list_success = "Thank you Successfull insert your interst list.";
            $_SESSION['interest_list_success_msg'] = $interest_list_success;
        } else {
            $insert_error = "interst list Insert Error.";
            $_SESSION['insert_error_msg'] = $insert_error;
            header("location:interest_list.php");
        }
    }
}

This code work but database extra save in save button value how to solved this problem??

enter image description here

  • 写回答

2条回答 默认 最新

  • douzhenggui8171 2016-05-02 06:00
    关注
    foreach ( $_POST as $key => $val ){
    

    You are directly looping the $_POST, so the SAVE button value is also saving in the database. Take the values individually instead of looping the whole $_POST, then the SAVE button value won't be saved in the database.

    And moreover you are using mysql functions which are deprecated, use mysqli or PDO.

    EDIT:: Just take it the same way as u took user_id ==> $variablename = $_POST['fieldname'];


    EDIT::: Let me suppose i have a form like this

    <form name="form1" id="form1" method="post" action="">
            <input type="checkbox" name="products[]" value="A" checked="checked" />A <br />
            <input type="checkbox" name="products[]" value="B" checked="checked" />B <br />
            <input type="checkbox" name="products[]" value="C" checked="checked" />C <br />
            <input type="checkbox" name="products[]" value="D" checked="checked" />D <br />
            <input type="checkbox" name="products[]" value="E" checked="checked" />E <br />
            <input type="checkbox" name="products[]" value="F" checked="checked" />F <br />
    
            <input type="submit" name="save" id="save" value="Save" />
        </form>
    

    then i can do it like:

    <?php
        if(isset($_POST['save']))
        {
            $products = $_POST['products'];
            foreach($products as $key => $value)
            {
                $qry = mysql_query("INSERT INTO tbl(product) VALUES('$value')");
            }
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?