2017-02-14 17 views
1

私は父のショップのために在庫管理ソリューションをPHPで構築しています。これは、MySQLデータベースからテーブル内の在庫を一覧表示するコードです。私も、「productdetails.php」に移動するものを、ボタン(フォームやボタンクラス)を入れて、変数として現在の行のバーコードを渡すことができますどのようにボタンを作成して次のページに変数を渡すもの

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

私が試した:

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

使用するJavaScript。パラメータを引数として受け取り、ボタンクリックで関数を呼び出すJavaScriptでの関数の書き込み。 – Naincy

答えて

0

私はフロリアンの答え@とそれに少し手を加えて、私はこの作業コードスニペットになってしまった:

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

あなたはactualyかなり接近していました。 このコードスニペットで試してみてください。

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

$ _GET ['barcode']でproductdetails.phpで取得できます。

+0

ありがとうございます。私はそれを試しましたが、多分それは変数を渡していません。 echo '​​

'; そして私はproductdetails.php に小さな "デバッガ" を作っ<?phpの$バーコード= $ _GET [' バーコード ']; エコー "のバーコードがある:$バーコード";?> 出力:バーコード: – Feralheart

+0

一重引用符の後に二重引用符を忘れてしまったと思いますが、method = "get"になる前に $ currentrow [...] method = "get" –

+0

私もそれを試しましたが、それも合格しませんでした。 (どうしてか分かりません)。私はそれを試してみたが、今はmethod = "post"と入力タイプ= "hidden" – Feralheart

関連する問題