0

I am trying to change the package status as received in MySQL table, i guess the action is not performing well, can someone please spot the error, i am pasting the code below.

When i am putting the action code inside the while loop, it changes the status to Received for all the records. But when i am putting it outside the while loop, nothing happens.

<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
    <thead>
        <tr>
            <th>Customer Email</th>
            <th>Shipping Company</th>
            <th>Delivery Date</th>
            <th>Tracking ID</th>
            <th>Destination Address</th>
            <th>Destination ZIP</th>
            <th>Mark As Recieved</th>

        </tr>
    </thead>
    <tbody>
    <?php
        require('config.php');
        $conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
        $sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";

        $q = $conn->prepare($sql);
        $q->execute();

        $q->bindColumn(2, $custemail);
        $q->bindColumn(3, $shipcompany);
        $q->bindColumn(4, $deliverydate);
        $q->bindColumn(5, $trackingid);
        $q->bindColumn(6, $destaddress);
        $q->bindColumn(7, $destzip);
        $q->bindColumn(8, $status);

        while($q->fetch()){                             
    ?>
        <tr class="odd gradeX">
            <td><?php echo $custemail ; ?></td>
            <td><?php echo $shipcompany; ?></td>
            <td><?php echo $deliverydate; ?></td>
            <td><?php echo $trackingid; ?></td>
            <td><?php echo $destaddress; ?></td>
            <td><?php echo $destzip; ?></td>
            <td>
            <?php 
            if($status == "Pending") {
                echo "
                    <form action='#' method='post' name='updatestatus'>
                    <input type='submit' name='submit' value='Mark As Recieved' />
                    </form> 
                ";
            }
            else {
                echo "Recieved";
            }
        }
            ?>                                                
            </td>
        </tr>
    <?php 
    $status = "Recieved";
    if(isset($_POST['submit'])){
        while($q->fetch()) {
            $sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
            $q = $conn->prepare($sql);
            $q->execute(array($status,$custemail));
            header('Location:cust_orders.php');
        }
    }                                           
     ?>
    </tbody>
</table>
15
  • Duplicate of stackoverflow.com/a/5126417/285587 Commented Apr 15, 2014 at 12:46
  • Shouldn't this start from 1? $q->bindColumn(2, $custemail); Commented Apr 15, 2014 at 12:48
  • 2
    @Fred-ii- most likely Column 1 is the row id, which the OP does not want to retrieve/return Commented Apr 15, 2014 at 12:49
  • I was under the impression that PDO needs to follow a sequence @Sean Commented Apr 15, 2014 at 12:50
  • 1
    No, not the field. Do they all have the same value -> id=1 [email protected], id=2 [email protected], etc. If the cust_email is the same value for each record, then you will need to use the id or other unique value in your update. Commented Apr 15, 2014 at 13:17

2 Answers 2

2

Please find the corrected code with the proper output.

 <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
        <thead>
            <tr>
                <th>Customer Email</th>
                <th>Shipping Company</th>
                <th>Delivery Date</th>
                <th>Tracking ID</th>
                <th>Destination Address</th>
                <th>Destination ZIP</th>
                <th>Mark As Recieved</th>

            </tr>
        </thead>
        <tbody>
        <?php
            require('config.php');
            $conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
            $sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";

            $q = $conn->prepare($sql);
            $q->execute();

            $q->bindColumn(1, $pid);
            $q->bindColumn(2, $custemail);
            $q->bindColumn(3, $shipcompany);
            $q->bindColumn(4, $deliverydate);
            $q->bindColumn(5, $trackingid);
            $q->bindColumn(6, $destaddress);
            $q->bindColumn(7, $destzip);
            $q->bindColumn(8, $status);

            while($q->fetch()){                             
        ?>
            <tr class="odd gradeX">
                <td><?php echo $custemail ; ?></td>
                <td><?php echo $shipcompany; ?></td>
                <td><?php echo $deliverydate; ?></td>
                <td><?php echo $trackingid; ?></td>
                <td><?php echo $destaddress; ?></td>
                <td><?php echo $destzip; ?></td>
                <td>
                <?php 
                if($status == "Pending") {
                    echo "
                        <form action='#' method='post' name='updatestatus'>
                        <input type='hidden' name='pid' value='$pid'>
                        <input class='btn btn-inverse' type='submit' name='submit' value='Mark As Recieved'><i class='icon-refresh icon-white'></i></input> 
                        </form> 
                    ";
                }
                else {
                    echo "Recieved";
                }
            }
                ?>                                                
                </td>
            </tr>
        <?php 
        $status = "Recieved";
        if(isset($_POST['submit'])){                                            
            $sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE package_id=?";
            $q = $conn->prepare($sql);
            $q->execute(array($status,$_POST['pid']));
            header('Location:cust_orders.php');                                         
        }                                           
         ?>
        </tbody>
    </table>
