您好,我是一名 PC 和笔记本电脑维修人员,想建立一个离线数据库站点,我可以在其中输入有关维修、客户和发票的详细信息。我对sqli和php有非常基本的了解,因为我才刚刚开始尝试学习它。
目前,我的数据库中有 3 个表,它们彼此没有链接。我使用 3 种表格将数据提交到其中。
客户
Id – name – surname – phone – address
维修
Id – name – surname – phone – address – unit – date – price
发票
Id - paidstatus – date paid - name – surname – phone – address- unit – date – price
由于我缺乏知识,我将数据显示在 3 个不同的页面上
Clients.php
Repairs.php
Invoices.php
这对我来说不是理想的解决方案,但这样做是因为我的知识非常基础。例如,当我转到我的表单添加新维修时,我想从客户表中提取信息并使用以下详细信息: 姓名 – 姓氏 – 电话 – 地址然后在发票上,我喜欢调用其他表中的数据。
我认为我所追求的被称为数据库规范化,我有一些链接,但在几个问题上追求某人的专业知识......
这是我从上面的表中提取数据的当前方式,我使用了示例,因此代码可能不会反映在表的名称或行上。
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from repair_jobs";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record
//start table
//creating our table heading
echo " <table class='table_basic'>";
echo "<thead><tr>";
echo "<th>Job #</th>";
echo "<th>Name Of Unit</th>";
echo "<th>Client</th>";
echo "<th>Estimated Value</th>";
echo "</thead></tr><tbody><tr>";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td width='40px'><a href='rdetails.php?id={$id}'># {$id}</a></td>";
echo "<td>{$type}</td>";
echo "<td>{$notes}</td>";
echo '<td align="center"><span class="badge badge-success">£';
include('repairest.php');
echo '</span></td>';
echo "</td>";
echo "";
}
echo "</tr></table>";//end table
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
使用规范化提取信息会有多大不同?也可以发布指向简单教程的链接,也许可以下载教程源文件?
另外,输入数据的形式会有多大不同?
上帕特
关于米亚的回答
如何通过 SQL 创建和联接表? 我在评论字段中的此问题下链接的页面上看到了示例,但您能否告诉我将其加入的方法
一对一和多重。
我的想法是否正确,当我添加新的修复时,我只会使用我的表单来添加 单位 – 日期 – 价格但是,如果未创建客户端怎么办?还是必须先制作客户?
类似的东西
客户:旧(下拉(或新(按钮((可能需要一段代码,这样我就可以做一些研究(
然后我会输入维修数据。
发票可以在之后生成,因为所需的所有信息都可以从维修表中获得,并且
我找不到一个有源代码的,但这里有一个有一些图表。
关键是您不想跨表重复数据,因此,例如,您可以将表修改为如下所示:
客户
Id – name – surname – phone – address
维修
Id – client_id – unit – date – price
发票
Id - repair_id - paidstatus – date paid
- 客户端
- 到维修是一对多的关系,即 1 个客户端可以有多个维修,一个维修只能有 1 个客户端。 客户
- 到发票是通过维修的一对多关系,即 1 个客户可以有多个发票,而一张发票只能有 1 个客户。
- 发票维修是一对一的,我认为这可能是一个问题,因为将多个维修绑定到一张发票可能是有意义的,但这是一个很好的起点。
您始终可以从发票开始,并在修复发票时使用外键,而不是客户端。 就业务逻辑而言,这有点交换,但我认为它会给你更大的灵活性:
客户
Id – name – surname – phone – address
发票
Id - client_id - paidstatus – date paid
维修
Id – invoice_id – unit – date – price
更新
好的,让我们假设您决定跟上原始模型,而不是我提到的修改版本。 您将像现在一样创建每个表,并且只需确保修复中的client_id
与客户端中的id
数据类型相同。
然后,您可以使用 JOIN(这是一个讨论 SQL JOIN 的 SO 答案(运行单个查询来提取数据:
# all the data
SELECT * FROM invoices i JOIN repairs r ON i.repair_id = r.id JOIN clients c ON r.client_id = c.id
# all the data for a specific customer
SELECT * FROM invoices i JOIN repairs r ON i.repair_id = r.id WHERE r.client_id = <id goes here>
# Also, if JOINs seem intimidating, you can reference multiple tables
# and link them all together in a where clause
SELECT * FROM invoices i, repairs r, clients c WHERE i.repair_id = r.id AND r.client_id = c.id
# But you don't always need to get all of the data at once.
# It makes a lot of sense to grab only what you need and create links to detail pages.
# So grab all of the unpaid invoices, and provide a link to a page that write that
# shows the client and repair details associated with that invoice
SELECT * FROM invoices i WHERE i.paidstatus='unpaid'
现在,您仍然必须首先创建客户端,但是在"创建修复"窗体中,您将有一个选择框,其中包含以客户端表中的 id 作为值的选项。
//query all records from the database
$query = "select id, name, surname from clients";
//execute the query
$result = $mysqli->query( $query );
echo("<select name='client_id'>");
while( $row = $result->fetch_assoc() ){
echo("<option value='" . $row['id'] . "'>" . $row['name'] . " " . $row['surname'] . "</option>";
}
echo("</select>");
是的,您可以在修复表单中拥有新的客户端字段,这只会增加数据库插入的复杂性,因此由您决定。
这里的基本思想是,您从表中获取您希望其他表引用的数据的 id。 这样,如果client_A更改了他们的电话号码,您只需将其更改一个位置即可。
<a href = "Page.php?View=Invoices">View Invoices</a>
<a href = "Page.php?View=Repairs">View Repairs</a>
<a href = "Page.php?View=Clients">View Clients</a>
<?php
if (isset($_GET['View'])){
//query all records from the database
$query = "select * from ".$mysqli->real_escape_string($_GET['View']);
//execute the query
$result = $mysqli->query( $query );
}
与此类似的方法将创造奇迹,并允许您将视图保留在一页上。如果数据库设置或多或少相同,则在个别情况下,如果$_GET['View'];
,则应根据以下结果验证视图:
if ($_GET['View'] === "Test_1"){
echo "Validate as Clients";
}elseif ($_GET['View'] === "Test_2"){
echo "Validate as Repairs";
}elseif ($_GET['View'] === "Test_3"){
echo "Validate as Invoices";
}
这是一个演示,您应该将此代码/结果调整到您自己的环境中