创建一个将变量传递给下一页的按钮

时间:2017-02-14 10:51:42

标签: php mysql

我为父亲的商店在php中构建了一个库存管理解决方案。这是从MySQL数据库中列出表中清单的代码。

<?php 
    $sql = "SELECT * FROM inventory";
    $res = mysqli_query($conn, $sql) or die ('Wrong statement!');
    echo '<table border=1>';
    echo '<tr>';
    echo '<th>ID</th>';
    echo '<th>Type</th>';
    echo '<th>Name</th>';
    echo '<th>Wholesaleprice</th>';
    echo '<th>Price</th>';
    echo '<th>Supplier</th>';
    echo '<th>Place</th>';
    echo '<th>Place2</th>';
    echo '<th>Count</th>';
    echo '<th>Barcode</th>';
    echo '<th>Last Change</th>';
    echo '<th>Details</th>
    echo '</tr>';       

    while ( ($current_row = mysqli_fetch_assoc($res))!= null) {
            echo '<tr>';
            echo '<td>' . $current_row["ID"] . '</td>';
            echo '<td>' . $current_row["type"] . '</td>';
            echo '<td>' . $current_row["name"] . '</td>';
            echo '<td>' . $current_row["wholeprice"] . '</td>';
            echo '<td>' . $current_row["price"] . '</td>';
            echo '<td>' . $current_row["wholesaler"] . '</td>';
            echo '<td>' . $current_row["whereis"] . '</td>';
            echo '<td>' . $current_row["whereis2"] . '</td>';
            echo '<td>' . $current_row["count"] . ' ' .   $current_row["dimension"] . '</td>';
            echo '<td>' . $current_row["barcode"] . '</td>';
            echo '<td>' . $current_row["lastchange"] . '</td>';
            echo '<td> HERE GOES THE BUTTON </td>';
            echo '</tr>';
    }
    echo '</table>';                                
    mysqli_free_result($res);?>

我如何设置按钮(表单或按钮类),导航到“productdetails.php”并将当前行的条形码作为变量传递?

我试过了:

<form action="productdetails.php" method="get">
    <button type="submit" class="button" value="' . $current_row["barcode"] . '">Product Details</button> 
</form>

2 个答案:

答案 0 :(得分:1)

你非常接近实际。 尝试使用此代码段;

<form action="productdetails.php?barcode=' . $current_row["barcode"] . '" method="get">
    <button type="submit" class="button">Product Details</button> 
</form>

然后您可以使用$ _GET [&#39;条形码&#39;]在productdetails.php中获取它。

答案 1 :(得分:0)

我用@ Florian的答案修改了一下,我最终得到了这个有效的代码片段:

while ( ($current_row = mysqli_fetch_assoc($res))!= null) {
$barcode= $current_row["barcode"];

 echo '<tr>';
 echo '<td>' . $current_row["ID"] . '</td>';
 echo '<td>' . $current_row["type"] . '</td>';
 echo '<td>' . $current_row["name"] . '</td>';
 echo '<td>' . $current_row["wholeprice"] . '</td>';
 echo '<td>' . $current_row["price"] . '</td>';
 echo '<td>' . $current_row["wholesaler"] . '</td>';
 echo '<td>' . $current_row["whereis"] . '</td>';
 echo '<td>' . $current_row["whereis2"] . '</td>';
 echo '<td>' . $current_row["count"] . ' ' . $current_row["dimension"] . '</td>';
 echo '<td>' . $current_row["barcode"] . '</td>';
 echo '<td>' . $current_row["lastchange"] . '</td>';?>
 <td> <form method="post" action='productdetails.php'>
    <input type="hidden" name="barcode" value="<?php echo "$barcode"?>"/>
    <button type="submit" class="button">More>></button> 
    </form></td>
 <?php echo '</tr>';
}