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

时间:2017-02-13 16:00:37

标签: php pdo

每当我尝试使用下面的PHP脚本插入时,我收到此错误消息发生错误:SQLSTATE [42000]:语法错误或访问冲突:1065查询为空,foreach循环用于处理多个选择值。

<?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>";
            }
     }
     ?>

2 个答案:

答案 0 :(得分:2)

你根本不需要从我能看到的东西中进行预测

<?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>";
        }
     }
?>

当然,如果您想删除一些不必要的代码,可以删除部分代码并将$_POST值直接放入$params数组中。

您还可能希望在执行INSERT之前确保所有$_POSTisset(),或者至少使用默认值初始化标量变量。只是为了完全安全。

答案 1 :(得分:2)

所以跟进我对课程的评论是我的简化答案:

在您的表单所在的页面上添加以下内容:

<?php
require_once("functions/function.php");
require_once("db_connection.php");
require_once("database.php");
require_once('class.handleit.php');

$handle = new HandleIt();

if (isset($_POST['group_name']))
{
    $handle->handleIt($_POST['group_name'], $_POST['group_reg_no'], $_POST['leader_name'], $_POST['leader_contact'], $_POST['region'], $_POST['district'], $_POST['division'],
                     $_POST['ward'], $_POST['village'], $_POST['activity'], $_POST['category'], $_POST['men'], $_POST['women'], $_POST['total']);
}
?>

让您的生活更轻松。制作class.handleit.php并添加:

<?php class HandleIt
{
    public function handleIt($group, $reg, $leader, $contact, $region, $district, $division, $ward, $village, $activity, $category, $men, $women, $total)
    {
        $stmt = $this->prepare("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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?, NOW())");
        $stmt->execute([$group, $reg, $leader, $contact, $region, $district, $division, $ward, $village, $activity, $category, $men, $women, $total]);

        echo "Success!";
    }

    public function fetchIt()
    {
        $stmt = "SELECT * FROM `group_details`";
        $res = $this->prepare($stmt);
        $res->execute();

        while ($row = $res)
        {
            $rows[] = $row;
        }

        return $rows;
    }
}

你会看到有两个类..插入和获取(插入解释了它的自我和获取是这样你可以在所需的页面上运行你的foreach)。

以下是foreach示例:

<?php

$getForeach = $handle->fetchIt();

foreach ($getForeach as $row)
{
    echo "<div>$row->group_name</div>";
    /* and so on.. you get the idea.. */
}

?>

请记住,foreach需要进入需要你的班级和班级名称的页面。