dongtigai3875 2017-02-13 16:00
浏览 36
已采纳

我正在尝试使用PDO将数据插入数据库,但每当我尝试查询失败时,我都在学习PDO

Whenever i try to insert using the below php script i get this error message An error occurred: SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty, the foreach loop is used to process multiple select values.

<?php   if(isset($result)) echo $result; ?>
<form class="form-horizontal" action="" method="post" role="form">
    <div class="form-group">
<label for="name" class="col-sm-2 control-label">Group Name</label>
    <div class="col-sm-8">
         <input type="text" class="form-control" name="group_name" placeholder="Group Name" id="name">
    </div>
</div>

<div class="form-group">
<label for="number" class="col-sm-2 control-label">Registration Number</label>
    <div class="col-sm-8">
         <input type="text" class="form-control" name="group_reg_no" placeholder="Registration Number" id="number">
    </div>
</div>

<div class="form-group">
<label for="leader" class="col-sm-2 control-label">Group Leader Name</label>
    <div class="col-sm-8">
     <input type="text" class="form-control" name="leader_name" placeholder="Group Leader Name" id="leader">
    </div>
</div>

<div class="form-group">
<label for="contact" class="col-sm-2 control-label">Group Leader Contact</label>
    <div class="col-sm-8">
     <input type="text" class="form-control" name="leader_contact" placeholder="Group Leader Contact" id="contact">
    </div>
</div>


<?php require_once'first.php'; ?>
<div class="form-group">
<label for="activity" class="col-sm-2 control-label">Group Activity</label>
    <div class="col-sm-3">
     <select class="form-control" name="activity[]"   id="activity" multiple="multiple">
     <option value="">Select Activity</option>
     </select>
    </div>
</div>
<div class="form-group">
<label for="category" class="col-sm-2 control-label">Group Category</label>
    <div class="col-sm-3">
     <select class="form-control" name="category"  id="category" onChange="JoinedOrNot()">
     <option value="">Select Category</option>
     <option value="Men">Men</option>
     <option value="Women">Women</option>
     <option value="Youth">Youth</option>
     <option value="Both">Both</option>
     </select>
    </div>
</div>

<div class="form-group">
<label for="name" class="col-sm-2 control-label">Number Of Men</label>
    <div class="col-sm-8">
    <input type='text' size='5' name="men" id="input1" value="" class='input' disabled></input>
    </div>
</div>

<div class="form-group">
<label for="name" class="col-sm-2 control-label">Number Of Women</label>
    <div class="col-sm-8">
    <input type='text' size='5' name="women" id="input2" value="" class='input' disabled></input>
    </div>
</div>

<div class="form-group">
<label for="total" class="col-sm-2 control-label">Total</label>
    <div class="col-sm-2">
    <input type='text' size='5' id="input4" value="" name="total" disabled></input>
    </div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label"></label>
    <div class="col-sm-8">
    <input type="submit" value="Register a Group" name="submit" class="btn btn-block btn-danger" id="subject"/>
    </div>
</div>
</form>

<?php
     require_once("functions/function.php"); 
     require_once("db_connection.php");
     require_once("database.php");
     if(isset($_POST['group_name'])){
         $group_name = $_POST['group_name'];
         $group_reg_no = $_POST['group_reg_no'];
         $leader_name = $_POST['leader_name'];
         $leader_contact = $_POST['leader_contact'];
         $region = $_POST['region'];
         $district = $_POST['district'];
         $division = $_POST['division'];
         $ward = $_POST['ward'];
         $village = $_POST['village'];
         $activity = $_POST['activity'];
         $category = $_POST['category'];
         $men = $_POST['men'];
         $women = $_POST['women'];
         $total = $_POST['total'];

         try{
             foreach ((array)$_POST['activity'] as $value) {
                $sqlInsert = "INSERT INTO group_details 
                                    (group_reg_no, group_name, leader_name, 
                                    leader_contact, region, district, division, 
                                    ward, village, activity, 
                                    category, men, women, 
                                    total, registred_date) 
                            VALUES (:group_reg_no, :group_name, :leader_name, 
                                    :leader_contact, :region, :district,                                        :division, 
                                    :ward, :village, :activity,
                                    :category, :men, :women, 
                                    :total, now())";

            }
            $statement = $db->prepare($sqlInsert);
            $statement->execute(array(':group_reg_no' => $group_reg_no, ':group_name' => $group_name, ':leader_name' => $leader_name, ':leader_contact' => $leader_contact, ':region' => $region, ':district' => $district, ':division' => $division, ':ward' => $ward, ':village' => $village, ':activity' => $activity, ':category' => $category, ':men' => $men, ':women' => $women, ':total' => $total));

            if($statement->rowCount() == 1){

                $result = "<p style='padding: 20px; color: green;'> Registration Successful</p>";
            }

            }catch (PDOException $ex){

                $result = "<p style='padding: 20px; color: red;'> An error occurred: ".$ex->getMessage()."</p>";
            }
     }
     ?>
</div>
  • 写回答

2条回答 默认 最新

  • doujiyun8846 2017-02-13 16:16
    关注

    You dont need to be foreach'ing at all from what I can see

    <?php
         require_once("functions/function.php"); 
         require_once("db_connection.php");
         require_once("database.php");
         if(isset($_POST['group_name'])){
    
             try{
                 $sql = "INSERT INTO group_details 
                                    (group_reg_no, group_name, leader_name, 
                                     leader_contact, region, district, division, 
                                     ward, village, activity, 
                                     category, men, women, 
                                     total, registred_date) 
                            VALUES (:group_reg_no, :group_name, :leader_name, 
                                    :leader_contact, :region, :district,
                                    :division, :ward, :village, :activity,
                                    :category, :men, :women, 
                                    :total, now())";
    
                $statement = $db->prepare($sql);
    
                $params = array(':group_reg_no' => $_POST['group_reg_no'], 
                                ':group_name' => $_POST['group_name'], 
                                ':leader_name' => $_POST['leader_name'],
                                ':leader_contact' => $_POST['leader_contact'], 
                                ':region' => $_POST['region'], 
                                ':district' => $_POST['district'], 
                                ':division' => $_POST['division'], 
                                ':ward' => $_POST['ward'], 
                                ':village' => $_POST['village'], 
                                ':activity' => $activity, 
                                ':category' => $_POST['category'], 
                                ':men' => $_POST['men'], 
                                ':women' => $_POST['women'], 
                                ':total' => $_POST['total'])
    
                foreach ( $_POST['activity'] as $activity ) {
                    $params[':activity'] = $activity;
    
                    $statement->execute();                
                } // endforeach
                $result = "<p style='padding: 20px; color: green;'> Registration Successful</p>";
            }
            catch (PDOException $ex){
                $result = "<p style='padding: 20px; color: red;'> An error occurred: ".$ex->getMessage()."</p>";
            }
         }
    ?>
    

    Of course you could remove some of the code and put the $_POST values directly into the $params array if you wanted to remove some unnecessary code.

    You also might want to make sure all of those $_POST values isset() before doing the INSERT, or at least initialising the scalar variables each with a default value. Just to be totally safe.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方