繁体   English   中英

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

[英]How to select a row(s) from mysql table using checkbox (PHP)

嗨,我有一个页面,显示我的桌子的所有内容。 沿着表的每一行,我还有一列包含一个复选框。 当用户通过选中复选框并按提交按钮选择一个或多个行时,我只希望这些行出现在下一页的表中(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>

您需要创建一个动态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);
?>
// 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

//此复选框的使用

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

$ query =“选择*来自项目的ID = $ item的项目”; //采用 ” ”

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM