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

时间:2012-03-31 12:21:49

标签: php mysql select checkbox

您好我有一个页面显示我表格的所有内容。沿着表格的每一行,我还有一个包含复选框的列。当用户通过勾选复选框并按下提交按钮选择一行或多行时,我希望这些行只显示在下一页的表格中(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>
编辑:我尝试了下面的建议而我没有得到一张桌子。只是页面的标题出现:

            <!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>

3 个答案:

答案 0 :(得分:6)

您需要创建一个动态SQL查询。

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

<input type='checkbox' name='buy[]' value='{$row['ID']}' />

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
  }
?>

更新:

<?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);
?>

答案 1 :(得分:0)

// this checkbox use 
<input type='checkbox' name='buy[]' value='{$row['ID']}' />


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

$query = "SELECT * FROM Items WHERE ID = $item";
//use " "

//   try it if any problem please carefully look you database table
// download file and try it 2file with table sql data

// https://copy.com/8ZsBAFj7LvypLJkK

答案 2 :(得分:0)

//使用此复选框

// buy.php foreach($ _ POST ['buy'] as $ item){

$ query =“SELECT * FROM Items WHERE ID = $ item”; //使用“”

//尝试一下如果有任何问题请仔细查看数据库表//下载文件并尝试使用表sql数据2file

// https://copy.com/8ZsBAFj7LvypLJkK