How to insert specific row of table data selected, into database table?
<p>This is my BAShop.php file</p>

<pre><code>&lt;?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);                                             
    }   
    ?&gt;
&lt;script&gt;                      
                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 &amp;&amp; xmlhttp.status == 200) {
                                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                            }
                        };
                        xmlhttp.open("GET","BAShopSort.php?q="+str,true);
                        xmlhttp.send();

                }

            &lt;/script&gt;
</code></pre>

<p>And this is my ShopSort.php file where i do my ajax so that users can click on a button and the page changes without refreshing it.</p>

<pre><code>$q = $_GET['q'];
$sql="SELECT * FROM product WHERE productname like '$q%'";
$result = mysqli_query($dbconn,$sql);
echo "&lt;table border=3&gt;
&lt;tr&gt;
&lt;th&gt;Productid&lt;/th&gt;
&lt;th&gt;Productname&lt;/th&gt;
&lt;th&gt;Retailprice&lt;/th&gt;
&lt;th&gt;Pointsformula&lt;/th&gt;
&lt;th&gt;Quantity&lt;/th&gt;
&lt;/tr&gt;";
while($row = mysqli_fetch_array($result)) {   
echo "&lt;tr&gt;&lt;form method='POST'&gt;";
echo "&lt;td&gt;"."&lt;input type = 'text' id ='productid' name='productid' value=". $row['productid'] ."&gt;&lt;/td&gt;";
echo "&lt;td&gt;"."&lt;input type = 'text' id = 'productname' name='productname' value=". $row['productname'] ."&gt;&lt;/td&gt;";
echo "&lt;td&gt;" . $row['retailprice'] . "&lt;/td&gt;";
echo "&lt;td&gt;" . $row['pointsformula'] . "&lt;/td&gt;";
echo "&lt;td&gt;"."&lt;button type='button' class='quantityaddsub' id='sub' onclick='quantitysub(".$row['productid'].")'&gt;-&lt;/button&gt;".
                "&lt;input type='text' class='quantity' name='cartquantity' id='quantity".$row['productid']."' value=1&gt;". //**name of quanitty text
                "&lt;button type='button' class='quantityaddsub' id='add' onclick='quantityadd(".$row['productid'].")'&gt;+&lt;/button&gt;".
"&lt;input type='hidden' name='hidden' value=".$row['productid']."&gt;&lt;/td&gt;";
echo "&lt;td&gt;" . "&lt;input type='submit' id = 'insert' name='insert' value='Add To Cart'" . "&gt;&lt;/td&gt;";
echo "&lt;/form&gt;&lt;/tr&gt;";
}echo "&lt;/table&gt;";
</code></pre>

<p>So after i run the BAShop.php file, lets say i would want to add the item 'Soap' to cart
<a href="https://i.stack.imgur.com/aFRSH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aFRSH.png" alt="enter image description here"></a>
When i go to the cart page, the soap isnt there, instead, the shampoo is there.
<a href="https://i.stack.imgur.com/ftf8P.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ftf8P.png" alt="enter image description here"></a></p>

<p>Please have a look at my code and let me know what is wrong... I have been stuck with this problem for my school project for 3 weeks, and time is running out..
FYI: the data for the table that I have displayed for the BAShop.php page is drawn from mysql database.</p>