如何将选中的特定行数据表插入数据库表?

时间:2016-06-06 07:08:44

标签: php sql ajax html-table sql-insert

这是我的BAShop.php文件

<?php      
    if(isset($_POST['insert'])){                
    $queryinsert = "insert into cart(productid,productname,cartquantity,amount) SELECT productid,productname,'$_POST[cartquantity]', '50' FROM product WHERE productid='$_POST[hidden]'";       
   mysqli_query($dbconn,$queryinsert);                                             
    }   
    ?>
<script>                      
                function sortproduct(str) {

                        if (window.XMLHttpRequest) {
                            // code for IE7+, Firefox, Chrome, Opera, Safari
                            xmlhttp = new XMLHttpRequest();
                        } else {
                            // code for IE6, IE5
                            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        xmlhttp.onreadystatechange = function() {
                            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                            }
                        };
                        xmlhttp.open("GET","BAShopSort.php?q="+str,true);
                        xmlhttp.send();

                }

            </script>

这是我的ShopSort.php文件,我在其中执行我的ajax,以便用户可以点击按钮,页面更改而无需刷新。

$q = $_GET['q'];
$sql="SELECT * FROM product WHERE productname like '$q%'";
$result = mysqli_query($dbconn,$sql);
echo "<table border=3>
<tr>
<th>Productid</th>
<th>Productname</th>
<th>Retailprice</th>
<th>Pointsformula</th>
<th>Quantity</th>
</tr>";
while($row = mysqli_fetch_array($result)) {   
echo "<tr><form method='POST'>";
echo "<td>"."<input type = 'text' id ='productid' name='productid' value=". $row['productid'] ."></td>";
echo "<td>"."<input type = 'text' id = 'productname' 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>". //**name of quanitty text
                "<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' id = 'insert' name='insert' value='Add To Cart'" . "></td>";
echo "</form></tr>";
}echo "</table>";

所以在我运行BAShop.php文件之后,让我说我想要添加项目&#39; Soap&#39;去购物车 enter image description here 当我去购物车页面时,肥皂不在那里,相反,洗发水就在那里。 enter image description here

请查看我的代码,让我知道出了什么问题......我已经为我的学校项目困扰了这个问题3个星期,时间已经不多了... 仅供参考:我为BAShop.php页面显示的表的数据是从mysql数据库中提取的。

1 个答案:

答案 0 :(得分:1)

问题是,如果您在表格中同时显示多篇文章,那么您的文档在同一表单中包含几个名为 hidden 的输入。由于PHP处理POST变量的方式,在表单提交时只考虑第一个变量。这就是为什么表格中显示的第一篇文章(在你的例子中,肥皂)将被添加到购物车中。无论如何。

要更改此设置,您可能要么为隐藏字段提供更具体的ID,并修改JS代码以做出相应的反应,或者每行创建一个表单(这可能是解决问题的最快方法)。

P.S。 :如果此脚本要在本地以外的任何地方运行,您还应该处理这些SQL查询。它们危险地不安全。

相关问题