如何在开关中的 2 个案例之间传输文本框值


how to transfer textboxt value between 2 cases in switch

每一个,我都遇到小问题,希望有人帮助我。这是电子购物网站,我只是想要一点帮助。按添加到购物车链接后的想法会将他转入购物车页面,有两个链接编辑和删除,删除将删除该项目,这是有效的.但是当有人按编辑链接时,数量列中的文本框会出现保存链接,一旦他按保存,就会出现保存链接,数据库中会有更新

问题是:1-如何将文本框值从案例"显示更新"转移到另一个案例"保存"?我试图$qq=$_POST['qun'];并在操作和 id 旁边的保存链接中发送变量,但它并不总是有效,它说未定义的变量"qun">

第二个问题是:文本框和保存链接出现在购物车上的所有项目中,我不希望这样,我只想要我按旁边的编辑的项目,更改为文本框并保存链接将适用。

我知道我已经放了 while 循环,这只是为了确保在按链接编辑时项目的 ID 得到它 谁将获得文本框并保存链接

这是代码请帮助我,

switch($action){
case "add":
{$id=$_REQUEST['id'];
 $query='insert into cart values("1",'.$id.',"1")';
 $result=mysql_query($query);
 header("location:cart.php?action=show");
    break;
}
case "remove":
{ $id=$_REQUEST['id'];
echo $id;
$query='delete from cart where itemId='.$id.'' or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
header ("location:cart.php?action=show");
break;}

    case "showupdate":
    { $id=$_REQUEST['id'];
      $sql2="select * from cart";
      $result2= mysql_query($sql2);
      while($row2 = mysql_fetch_array($result2))
    {   if ( $id == $row2['itemId'])
        {   $totalCost =0;
            $query = "select * from cart inner join items on cart.itemId = items.itemId";
            $result = mysql_query($query);
            ?>
            <table width="100%" border="1"> 
            <?php while($row = mysql_fetch_array($result)){
            $totalCost += ($row["qty"] * $row["itemPrice"]);
            ?>
            <tr>
            <td><img src="<?php echo $row["image"];?>" height="50" width="50"/></td>
            <td><?php echo $row["itemName"]; ?></td>
            <td>SR<?php echo $row["itemPrice"]; ?></td>
            <td>
            <form method="POST" name="form1">
            <input type = "text" name="qun" value="<?php echo $row['qty'];?>" size="10"/> 
            </form>
            </td>
            <td><a href="cart.php?action=savee&id=<?php echo $row["itemId"]; ?>">Save</a></td>
            <td><a href="cart.php?action=remove&id=<?php echo $row["itemId"]; ?>">Remove</a></td>
            </tr>
            <?php } 
           // Increment the total cost of all items
           $totalCost += ($row["qty"] * $row["itemPrice"]);
           $totalCost = $totalCost + ($row["qty"] * $row["itemPrice"]); ?>
           <tr> <td colspan="2"> <a href="homestore.php">Keep Shopping</a></td>
           <td colspan="2"> <b>Total: SR<?php echo $totalCost; ?></b></td></tr>
           </table>
           <?php  } 
        } break;}

    case "savee":
    {  $id=$_REQUEST['id'];
       $sql='update cart set qty='.$_POST['qun'].'where itemId='.$id.''or die(mysql_error());
       $result=mysql_query($sql)or die(mysql_error());
        header ("location:cart.php?action=show");
        break;
    }
    case "show":
    {
    $totalCost =0;
    $query = "select * from cart inner join items on cart.itemId = items.itemId";
    $result = mysql_query($query);
    ?>
    <table width="100%" border="1"> 
        <?php while($row = mysql_fetch_array($result))
        {   
        $totalCost += ($row["qty"] * $row["itemPrice"]);
        ?>
        <tr>
        <td><img src="<?php echo $row["image"];?>" height="50" width="50"/></td>
        <td><?php echo $row["itemName"]; ?></td>
        <td>SR<?php echo $row["itemPrice"]; ?></td>
        <td><a href="cart.php?action=showupdate&id=<?php echo $row["itemId"]; ?>">edit</a></td>
        <td><a href="cart.php?action=remove&id=<?php echo $row["itemId"]; ?>">Remove</a></td>
       </tr>
    <?php } 
    // Increment the total cost of all items
    $totalCost += ($row["qty"] * $row["itemPrice"]);
    $totalCost = $totalCost + ($row["qty"] * $row["itemPrice"]); ?>
    <tr> <td colspan="2"> <a href="homestore.php">Keep Shopping</a></td>
    <td colspan="2"> <b>Total: SR<?php echo $totalCost; ?></b></td></tr>
    </table>
    <?php break; }
    }?>

如果我点击添加链接,它将转换 action="add" 和该项目的 id之后,它将插入到添加大小写中的切换语句,因为有标题("位置.."(,它将立即传输以显示向我们显示表格编辑 + 删除链接的情况,一旦单击编辑链接,它将转到显示更新案例,显示文本框 + 保存链接,单击保存后它将转到萨维案。

A.要始终使用$_POST['qun'],您需要将其保存在会话中,请参阅文档 http://php.net/manual/en/function.session-start.php

session_start();
$_SESSION['qun'] = $_POST['qun'] ;

// to get qun back
echo $_SESSION['qun'] ;

二.您的代码充满了SQL注入 尝试对所有发布的数据使用mysql_real_escape_string

 $id = mysql_real_escape_string ( $_POST ['id'] );

C.抱歉,我可以为您编写整个class,但这是您如何使代码看起来多么干净的示例

include 'runner.class.php';
$runner = new Runner ( $action );
$runner->process ();

跑步者.class.php

class Runner {
    private $action;
    private $id;
    private $qun;
    function __construct($action) {
        $this->action = $action;
        $this->id = mysql_real_escape_string ( $_REQUEST ['id'] );
        $this->qun = mysql_real_escape_string ( $_REQUEST ['id'] );
    }
    function process() {
        switch ($this->action) {
            case "add" :
                $this->add ();
                break;
            case "remove" :
                $this->remove ();
                break;
            case "savee" :
                $this->save ();
                break;
            case "show" :
                $this->show ();
                break;
        }
    }
    function add() {
        $query = 'insert into cart values("1",' . $this->id . ',"1")';
        $result = mysql_query ( $query );
        header ( "Location: cart.php?action=show" );
    }
    function remove() {
        $query = 'delete from cart where itemId=' . $this->id . '' or die ( mysql_error () );
        $result = mysql_query ( $query ) or die ( mysql_error () );
        header ( "Location: cart.php?action=show" );
    }
    function save() {
        $_SESSION ['qun'] = mysql_real_escape_string ( $_POST ['qun'] ); // Save
                                                                         // Quu
        $sql = 'update cart set qty=' . $_POST ['qun'] . 'where itemId=' . $this->id . '' or die ( mysql_error () );
        $result = mysql_query ( $sql ) or die ( mysql_error () );
        header ( "Location: cart.php?action=show" );
        break;
    }
    function showUpdate() {
        $_SESSION ['qun'] = mysql_real_escape_string ( $_POST ['qun'] ); // Save Quu
        // Add Code
    }
    function show() {
        $qun = $_SESSION ['qun']; // Use Qui
         // Add Code
    }
}