将选中的复选框添加到数据库PHP [英] Adding selected check boxes to the database-PHP

查看:171
本文介绍了将选中的复选框添加到数据库PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续此问题:仅将选中的复选框添加到数据库PHP

我试图只添加指定的复选框到我的数据库。
在这个问题中,我的代码看起来如下:

I am trying to add only specified selected checkboxes to my db. As suggested in that question, my code looks as follows:

    <html>
<head>


<?php 


// First we execute our common code to connection to the database and start the session
require("common.php");

// At the top of the page we check to see whether the user is logged in or not
if(empty($_SESSION['user']))
{
    // If they are not, we redirect them to the login page.
    header("Location: login.php");

    // Remember that this die statement is absolutely critical.  Without it,
    // people can view your members-only content without logging in.
    die("Redirecting to login.php");
}

// Everything below this point in the file is secured by the login system

// We can display the user's username to them by reading it from the session array.  Remember that because
// a username is user submitted content we must use htmlentities on it before displaying it to the user.
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
?>

</head>
<body>
<body>
 <div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="brand" href="#">Notification system</a>
          <div class="nav-collapse collapse">
            <ul class="nav">
              <li class="active"><a href="logout.php">Logout</a></li>
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

    <div class="container">



    </div> 


     <intro>   <b>Hello,</b>
    <u><?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?></u>.</br>
    You are now logged in.
    <u><?php if( $_SESSION['user']['admin'] ==1) echo "you are an admin";
    else echo "you are not an admin"; ?>
    <br /></intro>
<!-- Here I set up a basic html form. When it is submitted, the file done.php is called, and the php method POST, which allows data to be sent.-->
    <form action="done.php" method="POST">
    <!-- This hidden check box is used to check if submit is clicked.-->
     <input type="hidden" name="check_submit" value="1" />
     <!--All the applications are added to an array which we can then use in done.php-->
    <applications><h2>Applications</h2><br><table border="0" cellspacing="10" cellpadding="10">
    <tr>
        <td><b>Application</b></td>
        <td><b>UK</b></td>
        <td><b>Spain</b></td>
        <td><b>France</b></td>
        <td><b>Benelux</b></td>
        <td><b>Germany</b></td>
        <td><b>Sweeden</b></td>
    </tr>
    <tr>
        <td>iManage</td>
        <td><center><input type = "checkbox" name="imanage[0][UK]" value ="UK"/></center></td>
            <td><center><input type = "checkbox" name="imanage[0][SPAIN]" value ="SPAIN"/></center></td>
                <td><center><input type = "checkbox" name="imanage[0][FRANCE]" value ="FRANCE"/></center></td>
                    <td><center><input type = "checkbox" name="imanage[0][BENELUX]" value ="BENELUX"/></center></td>
                        <td><center><input type = "checkbox" name="imanage[0][GERMANY]" value ="GERMANY"/></center></td>
                            <td><center><input type = "checkbox" name="imanage[0][SWEEDEN]" value ="SWEEDEN"/></center></td>
    </tr>
    <tr>
        <td>iReport</td>
        <td><center><input type = "checkbox" name="ireport[]" value="ireportuk"/></center></td>
            <td><center><input type = "checkbox" name="ireport[]" value="ireportspain"/></center></td>
                <td><center><input type = "checkbox" name="ireport[]" value="ireportfrance"/></center></td>
                    <td><center><input type = "checkbox" name="ireport[]" value="ireportbenelux"/></center></td>
                            <td><center><input type = "checkbox" name="ireport[]" value="ireportgermany"/></center></td>
                                    <td><center><input type = "checkbox" name="ireport[]" value="ireportsweeden"/></center></td>

    </tr><tr>
        <td>iQuote</td>
        <td><center><input type = "checkbox" name="iquote[]" value ="iquoteuk"/></center></td>
            <td><center><input type = "checkbox" name="iquote[]" value ="iquotespain"/></center></td>
                <td><center><input type = "checkbox" name="iquote[]" value ="iquotefrance"/></center></td>
                    <td><center><input type = "checkbox" name="iquote[]" value ="iquotebenelux"/></center></td>
                        <td><center><input type = "checkbox" name="iquote[]" value ="iquotegermany"/></center></td>
                            <td><center><input type = "checkbox" name="iquote[]" value ="iquotespain"/></center></td>
    </tr><tr>
        <td>Portal</td>
        <td><center><input type = "checkbox"/ name="portal[]" value="portaluk"></center></td>
            <td><center><input type = "checkbox" name="portal[]" value="portalspain"/></center></td>
                <td><center><input type = "checkbox" name="portal[]" value="portalfrance"/></center></td>
                    <td><center><input type = "checkbox" name="portal[]" value="portalbenelux"/></center></td>
                        <td><center><input type = "checkbox" name="portal[]" value="portalgermany"/></center></td>
                            <td><center><input type = "checkbox" name="portal[]" value="portalsweeden"/></center></td>
    </tr><tr>
    <tr>
        <td>iRepair</td>
        <td><center><input type = "checkbox" name="irepair[]" value="irepairuk"/></center></td>
            <td><center><input type = "checkbox"name="irepair[]" value="irepairspain"/></center></td>
                <td><center><input type = "checkbox" name="irepair[]" value="irepairfrance"/></center></td>
                    <td><center><input type = "checkbox"name="irepair[]" value="irepairbenelux"/></center></td>
                        <td><center><input type = "checkbox"name="irepair[]" value="irepairgermany"/></center></td>
                            <td><center><input type = "checkbox"name="irepair[]" value="irepairsweeden"/></center></td>
    </tr>

