简体   繁体   English

我将如何在 php 中编写 sql 查询以获得此结果?

[英]How would I write the sql query in php to get this result?

First of all I am currently trying to learn php and I thought I would build a basic maintenance management app to better grasp everything I see in tutorials, and I hit a roadblock.首先,我目前正在尝试学习 php,我想我会构建一个基本的维护管理应用程序来更好地掌握我在教程中看到的所有内容,但我遇到了障碍。 I've been trying to get this to work following various tutorials online but I had no success so far, so I thought I would ask for help here.我一直在尝试按照在线的各种教程使其工作,但到目前为止我没有成功,所以我想我会在这里寻求帮助。 I really hope you can help me.我真的希望你能帮助我。

What I would like to know is how can I write the php & mysql to write the data from the first 3 table to the desired_table from below ?我想知道的是如何编写 php 和 mysql 将前 3 个表中的数据从下面写入到所需的表?

Clients
------------------------------
client_id    | client_name
------------------------------  
    1        | client_1 
------------------------------  
    2        | client_2
------------------------------  
    3        | client_3 


Equipments
------------------------------
eq_id        | eq_name
------------------------------  
    1        | pc 
------------------------------  
    2        | laptop
------------------------------  
    3        | printer


Operations
------------------------------------------------
op_id        | op_desc
------------------------------------------------    
    1        | dust cleaning
------------------------------------------------    
    2        | changing processor cooling paste
------------------------------------------------    
    3        | cpu replacement
------------------------------------------------    
    4        | display replacement
------------------------------------------------    
    5        | dvd-rom replacement
------------------------------------------------    
    6        | ram replacement
------------------------------------------------    
    7        | cartrige replacement



Desired_table
-------------------------------------
id  | client_id  | eq_id  | op_id  |
-------------------------------------   
  1 |    1       |    1   |   1    |
-------------------------------------
  2 |    1       |    1   |   2    |
-------------------------------------   
  3 |    1       |    1   |   3    |
-------------------------------------   
  4 |    1       |    1   |   5    |
-------------------------------------   
  5 |    1       |    2   |   1    |
-------------------------------------   
  6 |    1       |    2   |   2    |
-------------------------------------   
  7 |    1       |    2   |   4    |
-------------------------------------   
  8 |    2       |    1   |   1    |
-------------------------------------   
  9 |    2       |    1   |   2    |
-------------------------------------   
  10|    2       |    1   |   3    |
-------------------------------------   
  11|    2       |    1   |   5    |
-------------------------------------   
  12|    2       |    3   |   1    |
-------------------------------------   
  13|    2       |    3   |   7    |

I thought I would have a form with input fields for the client data and the equipment data and operations data I would have in dropdowns.我想我会有一个表单,其中包含客户端数据和设备数据和操作数据的输入字段,我将在下拉列表中使用。 When I would select an equipment a new dropdown would appear with the operations, and then submit it.当我选择设备时,会出现一个带有操作的新下拉列表,然后提交它。

Hope this doesn't get marked as too broad subject :)希望这不会被标记为太广泛的主题:)

Using the code from your bitbucket this should do it:使用您的 bitbucket 中的代码应该可以做到:

$page = $db->prepare("
    INSERT INTO new_table_name (column_name1, column_name2, column_name3) 
    VALUES (:col1, :col2, :col3)
");

$page->bindparam(':col1', $value1);
$page->bindparam(':col2', $value2);
$page->bindparam(':col3', $value3);
$page->execute();
if($page->rowcount() > 0){
    echo "Inserted a new row";
}

Here is a example, replace 'new_table_name' to the name off the new table and change the column names.这是一个示例,将 'new_table_name' 替换为新表的名称并更改列名称。

After that simiply change $value1, 2 and 3 to the variables that contain the value you want to insert and you're good to go.之后,只需将 $value1、2 和 3 更改为包含要插入的值的变量,就可以了。

If you want to know if the insert was succesful just check with $page->rowcount().如果您想知道插入是否成功,只需检查 $page->rowcount()。 If the rowcount is more than 0 it was succesful.如果行数大于 0,则表示成功。

And just to clarify, this is called a prepared statement if you don't know this yet.澄清一下,如果您还不知道,这称为准备好的声明。 What the code does is this:代码的作用是这样的:

  • Prepare tells the server what you're planning to do. Prepare 告诉服务器你打算做什么。 This adds a really strong layer off protection since basic sql injection are close to impossible to do.这增加了一个非常强大的保护层,因为基本的sql 注入几乎不可能做到。

  • After that you bind the variables to the given parameters (Other syntax is possible, I just prefer this one).之后,您将变量绑定到给定的参数(其他语法也是可能的,我更喜欢这个)。 The code takes care off quotations so just put the parameter in there.代码会处理引号,因此只需将参数放在那里即可。

  • Execute the actual query执行实际查询

And just for good measure, you can add 2 extra parameters to bindparam.为了更好的衡量,您可以向 bindparam 添加 2 个额外的参数。 In the third you can define a variable type, PDO::PARAM_STR and PDO::PARAM_INT block any other type off variable.在第三个中,您可以定义一个变量类型,PDO::PARAM_STR 和 PDO::PARAM_INT 阻止任何其他类型的变量。 The 4th lets you set a max length, if the variable is longer it will get blocked.第四个让你设置最大长度,如果变量更长,它会被阻塞。

This will look like this:这将如下所示:

$page->bindparam(':col1', $value1, PDO::PARAM_STR, 100);
//block anything which isn't a string or longer than 100 characters

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

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