问题是:1-如何将文本框值从案例“showupdate”转移到另一个案例“savee”?
我试过$qq=$_POST['qun'];
并在动作和id旁边的save链接中发送变量,但是它总是起作用总是说未定义的变量'qun'
第二个问题是:文本框和保存链接出现在购物车上的所有项目中,我不希望这样,我只想按下它旁边的那个项目,更改为文本框并保存链接。< / p>
我知道我已经把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 之后它将插入到开关状态添加案例因为有标题(“位置..”)它将立即转移以显示案例,向我们显示表编辑+删除链接一旦点击编辑链接它将进入showupdate案例显示文本框+保存链接一旦单击保存它将转到 保存案例。
答案 0 :(得分:0)
一个。要始终使用$_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'] );
℃。抱歉,我可以为您编写整个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
}
}