dse84825 2011-01-12 13:41
浏览 79

php / mysql显示所选复选框的数据

I have a table called FRUIT

id  type    daysold
1   banana  5
2   apple   6
3   apple   4
4   peach   2
5   banana  6

What I would like is to have 3 checkboxes:
Banana [ ]
Apple [ ]
Peach [ ]
SUBMIT

Then if I've only checked "Banana" and "Peach" the mysql output should only show me the rows that matches those two types. And the checkboxes should remain checked to highlight what was chosen.

I can make the checkboxes but then that's about it really. I don't know how to properly get the info from the checkboxes and down to the WHERE argument in the MYSQL-code. Especially not with two types chosen.

If it was just a dropdown menu with a single choice then I'd add the choice to the url and put WHERE type='$choice' - but I'm struggling with the multiple choices.

I'm a bit of a novice at both php and mysql, so I'm a bit lost on this one.

  • 写回答

1条回答 默认 最新

  • doulin9679 2011-01-12 13:58
    关注

    I'd make a form like this:

    <form action="processingPage.php" method="post" name="nameHere">
        <input type="checkbox" name="fruit[]" value="Banana" /> Banana<br />
        <input type="checkbox" name="fruit[]" value="Apple" /> Apple<br />
        <input type="checkbox" name="fruit[]" value="Peach" /> Peach<br />
        <input type="submit" value="Submit" /> 
    </form>
    

    In processingPage.php:

    <?php
    $fruits = array();
    foreach($_POST['fruit'] as $fruit) {
        $fruit = mysql_real_escape_string($fruit);
        $fruits[] = "'{$fruit}'"; 
    }
    
    $sql = "select * from fruit where type in (" . implode(", ", $fruits) . ")";
    //execute query and retrieve results
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)