dongque3797 2014-10-28 19:12
浏览 46
已采纳

Php数组循环,为购买表单创建动态变量

Still getting used to stackoverflow excuse my rookie-ness.. :)

Have an SQL query that returns a data put into a table in php. I want this table to be used for purchasing.

My idea was to use the product id, meaning i would use a dynamic php variable (not sure if I'm doing that right now) believe I saw a post something like $.$varaible$.$ it wasn't very clear and was a different subject.

My code is as follows:

$result = mysqli_query($con, "SELECT * FROM Product WHERE Type = 'Game'");
?>

<div class="wrapper">
    <h1 class="headGame">Buy Some Games Man</h1>
</div>
<br />
<div class="wrapper">

<?php
    echo 
    "<table border='1'>
    <tr>
    <th> Name </th>
    <th> Picture </th>
    <th> Console </th>
    <th> Description </th>
    <th> Price </th>
    <th> Amount </th>
    </tr>";

    echo '<form id="gamesOrder" action="purchase.php">';

    while($row = mysqli_fetch_array($result)) {
        $id = $row['Pd_Key'];
        echo"<tr>";
        echo"<td>" . $row['Name'] . "</td>";
        echo"<td>" . '<img class="prdPic" src="'. $row['Picture']. '">' . "</td>";
        echo"<td>" . $row['Type2'] . "</td>";
        echo"<td>" . $row['Description'] . "</td>";
        echo"<td>" . $row['Price'] . "</td>";
        echo"<td>"  . '<input type="number" min="0" max="100"; name="'.$id.'" value=0>' . "</td>";
        echo"</tr>";
    }
    echo '<input type="submit" value="  BUY  ">';
    echo '</form>';
?>

When I click the submit it changes the url but nothing happens, it doesn't redirect.

Any advice on how to get this entire process to work. The variable being used in a purchasing form, via a php file ie (purchase.php) and the variable to use for this.

EDIT - Had minor errors, but still not 100% on variable %id, won't that get redefined each loop, how can I have it dynamic so it can be used in a form to identify what the user wants to buy.

Now redirects but not to purchase.php

URL is ~/purchase.php?1=0&2=0&3=0&4=0&5=0&6=0&7=0&8=0&9=0&10=0&11=0&12=0&13=0&14=0&15=0&16=0&17=0&18=0&19=0&20=0&21=0

Thanks you legends you!! =D

  • 写回答

3条回答 默认 最新

  • doutu2017 2014-10-28 19:14
    关注

    You are missing the closing form caret:

    echo '<form id="gamesOrder" action"purchase.php"';
    

    should be:

    echo '<form id="gamesOrder" action="purchase.php">';
    

    Also you concatenation for ID is incorrect:

    echo"<td>"  . '<input type="number" min="0" max="100"; name=".$id." value=0>' . "</td>";
    

    should just be:

    echo"<td>"  . '<input type="number" min="0" max="100"; name="id" value="' .$id. '" value=0></td>';
    

    To access the id in purchase.php use the following code:

    $id = isset($_GET['id']) ? $_GET['id'] : null;
    

    And you need to assign the action to the form with an equals sign:

    action="myaction.php"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上