SQLSTATE [23000]:完整性约束违规:1048列'stock_descr'不能为空

时间:2016-01-03 00:48:16

标签: php jquery mysql

我正在处理库存管理系统项目,我正在使用JQuery,Php和MySql。主页工作正常。每当我尝试编辑现有表或尝试添加更多库存时,就会发生错误。 我创建

时收到此错误消息
Notice: Undefined index: stock_status in C:\xampp\htdocs\stock\create.php on line 14

Notice: Undefined index: date_supplied in C:\xampp\htdocs\stock\create.php on line 15
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'stock_descr' cannot be null

我没有收到update.php的错误消息,只是在运行脚本后它永远不会更新我的表。

这是我的表结构

CREATE TABLE IF NOT EXISTS `stock` (
  `stock_id` tinyint(5) NOT NULL AUTO_INCREMENT,
  `stock_name` varchar(20) NOT NULL,
  `stock_categ` varchar(20) NOT NULL,
  `stock_descr` varchar(50) NOT NULL,
  `stock_comp` varchar(20) NOT NULL,
  `stock_supp` varchar(20) NOT NULL,
  `stock_quan` int(11) NOT NULL,
  `cost` int(11) NOT NULL,
  `stock_status` enum('Available','Inavailable') NOT NULL,
  `date_supplied` date NOT NULL,
  PRIMARY KEY (`stock_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

我在表中添加项目的代码是  create.php

<?php
require_once 'dbconfig.php';


    if($_POST)
    {
        $stock_name = $_POST['stock_name'];
        $stock_categ = $_POST['stock_categ'];
        $stock_descr = $_POST['stock_descr'];
        $stock_comp = $_POST['stock_comp'];
        $stock_supp = $_POST['stock_supp'];
        $stock_quan = $_POST['stock_quan'];
        $cost = $_POST['cost'];
        $stock_status = $_POST['stock_status'];
        $date_supplied = $_POST['date_supplied'];

        try{

            $stmt = $db_con->prepare("INSERT INTO stock(stock_name,stock_categ,stock_descr,stock_comp,stock_supp,stock_quan,cost,stock_status,date_supplied) VALUES(:sname, :scateg, :sdescr,:scomp, :ssupp, :squan,:scost, :sstatus, :ssupplied)");
            $stmt->bindParam(":sname", $stock_name);
            $stmt->bindParam(":scateg", $stock_categ);
            $stmt->bindParam(":sdescr", $$stock_descr);
            $stmt->bindParam(":scomp", $stock_comp);
            $stmt->bindParam(":ssupp", $stock_supp);
            $stmt->bindParam(":squan", $stock_quan);
            $stmt->bindParam(":scost", $cost);
            $stmt->bindParam(":sstatus", $stock_status);
            $stmt->bindParam(":ssupplied", $date_supplied);

            if($stmt->execute())
            {
                echo "Successfully Added";
            }
            else{
                echo "Query Problem";
            }   
        }
        catch(PDOException $e){
            echo $e->getMessage();
        }
    }

?>

我在桌子上编辑项目的代码是  update.php

<?php
require_once 'dbconfig.php';


    if($_POST)
    {
        $id = $_POST['id'];
        $stock_name = $_POST['stock_name'];
        $stock_categ = $_POST['stock_categ'];
        $stock_descr = $_POST['stock_descr'];
        $stock_comp = $_POST['stock_comp'];
        $stock_supp = $_POST['stock_supp'];
        $stock_quan = $_POST['stock_quan'];
        $cost = $_POST['cost'];
        $stock_status = $_POST['stock_status'];
        $date_supplied = $_POST['date_supplied'];

        $stmt = $db_con->prepare("UPDATE stock SET stock_name=:sn, stock_categ=:sc, stock_descr=:sd,stock_comp=:sc,stock_supp=:ss,stock_quan=:sq,cost=:c,stock_status=:ss,date_supplied=:ds WHERE emp_id=:id");
        $stmt->bindParam(":sn", $stock_name);
        $stmt->bindParam(":sc", $stock_categ;
        $stmt->bindParam(":sd", $stock_descr);
        $stmt->bindParam(":sc", $stock_comp);
        $stmt->bindParam(":ss", $stock_supp);
        $stmt->bindParam(":sq", $stock_quan);
        $stmt->bindParam(":c", $cost);
        $stmt->bindParam(":ss", $stock_status;
        $stmt->bindParam(":ds", $date_supplied);
        $stmt->bindParam(":id", $id);

        if($stmt->execute())
        {
            echo "Successfully updated";
        }
        else{
            echo "Query Problem";
        }
    }

?>

这是向表格添加更多项目的表格

<style type="text/css">
#dis{
    display:none;
}
</style>




    <div id="dis">
    <!-- display message here -->
    </div>


     <form method='post' id='stock-SaveForm' action="#">

    <table class='table table-bordered'>

        <tr>
            <td>Stock Name</td>
            <td><input type='text' name='stock_name' class='form-control' placeholder='EX : Piriton' required /></td>
        </tr>

        <tr>
            <td>Category</td>
            <td><input type='text' name='stock_categ' class='form-control' placeholder='EX : Tablet' required></td>
        </tr>

        <tr>
            <td>Description</td>
            <td><input type='text' name='stock_descr' class='form-control' placeholder='EX : Pain Killer' required></td>
        </tr>

        <tr>
            <td>Company</td>
            <td><input type='text' name='stock_comp' class='form-control' placeholder='EX : Dr Mayer' required /></td>
        </tr>

        <tr>
            <td>Supplier</td>
            <td><input type='text' name='stock_supp' class='form-control' placeholder='EX : Akol Pharmacy' required></td>
        </tr>

        <tr>
            <td>Quantity</td>
            <td><input type='text' name='stock_quan' class='form-control' placeholder='EX : 2000' required></td>
        </tr>
        <tr>
            <td>Cost</td>
            <td><input type='text' name='cost' class='form-control' placeholder='EX : 10' required /></td>
        </tr>

        <tr>
            <td>Status</td>
            <td><input type='text' name='stock_status' class='form-control' placeholder='EX : Available/Unavailable' required></td>
        </tr>

        <tr>
            <td>Date Supplied</td>
            <td><input type='text' name='date_supplied' class='form-control' placeholder='EX : 2015-10-10' required></td>
        </tr>

        <tr>
            <td colspan="2">
            <button type="submit" class="btn btn-primary" name="btn-save" id="btn-save">
            <span class="glyphicon glyphicon-plus"></span> Save this Record
            </button>  
            </td>
        </tr>

    </table>
</form>

我的jquery是

// JavaScript Document

$(document).ready(function(){

    /* Data Insert Starts Here */
    $(document).on('submit', '#stock-SaveForm', function() {

       $.post("create.php", $(this).serialize())
        .done(function(data){
            $("#dis").fadeOut();
            $("#dis").fadeIn('slow', function(){
                 $("#dis").html('<div class="alert alert-info">'+data+'</div>');
                 $("#stock-SaveForm")[0].reset();
             });    
         });   
         return false;
    });
    /* Data Insert Ends Here */


    /* Data Delete Starts Here */
    $(".delete-link").click(function()
    {
        var id = $(this).attr("id");
        var del_id = id;
        var parent = $(this).parent("td").parent("tr");
        if(confirm('Sure to Delete ID no = ' +del_id))
        {
            $.post('delete.php', {'del_id':del_id}, function(data)
            {
                parent.fadeOut('slow');
            }); 
        }
        return false;
    });
    /* Data Delete Ends Here */

    /* Get Edit ID  */
    $(".edit-link").click(function()
    {
        var id = $(this).attr("id");
        var edit_id = id;
        if(confirm('Sure to Edit ID no = ' +edit_id))
        {
            $(".content-loader").fadeOut('slow', function()
             {
                $(".content-loader").fadeIn('slow');
                $(".content-loader").load('edit_form.php?edit_id='+edit_id);
                $("#btn-add").hide();
                $("#btn-view").show();
            });
        }
        return false;
    });
    /* Get Edit ID  */

    /* Update Record  */
    $(document).on('submit', '#stock-UpdateForm', function() {

       $.post("update.php", $(this).serialize())
        .done(function(data){
            $("#dis").fadeOut();
            $("#dis").fadeIn('slow', function(){
                 $("#dis").html('<div class="alert alert-info">'+data+'</div>');
                 $("#stock-UpdateForm")[0].reset();
                 $("body").fadeOut('slow', function()
                 {
                    $("body").fadeOut('slow');
                    window.location.href="index.php";
                 });                 
             });    
        });   
        return false;
    });
    /* Update Record  */
});

有人能告诉我我应该做的事情,我没有注意到或我的错误。感谢

1 个答案:

答案 0 :(得分:2)

$stmt->bindParam(":sdescr", $$stock_descr);

应该是

$stmt->bindParam(":sdescr", $stock_descr);