提交按钮,将表格的每一行插入该行的数据到数据库中 [英] Submit button on each row of a table that inserts data of that row into database
问题描述
正如你所看到的,我有这个页面,每个表格行都有'add to cart'按钮。比方说,我点击添加到购物车的产品ID为1的表行,我想要productid,productname,cartquantity转移到另一个网页上的购物车。可能吗?
好吧,您可以看到我想将产品ID 1添加到购物车的数量3.但是,当我按下添加到购物车
它总是运行到我最后一次使用shofffwef并添加的产品它代替购物车。不知道发生了什么问题:($ / b>
以下是我添加到购物车按钮的sql代码
<$ p $如果(setset($ _ POST ['insert'])){// $ _ POST [X] X =文本框的名称(***你必须在BASHOP中执行此操作)注意产品名称。
$ queryinsert =insert into cart(productid,productname,cartquantity,amount)SELECT productid,productname,'$ _ POST [cartquantity]','50'FROM product WHERE productid ='$ _ POST [hidden]' ;
mysqli_query($ dbconn,$ queryinsert);
}
是我的代码来显示在第一张图片中的表。
while($ row = mysqli_fetch_array($ result)){
echo< tr>;
echo< td>。< input type ='text'name ='productid'value =。$ row ['productid']。 >< / td>;
echo< td>。< input type ='text'name ='productname'va lue =。$ row ['productname']。>< / td>;
回显< td> 。 $ row ['retailprice']。 < / TD> 中;
回显< td> 。 $ row ['pointsformula']。 < / TD> 中;
echo< td>。< button type ='button'class ='quantityaddsub'id ='sub'onclick ='quantitysub(。$ row ['productid']。)'> ; - < /按钮>中。
< input type ='text'class ='quantity'name ='cartquantity'id ='quantity。$ row ['productid']。'value = 1>。
< button type ='button'class ='quantityaddsub'id ='add'onclick ='quantityadd(。$ row ['productid']。)'> +< / button> 。
< input type ='hidden'name ='hidden'value =。$ row ['productid']。>< / td>;
回显< td> 。 < input type ='submit'name ='insert'value ='加入购物车'。 >< / TD> 中;
回声< / tr>;
}
echo< / table>;
我到处搜索但找不到解决方案,请帮助我!
你需要用自己的形式分隔每一行。假设你想避免使用jQuery($ row = mysqli_fetch_array($ result)){
echo< / p>
tr>< form action ='otherscript.php'method ='POST'>;
echo< td>。< input type ='text'name ='productid'value =。 $ row ['productid']。>< / td>;
echo< td>。< input type ='text'name ='productname'value =。 $ row ['productname']。>< / td>;
回显< td> 。 $ row ['retailprice']。 < / TD> 中;
回显< td> 。 $ row ['pointsformula']。 < / TD> 中;
echo< td>。< button type ='button'class ='quantityaddsub'id ='sub'onclick ='quantitysub(。$ row ['productid']。)'> ; - < /按钮>中。
< input type ='text'class ='quantity'name ='cartquantity'id ='quantity。$ row ['productid']。'value = 1>。
< button type ='button'class ='quantityaddsub'id ='add'onclick ='quantityadd(。$ row ['productid']。)'> +< / button> 。
< input type ='hidden'name ='hidden'value =。$ row ['productid']。>< / td>;
回显< td> 。 < input type ='submit'name ='insert'value ='加入购物车'。 >< / TD> 中;
echo< / form>< / tr>;
}
echo< / table>;
通过这种方式,当您单击提交时,只会发送该行的数据。现在它有点乱。
As you can see, i have this page where there is the 'add to cart' button on each table row. Lets say i click 'add to cart' for the table row where product id is 1, i would want the productid, productname, cartquantity to be transferred to the shopping cart on another webpage. is it possible?
okay so as you can see i would like to add product id 1 to my cart with quantity 3. But when i pressed add to cart
It always runs to my last productid which is shofffwef and adds it to the cart instead. Not sure what is going wrong :(
Here is my sql code for the add to cart button
if(isset($_POST['insert'])){ //$_POST[X] X = name of textbox (***You got to do this In BASHOP) take note the productname.
$queryinsert = "insert into cart(productid,productname,cartquantity,amount) SELECT productid,productname,'$_POST[cartquantity]', '50' FROM product WHERE productid='$_POST[hidden]'";
mysqli_query($dbconn,$queryinsert);
}
And here is my code to show the table as in the first picture.
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>"."<input type = 'text' name='productid' value=". $row['productid'] ."></td>";
echo "<td>"."<input type = 'text' name='productname' value=". $row['productname'] ."></td>";
echo "<td>" . $row['retailprice'] . "</td>";
echo "<td>" . $row['pointsformula'] . "</td>";
echo "<td>"."<button type='button' class='quantityaddsub' id='sub' onclick='quantitysub(".$row['productid'].")'>-</button>".
"<input type='text' class='quantity' name='cartquantity' id='quantity".$row['productid']."' value=1>".
"<button type='button' class='quantityaddsub' id='add' onclick='quantityadd(".$row['productid'].")'>+</button>".
"<input type='hidden' name='hidden' value=".$row['productid']."></td>";
echo "<td>" . "<input type='submit' name='insert' value='Add To Cart'" . "></td>";
echo "</tr>";
}
echo "</table>";
I've searched everywhere but couldnt find a solution, please help me thanks!
You need to separate each row with its own form. Assuming you want to avoid jQuery
while($row = mysqli_fetch_array($result)) {
echo "<tr><form action='otherscript.php' method='POST'>";
echo "<td>"."<input type = 'text' name='productid' value=". $row['productid'] ."></td>";
echo "<td>"."<input type = 'text' name='productname' value=". $row['productname'] ."></td>";
echo "<td>" . $row['retailprice'] . "</td>";
echo "<td>" . $row['pointsformula'] . "</td>";
echo "<td>"."<button type='button' class='quantityaddsub' id='sub' onclick='quantitysub(".$row['productid'].")'>-</button>".
"<input type='text' class='quantity' name='cartquantity' id='quantity".$row['productid']."' value=1>".
"<button type='button' class='quantityaddsub' id='add' onclick='quantityadd(".$row['productid'].")'>+</button>".
"<input type='hidden' name='hidden' value=".$row['productid']."></td>";
echo "<td>" . "<input type='submit' name='insert' value='Add To Cart'" . "></td>";
echo "</form></tr>";
}
echo "</table>";
That way when you click submit, only the data of that row is sent. Right now it's a bit messy.
这篇关于提交按钮,将表格的每一行插入该行的数据到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!