douchen5971 2014-02-19 15:23
浏览 40

使用MySQLi处理复选框时的PHP未定义索引

I have some PHP code that checks a MYSQL database using MySQLi and outputs all of the 'stock' type into a form with a while statement:

$result = mysqli_query($link,"SELECT name FROM items WHERE type='stock' ORDER BY name");
echo '<table style="text-align:center;"><tr><th>Name</th><th>Quantity</th><th>Use</th>';

while($item = mysqli_fetch_array($result)){

        echo '<tr><form action="chooseProduct" method="POST"><td>' . $item['name'] . '</td><td><input type="number" name="amount[]"></td><td><input type="checkbox" name="stock[]" value="' . $item['name'] . '"></td></tr>';

    }

echo '<tr><td rowspan="3" colspan="3"><input type="submit" value="Select"></td></tr></form></table>';

I then need to be able to select the stock that I wish to use and click on select to send me to the "chooseProduct" page where there I should then be able to use the data 'amount' and 'stock' in an array form. However, when using POST to retrieve these values on the chooseProduct page by doing the following:

$stock = $_POST['stock'];
$amount = $_POST['amount'];

I get the error as follows:

Notice: Undefined index: stock in C:\xampp\htdocs\production\chooseProduct\index.php on line 59

Notice: Undefined index: amount in C:\xampp\htdocs\production\chooseProduct\index.php on line 60

The checkbox is definitely checked, so what could be causing this error?

  • 写回答

2条回答 默认 最新

  • douqin0676 2014-02-19 15:26
    关注

    If you've not checked if POST action is already made so there will be warning / notice on assigning it. try

    $stock = isset($_POST['stock']) ? $_POST['stock'] : '';
    $amount = isset($_POST['amount']) ? $_POST['amount'] : '';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型