复选框内的值不会保存到数据库中

时间:2016-03-18 04:43:24

标签: php

美好的一天,大家。

我遇到一个问题,每当我选中其中一个复选框并添加记录时,这些值都不会显示在数据库中。我几天来一直在努力解决这个问题,我希望你们能帮助我。

非常感谢!

<?php

include('config/config1.php');

if(isset($_SESSION['username'])) {
    //header("location: Home.php");
    $message = "Login First!";
echo "<script type='text/javascript'>alert('$message');</script>";
}

?>

<!DOCTYPE html>
<!-- Priest Records -->
<?php
if(isset($_POST['validatefirstname']))
{   
    include('config/config1.php');
    global $conn; /* para makuha ang variable galing sa config/config1. */
    $p_firstname = $_POST['validatefirstname'];
    $p_middlename = $_POST['validatemiddlename'];
    $p_lastname = $_POST['validatelastname'];
    $p_birthdate = $_POST['validatebirthdate'];
    $p_description = $_POST['validatedescription'];

    $par_name = $_POST['parish_name'];
    $par_add = $_POST['parish_address'];

    $parish1 = $_POST['priest_parish[]'];

    //echo ($p_middlename);
    //echo ($p_lastname);
    //echo ($p_birthdate);

    /****       Query       ****/
    $insrtPriest = mysqli_query($conn, "INSERT INTO priest (priest_lastname,priest_firstname,priest_middlename,birthdate, description) VALUES ('$p_lastname','$p_firstname','$p_middlename','$p_birthdate', '$p_description')");

    $select = "SELECT idPriest from priest where priest_lastname = '$p_lastname' AND priest_firstname = '$p_firstname' AND priest_middlename = '$p_middlename' AND birthdate = '$p_birthdate' AND desciption = '$p_description';";

    mysqli_query($conn, "INSERT INTO parish_assignment VALUES('$par


    ish1', '$select')");


    //$select = mysqli_fetch_array(mysqli_query($conn, "SELECT idPriest FROM PRIEST where priest_lastname = '$p_lastname' AND priest_middlename = '$p_middlename' AND priest_firstname = '$p_firstname' AND birthdate = '$p_birthdate' AND description = '$p_description'")); 


    /*mysqli_query($conn, "INSERT INTO parish_assignment
    VALUES('$select', $select2')");*/

    echo '<script language="javascript">';
    echo 'alert("Data Saved!")';
    echo '</script>';

    mysqli_close($conn);
    header("Location: Priest Records.php");
}
?>
<html lang="en">
    <head>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/jquery.js"></script>
        <script src="js/smoothscroll.js"></script>

        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <title>Priest Records</title>
        <meta name="generator" content="Bootply" />
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <link href="css/table1.css" rel="stylesheet">
        <!--[if lt IE 9]>
            <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->


        <link href="css/styles.css" rel="stylesheet">
    </head>
    <body>
<div class="navbar-wrapper">
  <div class="container">
    <div class="navbar navbar-inverse navbar-static-top">


          <ul class="nav navbar-nav">
            <li><a href="Home.php">Home</a></li>
            <li><a href="About.php">About Us</a></li>
            <li><a href="Reservations.php">Reservations</a></li>

            <?php 
            //echo "<script type='text/javascript'>alert('".$_POST['userout']."');</script>";
            if(isset($_COOKIE['username']))
                if($_COOKIE['username'] == 'admin'){

                      echo "  <li class='dropdown'>
                          <a href='Sacraments.php' class='dropdown-toggle' data-toggle='dropdown'>Sacraments <b class='caret'></b></a>
                          <ul class='dropdown-menu'>
                            <li><a href='Baptism.php'>Baptism</a></li>
                            <li><a href='Confirmation.php'>Confirmation</a></li>
                            <li><a href='checkMarriage.php'>Marriage</a></li>
                          </ul>
                        </li>
                        <li class='dropdown'>
                          <a href='#' class='dropdown-toggle' data-toggle='dropdown'>Records <b class='caret'></b></a>
                          <ul class='dropdown-menu'>
                            <li><a href='Baptism Records.php'>Baptism Records</a></li>
                            <li><a href='Confirmation Records.php'>Confirmation Records</a></li>
                            <li><a href='Marriage Records.php'>Marriage Records</a></li>
                            <li><a href='Priest Records.php'>Priest Records</a></li>
                          </ul>
                        </li>
                        <li><a href='Certificates.php'>Certificates</a></li>
                        <li><a href='Reports.php'>Reports</a></li>";
                 }
                    elseif($_COOKIE['username'] == 'secretary') {

                        echo "  <li class='dropdown'>
                          <a href='Sacraments.php' class='dropdown-toggle' data-toggle='dropdown'>Sacraments <b class='caret'></b></a>
                          <ul class='dropdown-menu'>
                            <li><a href='Baptism.php'>Baptism</a></li>
                            <li><a href='Confirmation.php'>Confirmation</a></li>
                            <li><a href='checkMarriage.php'>Marriage</a></li>
                          </ul>
                        </li>
                        <li class='dropdown'>
                          <a href='#' class='dropdown-toggle' data-toggle='dropdown'>Records <b class='caret'></b></a>
                          <ul class='dropdown-menu'>
                            <li><a href='Baptism Records.php'>Baptism Records</a></li>
                            <li><a href='Confirmation Records.php'>Confirmation Records</a></li>
                            <li><a href='Marriage Records.php'>Marriage Records</a></li>
                            <li><a href='Priest Records.php'>Priest Records</a></li>
                          </ul>
                        </li>
                        <li><a href='Certificates.php'>Certificates</a></li>
                        <li><a href='Reports.php'>Reports</a></li>";

                    }
                    elseif ($_COOKIE['username'] == 'treasurer') {

                        echo " <li><a href='Certificates.php'>Certificates</a></li>";

                    }
                    elseif ($_COOKIE['username'] == 'priest') {

                        echo " <li><a href='Reports.php'>Reports</a></li>";

                    }



            ?>

            <!-- Login Stuff-->
            <?php
            //If no values, Username + Password + Login button are present; otherwise, no.
            if(!isset($_COOKIE['username'])&& !isset($_POST['userout'])&& !isset($_POST['passout'])){ ?>
                <li>
                        <!-- your login link -->                
                    <form id='login_form' name="login_form" class ='navbar-form navbar-right' method='POST' action="<?php echo $_SERVER["PHP_SELF"];?>">
                        <div class='form-group'>
                            <input type='text' class='form-control' name='userin' placeholder='Username' />
                        </div>

                        <div class='form-group'>
                            <input type='password' class='form-control' name='passin' placeholder='Password' />
                        </div>
                    <input type='button' value='Log In' onClick="validateLogin()"/>
                    </form>
                    <form name="login_click" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
                        <input type="hidden" name="userout" id="userout" value="">
                        <input type="hidden" name="passout" id="passout" value="">
                    </form>
                </li>

                <?php 
                } ?>


            <?php
            if(isset($_COOKIE['username'])){ ?>
                <li>
                    <form name="log_out" method="post" class= 'navbar-form navbar-right' action="<?php $_SERVER['PHP_SELF']; ?>">
                    <input type="submit" name="logout" id="logout" value="Log Out">
                    </form>
                </li>
                <?php
            }

            if(isset($_POST['userout'])&&isset($_POST['passout'])){

                $username = $_POST['userout'];

                $password = $_POST['passout'];

                $selectSql=mysqli_query($conn,"SELECT * FROM user where username = '$username'and password = '$password'");
                if(mysqli_fetch_array($selectSql)<1){   
                    header("Refresh: 2; url=Home.php");
                    echo "Error Username or Password";
                }
                else{
                    setcookie("username", $username, time() + 9000, '/');
                    header("Refresh: 0; url=Home.php");
                }
            }
            if(isset($_POST['logout'])) {
                setcookie("username",'', time() + 0, '/');
                header("Refresh: 0; url=Home.php");
            }
            ?>

          </ul>
        </div>

    </div>
  </div><!-- /container -->
</div><!-- /navbar wrapper -->


<!-- Carousel
================================================== -->
<div id="myCarousel" class="carousel slide" name="top">
  <!-- Indicators -->
  <ol class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    <li data-target="#myCarousel" data-slide-to="1"></li>
    <li data-target="#myCarousel" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner">
    <div class="item active">
      <img src="pic/love.jpg" style="width:100%" class="img-responsive">
      <div class="container">
        <div class="carousel-caption">
          <h1>Bootstrap 3 Carousel Layout</h1>
          <p></p>
        </p>
        </div>
      </div>
    </div>
    <div class="item">
      <img src="http://lorempixel.com/2000/600/abstract/1" class="img-responsive">
      <div class="container">
        <div class="carousel-caption">
          <h1>Changes to the Grid</h1>
          <p>Bootstrap 3 still features a 12-column grid, but many of the CSS class names have completely changed.</p>
        </div>
      </div>
    </div>
    <div class="item">
      <img src="http://placehold.it/1500X500" class="img-responsive">
      <div class="container">
        <div class="carousel-caption">
          <h1>Percentage-based sizing</h1>
          <p>With "mobile-first" there is now only one percentage-based grid.</p>
          <p></p>
        </div>
      </div>
    </div>
  </div>
  <!-- Controls -->
  <a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="icon-prev"></span>
  </a>
  <a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="icon-next"></span>
  </a>  
</div>
<!-- /.carousel -->


<div class="right_col" role="main">
    <div class="row">

        <div class="col-md-12 col-sm-12 col-xs-12" style="width:100%; margin:0 auto;">
            <div class="x_panel">


                <div class="x_title">
                    <h2><center><b>Priest</b></center><small></small></h2>

                    <ul class="nav navbar-right panel_toolbox">
                        <li></li>
                        <li></li>
                        <li></li>
                    </ul>
                    <div class="clearfix"></div>
                </div>

                <div class="x_content">

            <form class="form-horizontal form-label-left" >

                        <input type="hidden" name="submitted" value="true"/>

                        <center>
    <div class="right_col TableStyle" style="overflow-x:auto; width=100%" role="main">
        <table>
            <thead>
              <tr>
                <th>Firstname</th>
                <th>Middlename</th>
                <th>Lastname</th>
                <th>Birthday</th>
                <th>Description</th>
                <th></th>
              </tr>
            </thead>

            <?php
                include('config/config1.php');
                $priestlist = mysqli_query($conn,"SELECT * FROM priest WHERE idPriest IS NOT NULL");
                ?>

                <tbody>
                <?php
                    while($row = mysqli_fetch_array($priestlist))
                    {
                ?>      
                        <tr>
                            <td><?php echo $row['priest_firstname'];?></td>
                            <td><?php echo $row['priest_middlename'];?></td>
                            <td><?php echo $row['priest_lastname'];?></td>
                            <td><?php echo $row['birthdate'];?></td>
                            <td><?php echo $row['description'];?></td>

                            <!--save info-->
                            <td><form name="edit" method="post" action="EditPriest.php" >
                                <input type="hidden" id="editpriest" name="editpriest" value="<?php echo $row['idPriest']; ?>">
                                <input type="submit" name="button" value="Edit">
                                </form>
                            </td>

                        </tr>
                <?php           
                    }
                ?>
                </tbody>
        </table>
    </div>
</center>

                        <div class="item form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">First Name <span class="required">*</span></label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <input id="firstname" class="form-control col-md-7 col-xs-12" name="firstname" placeholder="First Name" required="required" type="text" value=""/>
                            </div>
                        </div>

                        <div class="item form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Middle Name <span class="required">*</span></label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <input id="middlename" class="form-control col-md-7 col-xs-12" name="middlename" placeholder="Middle Name" required="required" type="text"/>
                            </div>
                        </div>

                        <div class="item form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Lastname Name <span class="required">*</span></label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <input id="lastname" class="form-control col-md-7 col-xs-12" name="lastname" placeholder="Last Name" required="required" type="text"/>
                            </div>
                        </div>

                        <div class="item form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name">Birthdate <span class="required">*</span></label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                <input id="birthdate" class="datepicker form-control col-md-7 col-xs-12" name="birthdate" placeholder="Select date" required="required" type="date"/>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="name" >Description <span class="required">*</span></label>
                                <div class="col-md-6 col-sm-6 col-xs-12" >

                                    <select name="description" id="description" class="form-control" required="required" style="width:150px">       
                                        <option value="Parish Priest" >Parish Priest</option>
                                        <option value="Parochial Vicars" >Parochial Vicars</option>
                                        <option value="Resident Priest" >Resident Priest</option>
                                        <option value="Guest Priest" >Guest Priest</option>
                                    </select> 
                                </div>
                        </div>

                        <div class="item form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12">Parish Assginment</label>
                                <div class="col-md6 col-sm-6 col-xs-12">
                                    <form action=" " method="POST" >
                                    <table>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="2" value="2"> Ascension of the Lord Parish</td>
                                        <td><input type="checkbox" name="priest_parish[]" id="24" value="24"> Birhen Dolorosa Parish</td>
                                        <td><input type="checkbox" name="priest_parish[]" id ="23" value="23"> Blessed John Paul II Parish</td>

                                        </tr>
                                        <tr>


                                        <td><input type="checkbox" name="priest_parish[]" id="21" value="21"> Holy Family Parish</td>
                                        <td><input type="checkbox" name="priest_parish[]" id="15" value="15"> Immaculate Conception Paris</td>
                                        <td><input type="checkbox" name="priest_parish[]" id="31" value="31">Mary, Help of Christian Parish</td>

                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="29" value="29">Our Lady of Fatima Parish</td>
                                        <td><input type="checkbox" name="priest_parish[]" id="3" value="3">Our Lady of Lourdes Parish</td>
                                        <td><input type="checkbox" name="priest_parish[]" id="14" value="14">Our Lady of the Assumption Parish </td>

                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="27" value="27">Our Lady of the Miraculous Medal Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="18" value="18">Our Mother of Perpetual Help </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="30" value="30">Sacred Heart of Jesus Parish </td>

                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="4" value="4">Saint Francis of Assisi Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="12" value="12">Saint Francis of Assisi Quasi-Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="8" value="8">Saint Francis Xavier Parish </td>


                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="9" value="9">Saint James the Apostle Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="10" value="10">Saint John the Baptist Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="11" value="11">Saint Joseph the Worker Parish </td>

                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="22" value="22">Saint Jude Thaddeus Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="25" value="25">Saint Mary of the Perpetual Rosary Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="20" value="20">San Alfonso de Ligouri Parish </td>

                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="32" value="32">San Antonio de Padua Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="25" value="25">San Francisco de Asis </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="5" value="5">San Isidro Labrador Parish </td>

                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="16" value="16">San Lorenzo Ruiz Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="7" value="7">San Miguel Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="1" value="1">San Pablo Parish </td>

                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="13" value="13">San Pablo Cathedral </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="36" value="36">San Roque Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="34" value="34">Santa Ana Shrine Parish </td>

                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="28" value="28">Santo Niño Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="17" value="17">Santo Rosario Parish </td>
                                        <td><input type="checkbox" name="priest_parish[]" id="6" value="6">Shrine of the Most Holy Infant of Prague </td>

                                        </tr>
                                        <tr>

                                        <td><input type="checkbox" name="priest_parish[]" id="19" value="19">Virgen delos Remedios Parish </td>

                                        </tr>
                                        </table>



                        <div class="ln_solid"></div>
                            <div class="form-group">
                                <div class="col-md-6 col-md-offset-3">
                                    <input type="button" class="btn btn-primary submits" value="Add Record" onClick="validpriest()"></input>
                                </div>


                        </div>
        </div>
            </form>
        </div>
    </div>
</div>

        <div>
          <!-- FOOTER -->
          <footer>
            <p class="pull-right"><a href="#top" class="smoothScroll">Back to top</a></p>
            <p><a href="http://www.bootply.com/62603">Bootply.com</a></p>
          </footer>

        </div>  <!-- /.container -->
        <!-- script references -->
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>


        <form name="validate" method="post" action="Priest Records.php" id="validation" >   
            <input type="hidden" value="" id="validatefirstname" name="validatefirstname"> 
            <input type="hidden" value="" id="validatemiddlename" name="validatemiddlename"> 
            <input type="hidden" value="" id="validatelastname" name="validatelastname"> 
                    <!--UV info-->
            <input type="hidden" value="" id="validatebirthdate" name="validatebirthdate"> 
            <input type="hidden" value="" id="validatedescription" name="validatedescription"> 
        </form>
        <script src="js/validation.js"></script>
        <script src="validation.js"></script>


    </body>
</html>

2 个答案:

答案 0 :(得分:1)

替换此行

$parish1 = $_POST['priest_parish[]'];

$parish1 = $_POST['priest_parish'];

这将返回数组

答案 1 :(得分:0)

试试这个。它的工作

替换

$parish1 = $_POST['priest_parish'];

TO

$parish1 = $_POST['priest_parish'];
$parish1Str = implode(",",$parish1);

AND

在您的查询中,将$parish1替换为$parish1Str;