我是PHP的新手,只是按照一个课程并在我的最后一个案例中获得证书。我们正在网店上工作,而且我在更新购物车时遇到了问题。我知道之前已经问过这个问题,但在阅读完答案之后,我无法修复自己的代码。我希望有人可以帮助我。 如果购物车中有1个商品,它会更新,包含更多商品,只会更新一件商品。这是我的购物车代码(自第一篇帖子以来已被编辑为更完整):
<FORM method=get name=cart_quantity
action=winkelwagen.php>
<TABLE border=0 cellSpacing=0 cellPadding=0 width="100%">
<TBODY>
<TR>
<TD>
<TABLE class=productListing border=0 cellSpacing=0
cellPadding=2 width="100%">
<TBODY>
<TR>
<TD class=infoBoxHeading3 align=middle>Verwijder</TD>
<TD class=infoBoxHeading3>Artikel(en)</TD>
<TD class=infoBoxHeading3 align=middle>Aantal</TD>
<TD class=infoBoxHeading3
align=right>Totaal </TD></TR>
<?php
$sqlWinkelwagen = "SELECT * FROM tblorder
INNER JOIN tblorderregels
INNER JOIN tblartikelen
WHERE tblorder.Ordernummer = tblorderregels.Ordernummer AND SessieID = '" . session_id() . "' AND tblorderregels.Artikelnummer = tblartikelen.Artikelnummer";
$rsWinkelwagen = mysqli_query($db, $sqlWinkelwagen);
$WinkelwagenRegel = 0;
while ($rWinkelwagen = mysqli_fetch_array($rsWinkelwagen))
{
?>
<TR class=productListing-even>
<TD class=productListing-data vAlign=top
align=middle><br><br>
<a href="winkelwagen.php?Artikelnummer=<?php echo($rWinkelwagen["Artikelnummer"]);?>&action=del"><img border="0" src="images/bin.png" width="30" height="30"></a>
<input type=hidden name=Artikelnummer value=<?php echo($rWinkelwagen["Artikelnummer"]);?>>
<TD class=productListing-data><BR>
<TABLE border=0 cellSpacing=2 cellPadding=2>
<TBODY>
<TD class=productListing-data align=middle><A
href="http://www.mijnoscommerce.nl/demo/shop1/product_info.php?products_id=35{1}1"><IMG
title=" iPhone 3G - 8GB " border=0
alt="iPhone 3G - 8GB"
src="images/<?php echo($rWinkelwagen["Afbeelding"]);?>" width=100
height=80></A></TD>
<TD class=productListing-data vAlign=top><BR><A
href="http://www.mijnoscommerce.nl/demo/shop1/product_info.php?products_id=35{1}1"><B>
<?php echo($rWinkelwagen["Artikelnaam"]); ?>
</B></A> </TD></TR></TBODY></TABLE></TD>
<TD class=productListing-data vAlign=top
align=middle><BR><BR><INPUT
type="text" value=<?php echo($rWinkelwagen["Aantal"]); ?> size=4
name=AantalArtikel[<?php echo($rWinkelwagen["Artikelnummer"]);?>]
>
<input type=hidden
name=ArtikelID[<?php echo($rWinkelwagen["Artikelnummer"]);?>]
value=<?php echo($rWinkelwagen["Artikelnummer"]);?>
>
<input type=hidden name=action value=upd></TD>
<TD class=productListing-data vAlign=top
align=right><BR><BR><B>Ä
<?php
$TotaalArtikel = $rWinkelwagen["Aantal"] * $rWinkelwagen["Prijs"];
echo($TotaalArtikel);
$TotaalWinkelwagen = $TotaalWinkelwagen + $TotaalArtikel;
?>
</B> </TD>
<?php
} //einde while winkelwagen inhoud
mysqli_free_result($rsWinkelwagen);
?>
</TBODY></TABLE></TD></TR>
<TR>
<TD><IMG border=0 alt=""
src="winkelwagen_files/pixel_trans.gif" width="100%"
height=10></TD></TR>
<TR>
<TD class=main align=right><B>Subtotaal: Ä <?php echo($TotaalWinkelwagen); ?>
</B> </TD></TR>
<TR>
<TD class=stockWarning align=middle><BR>Artikelen met een: ***
Zijn momenteel niet besckikbaar in de door u gevraagde
hoeveelheid.<BR>U kan de geselecteerde hoeveelheid wel laten
staan en bij het afrekenen zal de beschikbare hoeveelheid
getoond worden.</TD></TR>
<TR>
<TD><IMG border=0 alt=""
src="winkelwagen_files/pixel_trans.gif" width="100%"
height=10></TD></TR>
<TR>
<TD>
<TABLE class=infoBox2 border=0 cellSpacing=1 cellPadding=2
width="100%">
<TBODY>
<TR class=infoBoxContents>
<TD>
<TABLE border=0 cellSpacing=0 cellPadding=2 width="100%">
<TBODY>
<TR>
<TD width=10><IMG border=0 alt=""
src="winkelwagen_files/pixel_trans.gif" width=10
height=1></TD>
<TD class=main><INPUT title=" Update Winkelwagen "
border=0 alt="Update Winkelwagen"
src="winkelwagen_files/button_update_cart.gif"
type=image
></TD>
<TD class=main><A
href="index.php"><IMG
title=" Verder winkelen " border=0
alt="Verder winkelen"
src="winkelwagen_files/button_continue_shopping.gif"
width=129 height=16></A></TD>
<TD class=main align=right><A
href="betalingswijze.php"><IMG
title=" Afrekenen " border=0 alt=Afrekenen
src="winkelwagen_files/button_checkout.gif"
width=86 height=16></A></TD>
<TD width=10><IMG border=0 alt=""
src="winkelwagen_files/pixel_trans.gif" width=10
height=1></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FORM>
应用表单后,它将返回到同一页面,顶部显示以下代码:
<?php
require_once 'config.php';
$db = mysqli_connect($strDatabaseServer, $strUsername, $strPassword, $strDatabase);
if(!$db)
die('Fout bij verbinden met database: ' . mysqli_connect_error());
$sql = "SELECT * FROM tblorder where SessieId = '" . session_id() . "'";
if(!$rs = mysqli_query($db, $sql))
die('Fout in query: ' . mysqli_error());
if (mysqli_num_rows($rs) > 0)
{
$r = mysqli_fetch_array($rs);
$intOrdernummer = $r["Ordernummer"]; // bestaande ordernr
mysqli_free_result($rs);
}
//echo ($intOrdernummer);
if (isset($_GET["action"]))
{
switch ($_GET["action"])
{
case "del":
$strSQL = "delete from tblorderregels where Artikelnummer = '" . $_GET["Artikelnummer"] . "' AND Ordernummer = $intOrdernummer";
break;
case "upd":
foreach($_GET['ArtikelID'] as $key => $id){
$Artikelnummer = $id;
$aantal = $_GET['AantalArtikel'][$key];
//$sql2 = "update orders SET quantity = '$quantity' where item_id = '$item_id' ";
$strSQL = "update tblorderregels set Aantal = '" . $aantal . "' where Artikelnummer = '" . $Artikelnummer . "' AND Ordernummer = $intOrdernummer";
}
break;
}
$intGoed = mysqli_query($db, $strSQL);
if (!$intGoed)
die("Foutje in de SQL: " . $strSQL);
}
?>
我尝试使用GET创建唯一变量,但不知何故它不起作用 我希望有人可以提前帮助,谢谢!
答案 0 :(得分:1)
确认:
$_GET['Artikelnummer'] = Artikelnummer
和$_GET['action'] = del
的GET变量$_POST['action'] = upd
的GET变量(因为没有在表单元素中指定的方法),Artikelnummer有一个隐藏的输入,以及一个文本输入数。如果您提交表单(当前HTML中没有提交按钮)有效的方法:
name=AantalArtikel
文字输入并提交表单将更新该项目问题:
name=AantalArtikel
文字输入指的是哪个Artikelnummer 解决方案:
name=AantalArtikel
文字输入必须具有唯一的名称,例如name=AantalArtikel-Artikel1
提示:
print_r($_GET)
查看表单/链接输出是什么,以查看您出错的地方补充评论:
粗略的格式是:
HTML
form
hidden input action="upd"
hidden input name="number_of_items" value="2"
product1 html
hidden input name="product1_name" value="red_car"
text input name="product1_number" value="1"
link ?product_name=NAME1&action=del
product2 html
hidden input name="product2_name" value="blue_car"
text input name="product2_number" value="1"
link ?product_name=NAME2&action=del
submit button
PHP
if GET action=del
-> sanitise input, delete product
if GET action=upd
-> for (i <= GET number_of_items)
-> sanitise input, get variables using $i in the name to loop through the numbers
答案 1 :(得分:0)
我解决了这个问题。 mysql查询不在我的开关中,所以这就是为什么sql查询没有很好地执行。
感谢您的帮助!