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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog