插入记录时出错

时间:2016-04-11 07:10:48

标签: php html mysql

我在编码方面不是很好,我想知道是否有人可以帮助我。我正在使用PHP / MYSQL创建一个包含单选按钮和下拉列表的表单。 当我试图测试并插入记录时,它说 “SORRY!插入记录时出错!”

这是我的编码。

dbconfig.php

 <?php

$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "";
$DB_name = "dblogin";


try
{
    $DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
    $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

include_once 'class.crud.php';

$crud = new crud($DB_con);

?>

附加data.php

<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-save']))
{

    $FteName     = $_POST['FteName'];
    $SalesMgr    = $_POST['SalesMgr'];
    $GEO         = $_POST['GEO'];
    $ProdMgr     = $_POST['ProdMgr'];
    $AeName      = $_POST['AeName'];
    $CustName    = $_POST['CustName'];
    $CustCat     = $_POST['CustCat'];
    $CustPoNum   = $_POST['CustPoNum'];
    $CustPoDate  = $_POST['CustPoDate'];
    $SalesQrt    = $_POST['SalesQrt'];
    $PaymentStat = $_POST['PaymentStat'];
    $SONum       = $_POST['SONum'];
    $AmtPaid     = $_POST['AmtPaid'];
    $DatePaid    = $_POST['DatePaid'];
    $OppoGold    = $_POST['OppoGold'];
    $CheckBy     = $_POST['CheckBy'];

    if($crud->create($FteName=$SalesMgr=$GEO=$ProdMgr=$AeName=$CustName=$CustCat=$CustPoNum=$CustPoDate=$SalesQrt=$PaymentStat=$SONum=$AmtPaid=$DatePaid=$OppoGold=$CheckBy))
    {
        header("Location: add-data.php?inserted");
    }
    else
    {
        header("Location: add-data.php?failure");
    }
}
?>
<?php include_once 'header.php'; ?>
<div class="clearfix"></div>

<?php
if(isset($_GET['inserted']))
{
    ?>
    <div class="container">
    <div class="alert alert-info">
    <strong>Data successfully save. </strong><a href="index.php">HOME</a>!
    </div>
    </div>
    <?php
}
else if(isset($_GET['failure']))
{
    ?>
    <div class="container">
    <div class="alert alert-warning">
    <strong>SORRY!</strong> ERROR while inserting record !
    </div>
    </div>
    <?php
}
?>

<div class="clearfix"></div><br />

<div class="container">


     <form method='post'>

    <table class='table table-bordered'>

        <tr>
            <td>FTE/SR Name:</td>
            <td><input type='text' name='FteName' class='form-control' required></td>
        </tr>

        <tr>
            <td>Sales Manager/Team Leader</td>
            <td><input type='text' name='SalesMgr' class='form-control' required></td>
        </tr>

        <tr>
            <td>GEO</td>
            <td><input type='text' name='GEO' class='form-control' required></td>
        </tr>

        <tr>
            <td>AE (Presales)</td>
            <td><input type='text' name='AeName' class='form-control' required></td>
        </tr>

        <tr>
            <td>Customer Name</td>
            <td><input type='text' name='CustName' class='form-control' required></td>
        </tr>

         <tr>
            <td>Customer Category</td>
            <td><select name='CustCat' class='form-control' required>
            <option value="New">New</option>
            <option value="Installed Base">Installed Base</option>
            <option value="DS-Reffered">DS Reffered</option>
            <option value="DS-Old">DS-Old</option>
            </select></td>
        </tr>

        <tr>
            <td>Custom PO Number</td>
            <td><input type='text' name='CustPoNum' class='form-control' required></td>
        </tr>

        <tr>
            <td>Customer PO Date</td>
            <td><input type='text' name='CustPoDate' class='form-control' required></td>
        </tr>

        <tr>
            <td>SO Number</td>
            <td><input type='text' name='SONum' class='form-control' required></td>
        </tr>

        <tr>
            <td>Sales for Quarter</td>
            <td><Input type = 'Radio' Name ='SalesQrt' value= '1st Quarter' />1st Quarter
            <Input type = 'Radio' Name ='SalesQrt' value= '2nd Quarter' />2nd Quarter                               
            <Input type = 'Radio' Name ='SalesQrt' value= '3rd Quarter'  />3rd Quarter
            <Input type = 'Radio' Name ='SalesQrt' value= '4th Quarter' />4th Quarter</td>
        </tr>

        <tr>
            <td>Payment Status</td>
            <td><Input type = 'Radio' Name ='PaymentStat' value= 'Yes' />Yes                            
            <Input type = 'Radio' Name ='PaymentStat' value= 'No'  />No</td>
        </tr>                

        <tr>
            <td>Amount Paid</td>
            <td><input type='text' name='AmtPaid' class='form-control' required></td>
        </tr>

        <tr>
            <td>Date Paid</td>
            <td><input type='text' name='DatePaid' class='form-control' required></td>
        </tr>

      <tr>
            <td>Enter in Goldmine?</td>
            <td><Input type = 'Radio' Name ='OppoGold' value= 'Yes' />Yes                               
            <Input type = 'Radio' Name ='OppoGold' value= 'No'  />No</td>
        </tr>      

        <tr>
            <td>Verified/Checked By:</td>
            <td><input type='text' name='CheckBy' class='form-control' required></td>
        </tr>

        <tr>
            <td colspan="2">
            <button type="submit" class="btn btn-primary" name="btn-save">
            <span class="glyphicon glyphicon-plus"></span> SAVE
            </button>  
            <a href="index.php" class="btn btn-large btn-success"><i class="glyphicon glyphicon-backward"></i> &nbsp; Back to index</a>
            </td>
        </tr>

    </table>
</form>


</div>

class.crud.php

class crud

{
    private $db;

    function __construct($DB_con)
    {
        $this->db = $DB_con;
    }

    public function create($FteName,$SalesMgr,$GEO,$ProdMgr,$AeName,$CustName,$CustCat,$CustPoNum,$CustPoDate,$SalesQrt,$SONum,$AmtPaid,$AmtDatePaid,$OppoGold,$CheckBy)
    {
        try
        {
            $stmt = $this->db->prepare("INSERT INTO info(FteName,SalesMgr,GEO,ProdMgr,AeName,CustName,CustCat,CustPoNum,CustPoDate,SalesQrt,SONum,AmtPaid,AmtDatePaid,OppoGold,CheckBy) VALUES(:FteName, :SalesMgr, :GEO, :ProdMgr, :AeName, :CustName, :CustCat, :CustPoNum, :CustPoDate, :SalesQrt, :SONum, :AmtPaid, :AmtDatePaid, :OppoGold, :CheckBy)");
            $stmt->bindparam(":FteName",$FteName);
            $stmt->bindparam(":SalesMgr",$SalesMgr);
            $stmt->bindparam(":GEO",$GEO);
            $stmt->bindparam(":ProdMgr",$ProdMgr);
            $stmt->bindparam(":AeName",$AeName);
            $stmt->bindparam(":CustName",$CustName);
            $stmt->bindparam(":CustCat",$CustCat);
            $stmt->bindparam(":CustPoNum",$CustPoNum);
            $stmt->bindparam(":CustPoDate",$CustPoDate);
            $stmt->bindparam(":SalesQrt",$SalesQrt);
            $stmt->bindparam(":SONum",$SONum);
            $stmt->bindparam(":AmtPaid",$AmtPaid);
            $stmt->bindparam(":AmtDatePaid",$AmtDatePaid);
            $stmt->bindparam(":OppoGold",$OppoGold);
            $stmt->bindparam(":CheckBy",$CheckBy);
            $stmt->execute();
            return true;
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();  
            return false;
        }

    }

    public function getID($infoID)
    {
        $stmt = $this->db->prepare("SELECT * FROM info WHERE infoID=:infoID");
        $stmt->execute(array(":infoID"=>$infoID));
        $editRow=$stmt->fetch(PDO::FETCH_ASSOC);
        return $editRow;
    }

    public function update($infoID,$FteName,$SalesMgr,$GEO,$ProdMgr,$AeName,$CustName,$CustCat,$CustPoNum,$CustPoDate,$SalesQrt,$SONum,$AmtPaid,$AmtDatePaid,$OppoGold,$CheckBy)
    {
        try
        {
            $stmt=$this->db->prepare("UPDATE info SET FteName=:FteName, 
                                                       SalesMgr=:SalesMgr, 
                                                       GEO=:GEO, 
                                                       ProdMgr=:ProdMgr,
                                                       AeName=:AeName,
                                                       CustName=:CustName,
                                                       CustCat=:CustCat,
                                                       CustPoNum=:CustPoNum,
                                                       CustPoDate=:CustPoDate,
                                                       SalesQrt=:SalesQrt,
                                                       SONum=:SONum,
                                                       AmtPaid=:AmtPaid,
                                                       AmtDatePaid=:AmtDatePaid,
                                                       OppoGold=:OppoGold,
                                                       CheckBy=:CheckBy
                                                    WHERE infoID=:infoID ");
            $stmt->bindparam(":FteName",$FteName);
            $stmt->bindparam(":SalesMgr",$SalesMgr);
            $stmt->bindparam(":GEO",$GEO);
            $stmt->bindparam(":ProdMgr",$ProdMgr);
            $stmt->bindparam(":AeName",$AeName);
            $stmt->bindparam(":CustName",$CustName);
            $stmt->bindparam(":CustCat",$CustCat);
            $stmt->bindparam(":CustPoNum",$CustPoNum);
            $stmt->bindparam(":CustPoDate",$CustPoDate);
            $stmt->bindparam(":SalesQrt",$SalesQrt);
            $stmt->bindparam(":SONum",$SONum);
            $stmt->bindparam(":AmtPaid",$AmtPaid);
            $stmt->bindparam(":AmtDatePaid",$AmtDatePaid);
            $stmt->bindparam(":OppoGold",$OppoGold);
            $stmt->bindparam(":CheckBy",$CheckBy);
            $stmt->execute();

            return true;    
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();  
            return false;
        }
    }

    public function delete($infoID)
    {
        $stmt = $this->db->prepare("DELETE FROM info WHERE infoID=:infoID");
        $stmt->bindparam(":infoID",$infoID);
        $stmt->execute();
        return true;
    }

    /* paging */

    public function dataview($query)
    {
        $stmt = $this->db->prepare($query);
        $stmt->execute();

        if($stmt->rowCount()>0)
        {
            while($row=$stmt->fetch(PDO::FETCH_ASSOC))
            {
                ?>
                <tr>                
                <td><?php print($row['FteName']); ?></td>
                <td><?php print($row['ProdMgr']); ?></td>
                <td><?php print($row['CustName']); ?></td>
                <td><?php print($row['CustPoNum']); ?></td>
                <td><?php print($row['CustPoDate']); ?></td>                
                <td align="center">
                <a href="../PDO-OOP-CRUD-with-Bootstrap/edit-data.php?edit_id=<?php print($row['infoID']); ?>"><i class="glyphicon glyphicon-edit"></i></a>
                </td>
                <td align="center">
                <a href="../PDO-OOP-CRUD-with-Bootstrap/delete.php?delete_id=<?php print($row['infoID']); ?>"><i class="glyphicon glyphicon-remove-circle"></i></a>
                </td>
                </tr>
                <?php
            }
        }
        else
        {
            ?>
            <tr>
            <td>Nothing here...</td>
            </tr>
            <?php
        }

    }

    public function paging($query,$records_per_page)
    {
        $starting_position=0;
        if(isset($_GET["page_no"]))
        {
            $starting_position=($_GET["page_no"]-1)*$records_per_page;
        }
        $query2=$query." limit $starting_position,$records_per_page";
        return $query2;
    }

    public function paginglink($query,$records_per_page)
    {

        $self = $_SERVER['PHP_SELF'];

        $stmt = $this->db->prepare($query);
        $stmt->execute();

        $total_no_of_records = $stmt->rowCount();

        if($total_no_of_records > 0)
        {
            ?><ul class="pagination"><?php
            $total_no_of_pages=ceil($total_no_of_records/$records_per_page);
            $current_page=1;
            if(isset($_GET["page_no"]))
            {
                $current_page=$_GET["page_no"];
            }
            if($current_page!=1)
            {
                $previous =$current_page-1;
                echo "<li><a href='".$self."?page_no=1'>First</a></li>";
                echo "<li><a href='".$self."?page_no=".$previous."'>Previous</a></li>";
            }
            for($i=1;$i<=$total_no_of_pages;$i++)
            {
                if($i==$current_page)
                {
                    echo "<li><a href='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
                }
                else
                {
                    echo "<li><a href='".$self."?page_no=".$i."'>".$i."</a></li>";
                }
            }
            if($current_page!=$total_no_of_pages)
            {
                $next=$current_page+1;
                echo "<li><a href='".$self."?page_no=".$next."'>Next</a></li>";
                echo "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Last</a></li>";
            }
            ?></ul><?php
        }
    }

    /* paging */

}

1 个答案:

答案 0 :(得分:0)

您的CRUD课程没问题。

您没有将所有15个参数发送到create()方法。

更新

if($crud->create($FteName=$SalesMgr=$GEO=$ProdMgr=$AeName=$CustName=$CustCat=$CustPoNum=$CustPoDate=$SalesQrt=$PaymentStat=$SONum=$AmtPaid=$DatePaid=$OppoGold=$CheckBy))

要:

if($crud->create($FteName, $SalesMgr, $GEO, $ProdMgr, $AeName, $CustName, $CustCat, $CustPoNum, 
$CustPoDate, $SalesQrt, $PaymentStat, $SONum, $AmtPaid, $DatePaid, $OppoGold, $CheckBy))

=替换为,。通过在每个参数后添加=,您将发送赋值运算符。

只发送作为单个参数的最后一个值,而需要15个参数。