dswqz24846
2016-05-02 05:47
浏览 39

如何使用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

图片转代码服务由CSDN问答提供 功能建议

如何在没有保存按钮值的情况下使用php保存数据库中的多个单选按钮。 < p>我的代码:

  $ user_id = $ _POST ['user_id']; 
foreach($ _POST as $ key =&gt; $ val){
 if($  key&lt;&gt;'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()&gt; 0){
 $ interest_list_success =“谢谢你成功插入你的第一个列表。”; 
 $ _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”); 
} 
} 
} 
    
 
 

此代码工作但数据库额外保存在保存按钮值中如何 解决了这个问题??

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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')");
            }
        }
    ?>
    
    点赞 评论
  • duanpo2813 2016-05-02 06:08

    Try this

    unset($_POST['name-of-save-button']); $data = $_POST; foreach ( $data as $key => $val ){//your code here}

    点赞 评论

相关推荐 更多相似问题