Sign up to request clarification or add additional context in comments.

Comments

0

You need to define email, try this:

<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
    <tr>
        <th>Customer Email</th>
        <th>Shipping Company</th>
        <th>Delivery Date</th>
        <th>Tracking ID</th>
        <th>Destination Address</th>
        <th>Destination ZIP</th>
        <th>Mark As Recieved</th>

    </tr>
</thead>
<tbody>
<?php
    require('config.php');
    $conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
    $sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";

    $q = $conn->prepare($sql);
    $q->execute();

    $q->bindColumn(2, $custemail);
    $q->bindColumn(3, $shipcompany);
    $q->bindColumn(4, $deliverydate);
    $q->bindColumn(5, $trackingid);
    $q->bindColumn(6, $destaddress);
    $q->bindColumn(7, $destzip);
    $q->bindColumn(8, $status);

    while($q->fetch()){                             
?>
    <tr class="odd gradeX">
        <td><?php echo $custemail ; ?></td>
        <td><?php echo $shipcompany; ?></td>
        <td><?php echo $deliverydate; ?></td>
        <td><?php echo $trackingid; ?></td>
        <td><?php echo $destaddress; ?></td>
        <td><?php echo $destzip; ?></td>
        <td>
        <?php 
        if($status == "Pending") {
            echo "
                <form action='#' method='post' name='updatestatus'>
                <input type='submit' name='submit' value='Mark As Recieved' />
                <input type='hidden' name='cust_email' value='<?php echo $custemail; ?>' />
                </form> 
            ";
        }
        else {
            echo "Recieved";
        }
    }
        ?>                                                
        </td>
    </tr>
<?php 
$status = "Recieved";
if(isset($_POST['submit'])){
    while($q->fetch()) {
        $sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
        $q = $conn->prepare($sql);
        $q->execute(array($status,$_POST['cust_email']));
        header('Location:cust_orders.php');
    }
}                                           
 ?>
</tbody>

If you want to do multiple update by one submit. For this you need create one form for all items, like this:

<form action='#' method='post' name='updatestatus'>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
    <tr>
        <th>Customer Email</th>
        <th>Shipping Company</th>
        <th>Delivery Date</th>
        <th>Tracking ID</th>
        <th>Destination Address</th>
        <th>Destination ZIP</th>
        <th>Mark As Recieved</th>

    </tr>
</thead>
<tbody>
<?php
    require('config.php');
    $conn = new PDO("mysql:host=".$DB_HOST.";dbname=".$DB_NAME,$DB_USER,$DB_PASS);
    $sql = "SELECT * FROM packages_to_be_shipped_on_bremail_address";

    $q = $conn->prepare($sql);
    $q->execute();

    $q->bindColumn(2, $custemail);
    $q->bindColumn(3, $shipcompany);
    $q->bindColumn(4, $deliverydate);
    $q->bindColumn(5, $trackingid);
    $q->bindColumn(6, $destaddress);
    $q->bindColumn(7, $destzip);
    $q->bindColumn(8, $status);

    while($q->fetch()){                             
?
    <tr class="odd gradeX">
        <td><?php echo $custemail ; ?></td>
        <td><?php echo $shipcompany; ?></td>
        <td><?php echo $deliverydate; ?></td>
        <td><?php echo $trackingid; ?></td>
        <td><?php echo $destaddress; ?></td>
        <td><?php echo $destzip; ?></td>
        <td>
        <?php 
        if($status == "Pending") {
            echo "
                <input type='checkbox' name='cust_email[]' value='<?php echo $custemail; ?>' />
            ";
        }
        else {
            echo "Recieved";
        }
    }
        ?>                                                
        </td>
    </tr>
    <tr class="odd gradeX">
        <td colspan="7">
                <input type='submit' name='submit' value='Mark As Recieved' />
        </td>
    <tr>
</tbody></table></form> 
<?php 
$status = "Recieved";
if(isset($_POST['submit'])){
    while($q->fetch()) {
        $sql = "UPDATE packages_to_be_shipped_on_bremail_address SET status=? WHERE cust_email=?";
        $q = $conn->prepare($sql);
        foreach($_POST['cust_email'] as $cust_email)
        $q->execute(array($status,$cust_email));
        header('Location:cust_orders.php');
    }
}                                           
 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.