I want to have this table and user will be able t delete row of data if they wish to with the checkbox. But the errors i don't know how to solve:

Notice: Undefined index: itemcode in /home/lh026/public_html/pro1/ostock.php on line 153

Notice: Undefined index: saiz in /home/lh026/public_html/pro1/ostock.php on line 154

Notice: Undefined index: quantity in /home/lh026/public_html/pro1/ostock.php on line 155
1 Records deleted Successfully.

The data is not deleted in database but message show 1 Records deleted Successfully...Please help.

<form name="form1" method="POST" action="">
                        <div id="content2" class="float_r">
                          <h1> </h1>
                          <h4><a href="addstock.php"><img src= "img/add.png" /><strong> Add Items</strong></a></h4>
                          <p><strong><font color="red">
                            <?php 

          if($nume==0)
          {  echo "You currently have no item to show"; }
          else if(isset($_GET['msg']))
          {
            $msg = $_GET['msg'];
            if($msg!='')
            { 
            echo '<table border="1">
                <tr>
                <td><font color="red">'.$msg.'</font></td>
                </tr>
                </table>';
            }
        } 
            ?>
                            </font></strong></p>
                          <table width="747" cellspacing="0" cellpadding="5">
                            <tr bgcolor="#ddd">
                            <th width="61"> </th>
                              <th width="98" align="left">Date </th>
                              <th width="87" align="left">Item Code </th>
                              <th width="76" align="left">Quantity</th>
                              <th width="39" align="left">Size </th>
                              <th width="270" align="left">Remark</th>
                              <th width="44" align="left">ID </th>

                            </tr>
                            <?php

        $start=(isset($_GET['start'])) ? (int)$_GET['start']:0;

            if(strlen($start) > 0 and !is_numeric($start))
            {
                echo "Data Error";
                exit;
            }

            $eu = ($start - 0); 
            $limit = 5;                           
            $this1 = $eu + $limit; 
            $back = $eu - $limit; 
            $next = $eu + $limit; 
            $date =  date("Y-m-d",time() + (8 * 3600)); 

            $result = mysql_query("SELECT orderdb.odate,orderdb.itemcode,orderdb.quantity,orderdb.saiz,orderdb.remark,  
 pstaff.staff_id  
FROM orderdb ,pstaff  
WHERE  orderdb.staff_id =pstaff.staff_id
AND orderdb.staff_id='$id'
AND orderdb.odate='$date'");
            ?>
            <?php
 while($row=mysql_fetch_array($result))
 {
?>

 <tr>

    <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['odate'].$row['itemcode'].$row['saiz']; ?>"></td>
    <td><?php echo $row['odate'];?></td>
    <td><?php echo $row['itemcode'];?></td>
    <td><?php echo $row['quantity'];?></td>
    <td><?php echo $row['saiz'];?></td>
    <td><?php echo $row['remark'];?></td>
     <td><?php echo $row['staff_id'];?></td>
    <?php
    echo"<td><a href='update.php?id=".$row['odate']."'>Update</a></td>";
    ?>
 <?php
  }
 ?>  
 <tr><td><input type="submit" name="delete" value="Delete" id="delete"></td></tr></tr></table>

 <?php
// Check if delete button active, start this
$count = mysql_num_rows($result);
echo "$count";

if(isset($_POST['delete']))
    {
        $date =  date("Y-m-d",time() + (8 * 3600));
        $dcode = $_GET["itemcode"];
        $dsaiz = $_GET['saiz'];
        $dquantity = $_GET['quantity'];
         $delete_id = $_POST['checkbox'];
         $id = count($delete_id );
         if (count($id) > 0)
          {
             foreach ($delete_id as $id_d)
             {
                $sql = "DELETE FROM orderdb WHERE itemcode='$dcode' AND odate='$date' AND saiz='$dsaiz'";
                $delete = mysql_query($sql);
                $sql2 = "UPDATE pStockSize SET quantity=('$dquantity'+quantity) WHERE itemcode='$dcode' AND size='$dsaiz'";
                $update = mysql_query($sql2);
            }
        }
        if($delete)
        {
            echo $id." Records deleted Successfully.";
        }
    }
?>
                        </div>
                        </form>

Recommended Answers

All 12 Replies

It looks like you are using POST on the form

<form name="form1" method="POST" action="">

But then you are trying to set some variables based on GET

$dcode = $_GET["itemcode"];
$dsaiz = $_GET['saiz'];
$dquantity = $_GET['quantity'];

Which won't work unless these were set to POST or the form was using GET instead. You would not recommend using GET through for a delete statement.

i have change to $_POST but the errors are still the same.

Well, you have more issues that just the variables using GET.

The value you are posting is just a string of row data all mashed up together that your script would not be able to make any sense of. And the name of your checkbox is set to "checkbox[]".

<td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $row['odate'].$row['itemcode'].$row['saiz']; ?>"></td>

You should be doing something like posting the itemcode and then basing your delete statement off of that. Or you could create hidden fields with odate and saiz to pass those as well.

<input type="checkbox" name="itemcode" id="checkbox[]" value="<? $row['itemcode']; ?>">
<input type="hidden" name="saiz" value="<?php $row['saiz']; ?>" />
<input type="hidden" name="odate" value="<?php $row['odate']; ?>" />

See how the name on the form element correlates with the name of the post variable $_POST["itemcode"]?

I also wanted to make a quick recommendation, popping in and out of PHP like you are doing and mixing PHP with the HTML is something that can create a headache down the road for you. Trying to read the code makes it much harder, also there are more opportunities for running into problems and then trying to figure them out becomes much more of a task.

I would recommend keeping it all in PHP and then returning the HTML that you want through an echo or print statement.

Even more errors. I'm really having a hard time figuring out these errors.

<td><input type="checkbox" name="itemcode" id="checkbox[]" value="<? $row['itemcode']; ?>">
    <input type="hidden" name="saiz" value="<?php $row['saiz']; ?>" />
    <input type="hidden" name="odate" value="<?php $row['odate']; ?>" /></td>
    <td><?php echo $row['odate'];?></td>

Notice: Undefined index: quantity in /home/lh026/public_html/pro1/ostock.php on line 154

Notice: Undefined index: checkbox in /home/lh026/public_html/pro1/ostock.php on line 155

Warning: Invalid argument supplied for foreach() in /home/lh026/public_html/pro1/ostock.php on line 159

Notice: Undefined variable: delete in /home/lh026/public_html/pro1/ostock.php on line 168

Those errors are for the same reason you got the others. If you are trying to read the POST value of them, then you need to post them through the form as well. You don't get the error for the other that you put in the hidden fields because they are now being posted. For example:

<input type="hidden" name="quantity" value="<?php $row['quantity']; ?>" /></td>

Once you do something like above, now $_POST['quantity'] will actually exist and you can do what you like with it.

After following your advice to hidden the field the errors did reduce..

<td><input type="checkbox" name="itemcode" id="checkbox[]" value="<? $row['itemcode']; ?>">
    <input type="hidden" name="saiz" value="<?php $row['saiz']; ?>" />
    <input type="hidden" name="odate" value="<?php $row['odate']; ?>" />
    <input type="hidden" name="quantity" value="<?php $row['quantity']; ?>" />
    <input type="hidden" name="remark" value="<?php $row['remark']; ?>" />
    <input type="hidden" name="staff_id" value="<?php $row['staff_id']; ?>" />
    <input type="hidden" name="checkbox" value="<?php $row['checkbox']; ?>" />
    <input type="hidden" name="delete" value="<?php $row['delete']; ?>" />
    </td>

but how do i settle this?

Warning: Invalid argument supplied for foreach() in /home/lh026/public_html/pro1/ostock.php on line 166

Notice: Undefined variable: delete in /home/lh026/public_html/pro1/ostock.php on line 175

I am not saying that the below code works without error (I did it really quickly), I just wanted to show you example of what I meant about keeping it all in PHP. In my opinion this is a LOT easier to read and debug for me. I don't run into as many errors and I find my errors quicker when I do it this way. Again, It's just a suggestion based on my experience.

<form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<div id="content2" class="float_r">
<h1></h1>
<h4><a href="addstock.php"><img src= "img/add.png" /><strong> Add Items</strong></a></h4>
<?php 
    if($nume==0)
    {
        echo "You currently have no item to show";
    }
    else if(isset($_GET['msg']))
    {
        $msg = $_GET['msg'];
        if($msg!='')
        {
            echo '<table border="1">
            <tr>
            <td><font color="red">'.$msg.'</font></td>
            </tr>
            </table>';
        }
    }

    echo "<table width='747' cellspacing='0' cellpadding='5'>";
    $tableHeader = "<tr bgcolor='#ddd'>";
    $tableHeader .= "<th width='61'> </th>";
    $tableHeader .= "<th width='98' align='left'>Date </th>";
    $tableHeader .= "<th width='87' align='left'>Item Code </th>";
    $tableHeader .= "<th width='76' align='left'>Quantity</th>";
    $tableHeader .= "<th width='39' align='left'>Size </th>";
    $tableHeader .= "<th width='270' align='left'>Remark</th>";
    $tableHeader .= "<th width='44' align='left'>ID </th>";
    $tableHeader .= "</tr>";
    echo $tableHeader;

    $start=(isset($_GET['start'])) ? (int)$_GET['start']:0;
    if(strlen($start) > 0 and !is_numeric($start))
    {
        echo "Data Error";
        exit;
    }
    $eu = ($start - 0); 
    $limit = 5;                           
    $this1 = $eu + $limit; 
    $back = $eu - $limit; 
    $next = $eu + $limit; 
    $date =  date("Y-m-d",time() + (8 * 3600)); 
    $result = mysql_query("
    SELECT orderdb.odate,orderdb.itemcode,orderdb.quantity,orderdb.saiz,orderdb.remark, pstaff.staff_id  
    FROM orderdb ,pstaff  
    WHERE  orderdb.staff_id = pstaff.staff_id
    AND orderdb.staff_id='$id'
    AND orderdb.odate='$date'");

    while($row = mysql_fetch_array($result))
    {
        $tableData = "<tr><td>";
        $tableData .= "<input type='checkbox' name='itemcode' id='checkbox[]' value='". $row['itemcode'] ."'>";
        $tableData .= "<input type='hidden' name='saiz' value='". $row['saiz'] ."' />";
        $tableData .= "<input type='hidden' name='odate' value='". $row['odate'] ."' />";
        $tableData .= "<input type='hidden' name='quantity' value='". $row['quantity'] ."' />";
        $tableData .= "</td>";
        $tableData .= "<td>". $row['odate'] ."</td>";
        $tableData .= "<td>". $row['itemcode'] ."</td>";
        $tableData .= "<td>". $row['quantity'] ."</td>";
        $tableData .= "<td>". $row['saiz'] ."</td>";
        $tableData .= "<td>". $row['remark'] ."</td>";
        $tableData .= "<td>". $row['staff_id'] ."</td>";
        $tableData .= "<td><a href='update.php?id=".$row['odate']."'>Update</a></td>";
        echo $tableData;
    }

    echo "<tr><td><input type='submit' name='delete' value='Delete' id='delete'></td></tr></tr>
    </table>";


    // Check if delete button active, start this
    $count = mysql_num_rows($result);
    echo "$count";
    if(isset($_POST['delete']))
    {
        $date =  date("Y-m-d",time() + (8 * 3600));
        $dcode = $_POST["itemcode"];
        $dsaiz = $_POST['saiz'];
        $dquantity = $_POST['quantity'];
        $delete_id = $_POST['checkbox'];

        $id = count($delete_id);
        if (count($id) > 0)
        {
            foreach ($delete_id as $id_d)
            {
                $sql = "DELETE FROM orderdb WHERE itemcode='$dcode' AND odate='$date' AND saiz='$dsaiz'";
                $delete = mysql_query($sql) or die (mysql_error());
                $sql2 = "UPDATE pStockSize SET quantity=('$dquantity'+quantity) WHERE itemcode='$dcode' AND size='$dsaiz'";
                $update = mysql_query($sql2) or die (mysql_error());
            }
            echo $id." Records deleted Successfully.";
        }
    }
?>
</div>

The problem with the foreach statement is, foreach expects an array. You are giving it $delete_id which is just a single value of $_POST['checkbox'].

I have to run to a meeting, and figuring out a way to correct that error would take a little more time than I have at the moment. I can look at it later if someone else hasn't done so yet, or if you haven't come up with a solution by then.

I still have time for one week.. thanks in advance.. its my lucky day to have ur guidance..

Hmm, well I haven't done it the way you are doing it for a while now. I usually use an AJAX call to a script that deletes the record, so I avoid having to submit a form or having the page reload.

While I was driving down the road I remembered how it worked with check boxes and realized why you had set the name on it to checkbox[]. I sort laughed and felt stupid for a minute. It is creating an array with the '[]' on the name.

Go ahead and set the name on it back.

<input type="checkbox" name="checkbox[]" id="checkbox[]" value="<? $row['itemcode']; ?>">

Test that and tell me what it gives back.

Wow..it really works..you are genius..thank you so much pixelsoul =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.