我有以下代码显示我并允许我在产品表/或客户表中进行搜索。我想知道如何将某些产品与某些客户联系起来,就像我在php中创建发票一样
if(isset($_POST['q'])) {
$q = $_POST['q'];
$select = $db->query("SELECT * FROM products WHERE Model LIKE '%$q%' OR Description LIKE '%$q%' ORDER BY id DESC");
} else {
$select = $db->query("SELECT * FROM products ORDER BY id DESC");
}
<form method="post">
<input type="text" name="q" /> <input type="submit" name="search" />
</form>
答案 0 :(得分:1)
我使用按钮而不是div来格式化行中的列,因为<button>
默认显示为inline-block
,并且它们很容易设置样式。我经常使用它们来点击它们时提供额外的信息。 (onclick
)
测试数据库表:
CREATE TABLE IF NOT EXISTS `Invoice` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`customer` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `Items` (
`Invoice` int(11) NOT NULL,
`product` int(11) NOT NULL,
`qty` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `Products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Model` char(16) CHARACTER SET latin1 NOT NULL,
`Description` char(32) CHARACTER SET latin1 NOT NULL,
`Cost` decimal(7,2) NOT NULL,
`Retail` decimal(7,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=3 ;
INSERT INTO `Products` (`id`, `Model`, `Description`, `Cost`, `Retail`) VALUES
(1, 'Model 1', 'Model 1', '0.50', '1.00'),
(2, 'Model 2', 'Model 2', '1.00', '2.00');
CREATE TABLE IF NOT EXISTS `Customer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`Name` char(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
INSERT INTO `Customer` (`id`, `Name`) VALUES
(1, 'Customer Name');
CREATE TABLE IF NOT EXISTS `Invoice` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PHP HTML表单:
的测试强>
<?php
$conn = mysqli_connect("localhost","user","pass","db");
$customer = 1;
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Order</title><meta name="viewport" content="width=320, initial-scale=1.0" />
<style type="text/css">
.desc{border:0;width:15em;}
.model{border:0;width:5em;}
.price{border:0;width:5em;}
</style></head><body><div id="page"><div id="page">
<form action="" method="post"><div>
<button class="go" type="submit">Back</button>
<input type="hidden" name="customer" value="$customer" />
</div></form>
<form action="order.php" method="post"><div>
Customer: <input type="number" name="customer" value="$customer" /><br/><br/>
EOT;
$sql = "SELECT `ID`,`Model`,`Description`,`Retail` FROM `Products` WHERE 1 ORDER BY `Description` LIMIT 25";
$results = mysqli_query($conn,$sql);
if (mysqli_errno($conn) > 0){echo mysqli_error($conn) . "<br/>\n$sql\n";}
while ($row = mysqli_fetch_array($results, MYSQL_NUM)){
echo "<input type=\"number\" name=\"q$row[0]\" /> <button class=\"model\" type=\"button\">$row[1]</button> <button class=\"desc\" type=\"button\">$row[2]</button> <button class=\"price\" type=\"button\">$row[3]</button><br/>";
}
echo '<input type="submit" value="Submit"></div></form></body></html>';
?>
将表格提交给PHP:
的测试强>
<?php
$conn = mysqli_connect("localhost","user","pass","db");
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Order</title><meta name="viewport" content="width=320, initial-scale=1.0" />
<style type="text/css">
</style></head><body><div id="page"><div id="page">
EOT;
foreach ($_POST as $key => $value){
$type = substr($key,0,1);
$id = substr($key,1);
$pair[$type][$id] = intval($value);
}
$sql = "INSERT INTO `Invoice` (`ID`,`customer`) VALUES(NULL,$customer)";
$results = mysqli_query($conn,$sql);
if (mysqli_errno($conn) > 0){echo mysqli_error($conn) . "<br/>\n$sql\n";}
$invoice = mysqli_insert_id ($conn );
echo "<h3>Invoice: $invoice</h3>";
foreach ($pair['q'] as $id => $qty){
echo "<p>Qty: $qty id: $id,</p>";
$sql = "INSERT INTO `Items` (`Invoice`,`product`,`qty`) VALUES($invoice,$id,$qty)";
$results = mysqli_query($conn,$sql);
if (mysqli_errno($conn) > 0){echo mysqli_error($conn) . "<br/>\n$sql\n";}
}
echo '</div></body></html>';
?>