0

every one , I face small problem I hope any one help me . It is e-shopping site , I just want little help. the idea after pressing Add to cart link it will transfare him to cart page, there is two link edit and remove , the remove will remove the item , and that is working . but when some one press edit link , textbox in the quantity column will appear ans save link will appear also once he press save , there will be update in the databse

The problem is : 1- how to transfer textbox value from case "showupdate" to another case "savee"? I 've tried to $qq=$_POST['qun']; and send the variable in the link of save beside the action and id but it doesnot work always it said undefined variable 'qun'

The second problem is : the textbox and save link appears in all items on the cart , i do not want that , I want just that item who i press edit beside it , to change to textbox and save link will apper.

i know that i 've put while loop , that is just to make sure that the id of item got it when press link edit only who will get textbox and save link

here is the code please please help me,

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

If I click add link , it will transfare the action="add" and the id of that item after that it will insert to switch statment in add case because there is header ("location..") it will transfare immeddiatly to show case which show us the table edit + remove link once click on edit link it will go to showupdate case which show textbox + save link once click save it will go to savee case.

4

1 回答 1

0

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'] ;

B. 你的代码充满了 SQL 注入尝试mysql_real_escape_string用于所有发布的数据

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

C. 对不起,我可以class为你写完整的,但这是一个例子,你可以让你的代码看起来有多干净

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

//runner.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
    }

}
于 2012-04-28T23:08:33.820 回答