简体   繁体   English

在一个 php 页面中访问来自多个表 (SQL) 的数据

[英]Acessing data from multiple tables (SQL) in one php page

Hi im new in php/sql programing, im making a website in html and whem i enter the fields on texbox i could pass the data to mysql database.嗨,我是 php/sql 编程的新手,我用 html 制作了一个网站,我输入了 texbox 上的字段,我可以将数据传递给 mysql 数据库。

html example: html示例:

<form action="insert.php" method="get">
<TABLE>

<TR>
<TH style="width: 454px">

<select name="marcas">
<option value ="1"> Peugeot   </option>
<option value = "2"> Renault  </option>
<option value ="3"> Opel </option>
</select>
<select name="modelo">
<option value ="1"> 105   </option>
<option value = "2"> 205  </option>
<option value ="3"> 507 </option>
</select>
<P align="left">Matricula: <INPUT NAME="matricula" TYPE=text SIZE="20">
<P align="left">Ano: <INPUT NAME="ano" TYPE=text SIZE="60">
<p align="left">Quilometros: <INPUT NAME="kms" TYPE=text SIZE="20"> 
<tr><td>Estado:</td><td>
<input type="radio" name="estado"value="usado">Usado<br>
<input type="radio" name="estado" value="novo">Novo
</td></tr>  

</TH>
</TR>
</TABLE>

Now i got PHP to connect to table cars:现在我让 PHP 连接到桌车:

<?php
  include "connect.php";

$sql = "INSERT INTO carro (id_modelo, id_cor,id_extras, matricula, ano, kms,    estado) values (?,?,?,?,?,?,?)";

if ($stmt = $mysqli->prepare($sql)) {
$stmt->bind_param("sssssss",$_REQUEST["id_modelo"],$_REQUEST["id_cor"],$_REQUEST["id_extras"], $_REQUEST["matricula"], $_REQUEST["ano"], $_REQUEST["kms"], $_REQUEST["estado"]);

$stmt->execute();

if ($stmt->affected_rows == -1) {
  echo "<p>". $mysqli->error. "</p>";
}
else {
  echo "<sp>Carro Inserido com Sucesso!</p>";
}

$stmt->close();
}

$mysqli->close();
?>

My question is , i want that some of the fields in textbox connect with other diferent table but i dont know if thats possible, for example something like:我的问题是,我希望文本框中的某些字段与其他不同的表连接,但我不知道这是否可能,例如:

$sql = INSERT INTO car
$sql2 = INSERT INTO engine
$sql3 = INSERT INTO brand

some code that can make me in the same page enter the data for diferent tables一些代码可以让我在同一页面中输入不同表的数据

Thanks谢谢

You're on the right track already, although you cannot insert into different tables in a single INSERT INTO query.尽管您无法在单个INSERT INTO查询中插入不同的表,但您已经走上了正确的轨道。

Instead all you need to do is perform three separate INSERT INTO queries, ie one for each table.相反,您需要做的只是执行三个单独的INSERT INTO查询,即每个表一个。

In each query just use the value from your $_REQUEST data that you need.在每个查询中,只需使用您需要的$_REQUEST数据中的值。 For example once you've done your query to add data to your Cars table you would just perform another query for eg Engine like:例如,一旦您完成查询以将数据添加到 Cars 表,您只需执行另一个查询,例如 Engine,例如:

$sql2 = "INSERT INTO Engines (** Columns **) values (?,?,?,?,?,?,?)";

if ($stmt = $mysqli->prepare($sql)) {
    $stmt->bind_param(...);

$stmt->execute();

...

}

and bind the inputs you want like you did before.并像以前一样绑定您想要的输入。 All you'll need to do is add extra input fields to your form to get the extra data you want.您需要做的就是在表单中添加额外的input字段以获得所需的额外数据。

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

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