如何使用复选框(PHP)从mysql表中选择一行

Mat*_*ins 0 php mysql checkbox select

嗨我有一个页面显示我的表的所有内容.沿着表格的每一行,我还有一个包含复选框的列.当用户通过勾选复选框并按下提交按钮选择一行或多行时,我希望这些行只显示在下一页的表格中(buy.php).我知道它基本上是每行选择的一个select语句.但我不知道如何处理这个问题.有人可以帮忙吗?谢谢

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Shopping</title>
</head>
<body>
<form action='buy.php' method='post'>
<input type='submit' value='Submit' />
</form> 
<h1>Buy</h1>
<?php // Script 12.7 - sopping.php

$db = mysql_connect('localhost', '#####', '#####');
mysql_select_db('shopping', $db);

$query = 'SELECT * FROM Items';

if ($r = mysql_query($query, $db)) { 
    print "<form>
    <table>";
    while ($row = mysql_fetch_array($r)) {
        print 
        "<tr>
        <td>{$row['ID']}</td>
        <td>{$row['Name']}</td>
        <td>{$row['Cost']}</td>
        <td><input type='checkbox' name='buy[{$row['ID']}] value='buy' /></td>
        </tr>";
    }
    print "</table>
    </form>";

} else { 
    print '<p style="color: blue">Error!</p>';
} 

mysql_close($db); // Close the connection.

?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

编辑:我尝试了下面的建议,我没有得到一张桌子.只是页面标题出现:

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Confirmation</title>
</head>
<body>

<h1>Order Details</h1>
<?php // Script 12.7 - buy.php

$db = mysql_connect('localhost', '#####', '#####');
mysql_select_db('shopping', $db);

foreach($_POST['buy'] as $item) {

$query = 'SELECT * FROM Items WHERE ID = $item';

if ($r = mysql_query($query, $db)) { 
    print "<table>";
    while ($row = mysql_fetch_array($r)) {
        print 
        "<tr>
        <td>{$row['ID']}</td>
        <td>{$row['Name']}</td>
        <td>{$row['Cost']}</td>
        </tr>";
    }
    print "</table>";

} else { 
    print '<p style="color: blue">Error!</p>';
} 
}

mysql_close($db);

?>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 6

您需要创建动态SQL查询.

1)我建议您将复选框更改为以下内容

<input type='checkbox' name='buy[]' value='{$row['ID']}' />
Run Code Online (Sandbox Code Playgroud)

2)遍历所有购买选项

<?php
  foreach($_POST['buy'] as $item) {
    // Append the ID (in the $item variable) to the SQL query, using WHERE `ID` = $item OR `ID` = $item and so on
  }
?>
Run Code Online (Sandbox Code Playgroud)

更新:

<?php // Script 12.7 - buy.php

$db = mysql_connect('localhost', '#####', '#####');
mysql_select_db('shopping', $db);

$query = 'SELECT * FROM Items WHERE ';

$item_count = count($_POST['buy']);

for($i = 0; $i < $item_count; $i++) {
  $itemid = (int)mysql_real_escape_string($_POST['buy'][i]); // Secures It
  $query .= '`ID` = '.$itemid;
  if($i +1 < $item_count) {
    $query .= ' OR ';
  }
}

if ($r = mysql_query($query, $db)) { 
    print "<table>";
    while ($row = mysql_fetch_array($r)) {
        print 
        "<tr>
        <td>{$row['ID']}</td>
        <td>{$row['Name']}</td>
        <td>{$row['Cost']}</td>
        </tr>";
    }
print "</table>";
mysql_close($db);
?>
Run Code Online (Sandbox Code Playgroud)