</table>


        </applications>

    <servers>
    <!--<h2>Servers</h2><br><input type="checkbox" name="serv[]" value="phoenix">Phoenix<br><br>
    <input type="checkbox" name="serv[]" value="leasea">Lease A<br><br>

    </servers>
    <countries1><h2>Countries</h2><br><input type="checkbox" name="country" value="uk">UK<br><br>
    <input type="checkbox" name="country[]" value="germany">Germany<br><br>
    <input type="checkbox" name="country[]" value="france">France<br><br>

    </countries1>
    <countries2>
    <input type="checkbox" name="country[]" value="spain">Spain<br><br>
    <input type="checkbox" name="country[]" value="benelux">Benelux<br>
    <input type="checkbox" name="country[]" value="sweeden">Sweeden<br><br>
    </countries2> -->

    <submitb>

    <input type="submit" value="Update">

    </submitb>

    </form>


</body>
</html>

done.php

<?php

 // First we execute our common code to connection to the database and start the session 
    require("common.php"); 


 //Check whether the form has been submitted
if (array_key_exists('check_submit', $_POST)) {
   //Converts the new line characters (\n) in the text area into HTML line breaks (the <br /> tag)
     $arr = $_POST["imanage"];
        try {

            // depend on your server setting, you might need this to put it on.
           //$glb_connection->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

            //Loop through all row-value from the forms 
            foreach($arr as $key=>$value){

               if(count(array_filter($value)) >0){

               $data = array( 'UK' => $value['UK'], 'SPAIN' => $value['SPAIN'], 'FRANCE' => $value['FRANCE'], 'BENELUX' => $value['BENELUX'], 'GERMANY' => $value['GERMANY'], 'SWEEDEN' => $value['SWEEDEN'] );  
              $query = $glb_conn->prepare("INSERT INTO imanage (UK, SPAIN, FRANCE,BENELUX,GERMANY,SWEEDEN) VALUES (:UK, :SPAIN, :FRANCE, :BENELUX, :GERMANY, :SWEEDEN)");                           

               $query->bindValue(':UK', $value['UK']);
               $query->bindValue(':SPAIN', $value['SPAIN']);
               $query->bindValue(':FRANCE', $value['FRANCE']);  
               $query->bindValue(':BENELUX', $value['BENELUX']);                                      
               $query->bindValue(':GERMANY', $value['GERMANY']);                        
               $query->bindValue(':SWEEDEN', $value['SWEEDEN']);                        
               $query->execute($data); 
                }
            }

        } //end try
        catch(PDOException $e) {
            echo $e->getMessage();
        }


 /*  if ( isset($_POST['ireport']) ) { 
     $print = implode(', ', $_POST['ireport']); //Converts an array into a single string
   echo $print;
    echo('<br>');
   }
    if ( isset($_POST['iquote']) ) { 
     $print = implode(', ', $_POST['iquote']); //Converts an array into a single string
   echo $print;
   echo('<br>');
}

  if ( isset($_POST['portal']) ) { 
     $print = implode(', ', $_POST['portal']); //Converts an array into a single string
   echo $print;
   echo('<br>');
}
    if ( isset($_POST['irepair']) ) { 
     $print = implode(', ', $_POST['irepair']); //Converts an array into a single string
   echo $print;
   echo('<br>');
}*/

} else {
    echo "You can't see this page without submitting the form.";
}
?>

当我运行这个我得到:
未定义的索引:SPAIN
注意:未定义指数:FRANCE(对于列出的所有国家都是如此)。我不确定如何解决这个。
任何帮助将非常感激。

When I run this I get: Undefined index: SPAIN Notice: Undefined index: FRANCE (does this for all countries listed). I am unsure as to how fix this. Any help would be greatly appreciated.

编辑:
一些研究建议使用print_r可以帮助我诊断问题。 print_r($ arr [0])给出。这使我更困惑,因为它看起来好像它应该工作。
Array([UK] => UK [SPAIN] => SPAIN [FRANCE] =>法国[BENELUX] => BENELUX [德国] =>德国[SWEEDEN] => SWEEDEN)

Some research suggested using print_r may help me diagnose the issue. print_r($arr[0]) gives. This has made me more confused as it looks to me as if it should work. Array ( [UK] => UK [SPAIN] => SPAIN [FRANCE] => FRANCE [BENELUX] => BENELUX [GERMANY] => GERMANY [SWEEDEN] => SWEEDEN )

推荐答案

请尝试执行以下操作,希望您会看到错误所在。

Please try to implement the following and hopefully you will see where the error lies. Then Every body will be happy to help...

处理这个问题的方法:

1.Recommended: Declare your variables. Or use isset() to check if they are declared before referencing them.
2.Set a custom error handler for E_NOTICE and redirect the messages away from the standard output (maybe to a log file). set_error_handler('myHandlerForMinorErrors', E_NOTICE | E_STRICT).
3.Disable E_NOTICE from reporting. A quick way to exclude just E_NOTICE is error_reporting( error_reporting() & ~E_NOTICE ).
4.Suppress the error with the @ operator.

注意:强烈建议您只实施第1点。

Note: It's strongly recommended to implement just point 1.


资料来源: - > 以下是来源

这篇关于将选中的复选框添加到数据库PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