-5

I am working on Inventory Management System project and i am using JQuery, Php and MySql. The main page is working fine. the error occur whenever i try to edit the existing table or try to add more stock. I got this error message when i create

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

I got no error message for the update.php, just that it never update my table after i run the script.

This is my table structure

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 ;

My code to add items to the table is 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();
        }
    }

?>

My code to edit items on the table is 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";
        }
    }

?>

This is the form for adding more items to the table

<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>

And my jquery is

// 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  */
});

Can someone tell me what i should have done that i didn't notice or my error. Thanks

dovespring
  • 19
  • 2
  • 8
  • In line `$stmt->bindParam(":sdescr", $$stock_descr);` isn't there a `$` too much at the end? I'm not used to that language, but this doesn't look like the rest of the code. – René Vogt Jan 03 '16 at 00:53
  • Thanks for your timely response, that definitely shouldn't be there. i have adjusted it but the error persist.Any suggestion to solve this will be appreciated. Thanks again – dovespring Jan 03 '16 at 01:08

1 Answers1

2

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

should be

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

Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
  • Thanks for your timely response. I just noticed that now and i have adjusted accordingly but the error persist. Is there any other thing that you noticed? Thanks again – dovespring Jan 03 '16 at 01:06
  • Make sure that `$stock_status` and `$date_supplied` indeed are set and are not `null`. Also make sute the values are posted from the client. – Sani Huttunen Jan 03 '16 at 01:11
  • Undefined index notices will not be caused by that `$$`. OP keeps on asking questions about many related issues and sincerely doubt we'll be seeing a green tick next to your answer, given the OP's track record. The fact that they didn't post their HTML form, is the main cause here. Someone voted to reopen, but I will not reopen the question. The notice is clear about why it's failing, from what you may have noticed, there is more than one for it. – Funk Forty Niner Jan 03 '16 at 01:11
  • No but the `Integrity constraint violation` is caused by by the `$$` but you're correct that there is more we need from the OP. – Sani Huttunen Jan 03 '16 at 01:15
  • @Sani Huttunen. Thanks again. Please i just edit my code as provided my jquery as well as my form for update. I need your impute on this. This time around, it just load without updating any value both the new data and edited one. Thanks. – dovespring Jan 03 '16 at 15:22