1

I building an inventory management solutions in php for my father's shop. This is the code what lists the inventory in table from MySQL database.

<?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);?>

How I can put a button (form or button class), what navigates to the "productdetails.php" and pass the current row's barcode as a variable, too?

I tried:

<form action="productdetails.php" method="get">
    <button type="submit" class="button" value="' . $current_row["barcode"] . '">Product Details</button> 
</form>
1
  • use JavaScript. Write function in JavaScript that accepts the parameter as argument and call function on button click. Commented Feb 14, 2017 at 10:55

2 Answers 2

1

You were pretty close actualy. Try it with this code snippet;

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

You will then be able to get it in the productdetails.php with $_GET['barcode'].

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I tried it, but somewhy it's not passing the variable. echo '<td> <form action="productdetails.php?barcode=' . $current_row["barcode"] . ' method="get"> <button type="submit" class="button">Product details</button> </form></td>'; And I made a small "debugger" in a productdetails.php <?php $barcode = $_GET['barcode']; echo "The barcode is: $barcode";?> Output: The barcode is:
I think you forgot a double quote after the single quote before method="get". It should be $currentrow[...] <dot> <singlqQuote><doubleQuote> method="get"
I tried that, too but it didn't passed it either. (I don't know why). I tinkered with it and now it's working with method="post" and input type="hidden"
0

I tinkered with it a bit with @Florian's answer and I ended up with this working code snippet:

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>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.