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 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致