按钮上的状态更改单击php mysql PDO

时间:2014-04-15 12:43:40

标签: php

我正在尝试更改MySQL表中收到的包状态,我猜这个动作效果不好,有人可以发现错误,我正在粘贴下面的代码。

当我将动作代码放在while循环中时,它会将状态更改为Received以获取所有记录。但是当我把它放在while循环之外时,没有任何反应。

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

2 个答案:

答案 0 :(得分:2)

请找到正确输出的更正代码。

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

答案 1 :(得分:0)

您需要定义电子邮件,试试这个:

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

如果您想通过一次提交进行多次更新。为此,您需要为所有项目创建一个表单,如下所示:

<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');
    }
}                                           
 ?>