我正在开发一个发票的项目。我有一个包含服务和客户端表的数据库。我尝试从名为selectService的select中提取一个值并将其添加到数组中。问题是每次新选择时,变量都会被覆盖。我需要这个来添加多个发票服务。
<div class="client-select">
<form method='post' action=''>
<select name='selectClient'>
<?php
$query = "select company_name from clients";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<option value="<?php printf ("%s", $row["company_name"]); ?>">
<?php printf ("%s", $row["company_name"]); ?>
</option>
<?php
}
$result->free();
}
?>
</select>
<button type='submit' name='button1'>Załaduj fakturę</button>
</form>
</div>
<?php function createForm($name='',$nip='', $address='', $error='') { ?>
<h2>Dane klienta:</h2>
<div class='client-data'>
<label>Nazwa firmy:</label><input type="text" name="company_name" value="<?php echo $name; ?>" placeholder='NAZWA FIRMY'/>
...
</div>
<?php }
function createCompany() {
include('company-data.php'); ?>
<h2>Dane wystawiającego:</h2>
<div class='company-data'>
<label>Nazwa firmy:</label><input type="text" name="my_company_name" value="<?php echo $comapny_name; ?>" placeholder='NAZWA FIRMY'/>
...
</div>
<?php }
function createPayment() {
include('company-data.php'); ?>
<h2>Dane do płatności:</h2>
<div class='payment-data'>
<label>Data wydania:</label><input type="date" name="issue_date" value="<?php echo $issue_date; ?>" placeholder='DATA WYDANIA'/>
...
</div>
<div class='bank-data'>
<label>Nr konta:</label><input type="text" name="account_number" value="<?php echo $account_number; ?>" placeholder='NR KONTA' size='30'/>
...
</div>
<?php }
function serviceSelect() {
include('db_connect.php'); ?>
<div class="service-select">
<form method='post' action=''>
<select name='selectService'>
<?php
$query = "select service_name from service";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_assoc()) {
?>
<option value="<?php printf ("%s", $row["service_name"]); ?>">
<?php printf ("%s", $row["service_name"]); ?>
</option>
<?php
}
$result->free();
}
?>
</select>
<button type='submit' name='button2'>Dodaj usługę</button>
</form>
</div>
<?php }
function serviceForm($service_name='',$quantity='', $unit='', $net_price='', $net_value='', $vat_rate='', $amount_vat='', $gross_value='', $error='') { ?>
<div class='service-data'>
<label>Nazwa usługi:</label><input type="text" name="service_name" value="<?php echo $service_name; ?>" placeholder='NAZWA USŁUGI'/>
...
</div>
<?php } ?>
if(isset($_POST['button1'])) {
$client = $_POST['selectClient'];
if($stmt = $mysqli->prepare("SELECT company_name,nip,address FROM clients WHERE company_name='$client'")){
$stmt->execute();
$stmt->bind_result($name,$nip,$address);
$stmt->fetch();
createForm($name,$nip,$address,NULL);
$stmt->close();
} else {
echo "Błąd zapytania";
}
createCompany();
createPayment();
serviceSelect();
};
if(isset($_POST['button2'])) {
createCompany();
createPayment();
serviceSelect();
$service = $_POST['selectService'];
if($stmt = $mysqli->prepare("SELECT service_name,quantity,unit,net_price,net_value,vat_rate,amount_vat,gross_value FROM service WHERE service_name='$service'")){
$stmt->execute();
$stmt->bind_result($service_name,$quantity,$unit,$net_price,$net_value,$vat_rate,$amount_vat,$gross_value);
$stmt->fetch();
serviceForm($service_name,$quantity,$unit,$net_price,$net_value,$vat_rate,$amount_vat,$gross_value,NULL);
$stmt->close();
array_push($_SESSION['array'], $service);
} else {
echo "Błąd zapytania";
}
};
$mysqli->close();
如果单击button2,则生成客户端表单的功能将消失。我可能不得不使用其他方式来生成表单,但我不知道。我还指望解决在发票中添加多项服务的问题。
答案 0 :(得分:0)
如果你在谈论这一行:
while ($row = $result->fetch_assoc()) {
通过将数据保存到数组作为数组的新行,可以多次使用数据:
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
//...
}
后来在循环中使用它:
foreach($rows as $row) {
//...
}